#ifndef XML_FIND_CREATE_H
#define XML_FIND_CREATE_H


//Global template functions to CREATE and to FIND the underlying xmlObjects.
//They are declared as static functions of a fake class called xmlFC ( for Find-Create )



struct xmlFC
{


template<class XMLIZED>
static xmlObject_base * find_xmlObject_base( XMLIZED & object)
{
  map<void *,xmlObject_base *>::iterator iter = xmlObject_base::reverseMap.find(& object);
  if (iter != xmlObject_base::reverseMap.end())
    return ((*iter).second);
  return 0;
}


template<class XMLIZED>
static xmlObject<XMLIZED> * find_xmlObject( XMLIZED & object)
{
  map<void *,xmlObject_base *>::iterator iter = xmlObject_base::reverseMap.find(& object);
  if (iter != xmlObject_base::reverseMap.end())
  {
    xmlObject<XMLIZED> * res = dynamic_cast< xmlObject<XMLIZED>* > ((*iter).second);
    if ( res != 0 )
      return res;
    else
      cerr<<"ERROR in find_xmlObject() : the reference object was already registered in the xml reverseMap, but under another type! Make sure that you have added all your objects in the xml structure with functions add_in_XML() or add_child_XML() , before calling register/save/load_XML functions on these objects. If you have, then... dump xmlParameters++ and get a REAL serializing tool (I mean... it should not happen, right...)"<<endl;
  }
  return 0;
}


//THIS IS THE ONLY PLACE WHERE FUNCTION xml_ize() is called on the XMLIZED object! But it is crucial :-)

template<class XMLIZED>
static xmlObject<XMLIZED> * create_xmlObject( XMLIZED *object, std::string name = "" )
{
  //ENTERING( create_from_XMLIZED)

  xmlObject<XMLIZED> * res = new xmlObject<XMLIZED>(*object , name);
  object -> xml_ize() ;
  return res;
  //EXITING( create_from_XMLIZED)
}

//other object types, based on an XMLIZED class:

#define create_macro(objectType) \
template<class XMLIZED> \
static xmlObject<objectType > * create_xmlObject( objectType * object, std::string name = "" ) \
{ \
return new xmlObject<objectType > (*object , name); \
}

create_macro(vector<XMLIZED> )
create_macro(list<XMLIZED> )
create_macro(deque<XMLIZED> )
create_macro(XMLIZED * )

};

#endif


