| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 | /** * XML Serialization * Simple and lightweight xml serialization class *  * Original code by Lothar Perr *  * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any * damages arising from the use of this software. *  * Permission is granted to anyone to use this software for any * purpose, including commercial applications, and to alter it and * redistribute it freely */#pragma once#include "stdafx.h"#include <string>#include <vector>#include <map>#include <set>#include "tinyxml2.h"/**	XML Serialization*/namespace xmls{	//using namespace std;#define  RootClassName  "XMLData"#define XMLClassEndTag  std::string("</")+RootClassName+">"#define Empty_String  std::string("")	class Slo;	/**	serializable members base class*/	class __declspec(dllexport) MemberBase	{	protected:		std::string m_sValue;	public:		virtual ~MemberBase() {};		std::string toString(){return m_sValue;};		const char* c_str() {return m_sValue.c_str();};		std::string *getStringPtr() {return &m_sValue;};	};/**	serializable string*/		class __declspec(dllexport) xString: public MemberBase	{	private:		void AssignValue(const std::string value){m_sValue=value;};	public:		xString(){};		xString(std::string value) {AssignValue(value);};		std::string value(){return m_sValue;};		xString operator=(const std::string value) {AssignValue(value);return *this;};		xString operator=(const char* value) {AssignValue(value);return *this;};	};/**	serializable int*/		class __declspec(dllexport) xInt: public MemberBase	{	private:		void AssignValue(const int value);	public:		xInt() {AssignValue(0);};		xInt(int value) {AssignValue(value);};		int value();		xInt operator=(const int value) {AssignValue(value);return *this;};	};	class __declspec(dllexport) xDouble : public MemberBase	{	private:		void AssignValue(const double value);	public:		xDouble() { AssignValue(0); };		xDouble(double value) { AssignValue(value); };		double value();		xDouble operator=(const double value) { AssignValue(value); return *this; };	};	class __declspec(dllexport) xLong : public MemberBase	{	private:		void AssignValue(const long value);	public:		xLong() { AssignValue(0); };		xLong(long value) { AssignValue(value); };		long value();		xLong operator=(const long value) { AssignValue(value); return *this; };	};	class __declspec(dllexport) xDWORD : public MemberBase	{	private:		void AssignValue(const DWORD value);	public:		xDWORD() { AssignValue(0); };		xDWORD(DWORD value) { AssignValue(value); };		DWORD value();		xDWORD operator=(const DWORD value) { AssignValue(value); return *this; };	};/**	serializable bool*/		class __declspec(dllexport) xBool: public MemberBase	{	private:		void AssignValue(const bool value){m_sValue = value ? "true":"false";};	public:		xBool() {AssignValue(false);};		xBool(bool value) {AssignValue(value);};		bool value();		xBool operator=(const bool value) {AssignValue(value);return *this;};	};/**	serializable time_t*/		class __declspec(dllexport) xTime_t: public MemberBase	{	private:		void AssignValue(const time_t value);	public:		xTime_t() {AssignValue(0);};		xTime_t(time_t value) {AssignValue(value);};		time_t value();		xTime_t operator=(const time_t value) {AssignValue(value);return *this;};	};	/**	serializable OleDateTime*/	class __declspec(dllexport) xOleDateTime : public MemberBase	{	private:		void AssignValue(const COleDateTime value);	public:		xOleDateTime() { };		xOleDateTime(COleDateTime value) { AssignValue(value); };		COleDateTime value();		xOleDateTime operator=(const COleDateTime value) { AssignValue(value); return *this; };	};	class __declspec(dllexport) xOleDateTimeSpan : public MemberBase	{	private:		void AssignValue(const COleDateTimeSpan value);	public:		xOleDateTimeSpan() { };		xOleDateTimeSpan(COleDateTimeSpan value) { AssignValue(value); };		COleDateTimeSpan value();		xOleDateTimeSpan operator=(const COleDateTimeSpan value) { AssignValue(value); return *this; };	};	/**serializable Rect*/	class __declspec(dllexport) xRect : public MemberBase	{	private:		void AssignValue(const CRect value, int shape = 1);	public:		xRect() { AssignValue(0); };		xRect(CRect value,int shape) { AssignValue(value,shape); };		CRect value();		xRect operator=(const CRect value) { AssignValue(value); return *this; };	};	/**serializable Point*/	class __declspec(dllexport) xPoint : public MemberBase	{	private:		void AssignValue(const CPoint value);	public:		xPoint() { AssignValue(0); };		xPoint(CPoint value) { AssignValue(value); };		CPoint value();		xPoint operator=(const CPoint value) { AssignValue(value); return *this; };	};/**	Class-collection base*/		class CollectionBase;	typedef  std::map<std::string,CollectionBase*>::iterator  __declspec(dllexport)  CollectionIterator;	class __declspec(dllexport) CollectionBase	{		friend class ISlo;	private:		std::string m_sCollectionName;		std::string m_sCollectionClassType;	public:		CollectionBase() { m_vCollection.clear(); m_mOwner.clear(); };		std::vector<ISlo*> m_vCollection;		std::map<ISlo*, bool> m_mOwner;		void setCollectionName(std::string value) {m_sCollectionName=value;};		void setCollectionClassType(std::string value) {m_sCollectionClassType=value;};		virtual ISlo *newElement()=0;	public:		virtual ~CollectionBase() 		{ 			m_sCollectionName.clear(); m_sCollectionClassType.clear(); m_vCollection.clear(); m_mOwner.clear();		};		std::string getCollectionName() { return m_sCollectionName; };		size_t size() {return m_vCollection.size();};		ISlo *getItem(int itemID) { return m_vCollection.at(itemID);};		void Clear();	};/**	Class-collection template*/		template <typename T>	class __declspec(dllexport) Collection: public CollectionBase	{		//friend class Slo;	public:		~Collection() {Clear();	};		T*newElement();		void addItem(T*item) { m_vCollection.push_back(item);/* m_mOwner[item] = false;*/ };		T*getItem(int itemID) { return (T*)m_vCollection.at(itemID); };	};/**	create new element of type T	@return empty object of type T*/		template <typename T>	T* Collection<T>::newElement()	{ 		T* newItem = new T();		m_vCollection.push_back(newItem);		//I change this value to be false forever(gsp).No matter what case there's no need to set the object's owner to the collection		//after we created the object we'll put them to in a smartpointer.then the smartpointer will manage it.		m_mOwner[newItem]=false;//m_mOwner[newItem]=true 		return newItem;	};/**	Serializeable base class	derive your serializable class from Serializable*/		class __declspec(dllexport) ISlo	{	public:	virtual void Serialize(bool isStoring, tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode) = 0;	virtual ~ISlo() = default;	};	typedef std::vector<Slo*>::iterator  __declspec(dllexport) SerializableIterator;	class __declspec(dllexport) Slo:public ISlo	{		friend class CollectionBase;	private:		Slo(Slo const &s) { }		Slo operator=(Slo const &s) {return *this;};		static std::string strReplaceAll(std::string source, const std::string from, const std::string to);		void toXML(tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode);		void fromXML(tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode);	public:		std::string m_sXML;		std::string m_sClassName;		std::string m_sVersion;		std::map<std::string,MemberBase*> m_AttributeMappings;		std::map<std::string,ISlo*> m_MemberMappings;		std::map<std::string,CollectionBase*> m_MemberCollections;		void setClassName(std::string ClassName) { m_sClassName = ClassName; };		Slo();		virtual ~Slo();				public:		void Register(std::string MemberName, MemberBase *Member, std::string DefaultValue = "");		void Register(std::string MemberName, ISlo *Member);		void Register(std::string CollectionName, CollectionBase *SubclassCollection);		 virtual void Serialize(bool isStoring, tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode) 		 {				 if (isStoring)				 {					 toXML(classDoc, rootNode);				 }				 else				 {					 fromXML(classDoc, rootNode);				 }					 };		static std::string IdentifyClass(std::string XMLSource);		static std::string IdentifyClassVersion(std::string XMLSource);		std::string getClassName(){return m_sClassName;};		void setVersion(std::string value) {m_sVersion=value;};		std::string getVersion() {return m_sVersion;};			void Clear();	};	void __declspec(dllexport) SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c);	std::vector<std::string>  __declspec(dllexport) SplitString(const std::string& s, const std::string& c);	void __declspec(dllexport) ReplaceAll(std::string& content, std::string searchFor, std::string replaceWith);	void __declspec(dllexport) ReplaceFirst(std::string& content, std::string searchFor, std::string replaceWith);}
 |