#ifndef _TXMLOBJECT_H_
#define _TXMLOBJECT_H_

#include "tobject.h"
#include "tinyxml/tinyxml.h"

#include <map>
#include <string>

using namespace std;

/**
 * \brief Base class for all object, which can be serialized/deserialized.
 * 
 * \date 01-13-2006
 *
 * \author Ondrej Svejdar
 *
 * \todo 
 *
 * \bug 
 *
 */
class TXmlObject:public TObject
{
public:
	typedef TXmlObject* (*t_xml_factory)();
	typedef map<string,t_xml_factory> t_ds_map;
	typedef map<string,const char*> t_name_map;
private:
	static t_ds_map dsMap;
	static t_name_map nameMap;
protected:
	virtual void doSerialize(TiXmlElement& element) const=0;
	virtual void doDeserialize(const TiXmlElement& element)=0;
public:
	static const char* type;
	const char* getType() const;
	bool hasParent(const char* className) const;

	/**
	 * \brief Deserialization.
	 * Create new TXmlObject from XML element
	 * \param element XML element
	 * \return new instance of TXmlObject
	 */
	static TXmlObject* getXmlObject(const TiXmlElement& element);
	
	/**
	 * \brief Translate class name to pointer, required to pointer comparation.
	 * \note This works only on classes enregistred by registerClass method
	 * \param name class name
	 */
	static const char* getClassUid(const string& name);

	/**
	 * \brief Enregister factory for creating instances of specific class.
	 * \param className name of class
	 * \param method pointer to factory method
	 */
	static void registerClass(const char* className,t_xml_factory method);
	
	/**
	 * \brief Enregister all classes, which can perform serialization/deserialization.
	 */
	static void registerAll();

	/**
	 * \brief Serialization.
	 * Create XML element from object instance.
	 * \return XML element
	 */
	TiXmlElement& serialize() const
	{
		TiXmlElement* element=new TiXmlElement(getType());
		doSerialize(*element);
        
		return *element;
	}
};

#endif/*_TXMLOBJECT_H_*/

