XMLSerialization.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * XML Serialization
  3. * Simple and lightweight xml serialization class
  4. *
  5. * Original code by Lothar Perr
  6. *
  7. * This software is provided 'as-is', without any express or implied
  8. * warranty. In no event will the authors be held liable for any
  9. * damages arising from the use of this software.
  10. *
  11. * Permission is granted to anyone to use this software for any
  12. * purpose, including commercial applications, and to alter it and
  13. * redistribute it freely
  14. */
  15. #pragma once
  16. #include "stdafx.h"
  17. #include <string>
  18. #include <vector>
  19. #include <map>
  20. #include <set>
  21. #include "tinyxml2.h"
  22. /**
  23. XML Serialization
  24. */
  25. namespace xmls
  26. {
  27. //using namespace std;
  28. #define RootClassName "XMLData"
  29. #define XMLClassEndTag std::string("</")+RootClassName+">"
  30. #define Empty_String std::string("")
  31. class Slo;
  32. /**
  33. serializable members base class
  34. */
  35. class __declspec(dllexport) MemberBase
  36. {
  37. protected:
  38. std::string m_sValue;
  39. public:
  40. virtual ~MemberBase() {};
  41. std::string toString(){return m_sValue;};
  42. const char* c_str() {return m_sValue.c_str();};
  43. std::string *getStringPtr() {return &m_sValue;};
  44. };
  45. /**
  46. serializable string
  47. */
  48. class __declspec(dllexport) xString: public MemberBase
  49. {
  50. private:
  51. void AssignValue(const std::string value){m_sValue=value;};
  52. public:
  53. xString(){};
  54. xString(std::string value) {AssignValue(value);};
  55. std::string value(){return m_sValue;};
  56. xString operator=(const std::string value) {AssignValue(value);return *this;};
  57. xString operator=(const char* value) {AssignValue(value);return *this;};
  58. };
  59. /**
  60. serializable int
  61. */
  62. class __declspec(dllexport) xInt: public MemberBase
  63. {
  64. private:
  65. void AssignValue(const int value);
  66. public:
  67. xInt() {AssignValue(0);};
  68. xInt(int value) {AssignValue(value);};
  69. int value();
  70. xInt operator=(const int value) {AssignValue(value);return *this;};
  71. };
  72. class __declspec(dllexport) xDouble : public MemberBase
  73. {
  74. private:
  75. void AssignValue(const double value);
  76. public:
  77. xDouble() { AssignValue(0); };
  78. xDouble(double value) { AssignValue(value); };
  79. double value();
  80. xDouble operator=(const double value) { AssignValue(value); return *this; };
  81. };
  82. class __declspec(dllexport) xLong : public MemberBase
  83. {
  84. private:
  85. void AssignValue(const long value);
  86. public:
  87. xLong() { AssignValue(0); };
  88. xLong(long value) { AssignValue(value); };
  89. long value();
  90. xLong operator=(const long value) { AssignValue(value); return *this; };
  91. };
  92. class __declspec(dllexport) xDWORD : public MemberBase
  93. {
  94. private:
  95. void AssignValue(const DWORD value);
  96. public:
  97. xDWORD() { AssignValue(0); };
  98. xDWORD(DWORD value) { AssignValue(value); };
  99. DWORD value();
  100. xDWORD operator=(const DWORD value) { AssignValue(value); return *this; };
  101. };
  102. /**
  103. serializable bool
  104. */
  105. class __declspec(dllexport) xBool: public MemberBase
  106. {
  107. private:
  108. void AssignValue(const bool value){m_sValue = value ? "true":"false";};
  109. public:
  110. xBool() {AssignValue(false);};
  111. xBool(bool value) {AssignValue(value);};
  112. bool value();
  113. xBool operator=(const bool value) {AssignValue(value);return *this;};
  114. };
  115. /**
  116. serializable time_t
  117. */
  118. class __declspec(dllexport) xTime_t: public MemberBase
  119. {
  120. private:
  121. void AssignValue(const time_t value);
  122. public:
  123. xTime_t() {AssignValue(0);};
  124. xTime_t(time_t value) {AssignValue(value);};
  125. time_t value();
  126. xTime_t operator=(const time_t value) {AssignValue(value);return *this;};
  127. };
  128. /**
  129. serializable OleDateTime
  130. */
  131. class __declspec(dllexport) xOleDateTime : public MemberBase
  132. {
  133. private:
  134. void AssignValue(const COleDateTime value);
  135. public:
  136. xOleDateTime() { };
  137. xOleDateTime(COleDateTime value) { AssignValue(value); };
  138. COleDateTime value();
  139. xOleDateTime operator=(const COleDateTime value) { AssignValue(value); return *this; };
  140. };
  141. class __declspec(dllexport) xOleDateTimeSpan : public MemberBase
  142. {
  143. private:
  144. void AssignValue(const COleDateTimeSpan value);
  145. public:
  146. xOleDateTimeSpan() { };
  147. xOleDateTimeSpan(COleDateTimeSpan value) { AssignValue(value); };
  148. COleDateTimeSpan value();
  149. xOleDateTimeSpan operator=(const COleDateTimeSpan value) { AssignValue(value); return *this; };
  150. };
  151. /**
  152. serializable Rect
  153. */
  154. class __declspec(dllexport) xRect : public MemberBase
  155. {
  156. private:
  157. void AssignValue(const CRect value, int shape = 1);
  158. public:
  159. xRect() { AssignValue(0); };
  160. xRect(CRect value,int shape) { AssignValue(value,shape); };
  161. CRect value();
  162. xRect operator=(const CRect value) { AssignValue(value); return *this; };
  163. };
  164. /**
  165. serializable Point
  166. */
  167. class __declspec(dllexport) xPoint : public MemberBase
  168. {
  169. private:
  170. void AssignValue(const CPoint value);
  171. public:
  172. xPoint() { AssignValue(0); };
  173. xPoint(CPoint value) { AssignValue(value); };
  174. CPoint value();
  175. xPoint operator=(const CPoint value) { AssignValue(value); return *this; };
  176. };
  177. /**
  178. Class-collection base
  179. */
  180. class CollectionBase;
  181. typedef std::map<std::string,CollectionBase*>::iterator __declspec(dllexport) CollectionIterator;
  182. class __declspec(dllexport) CollectionBase
  183. {
  184. friend class ISlo;
  185. private:
  186. std::string m_sCollectionName;
  187. std::string m_sCollectionClassType;
  188. public:
  189. CollectionBase() { m_vCollection.clear(); m_mOwner.clear(); };
  190. std::vector<ISlo*> m_vCollection;
  191. std::map<ISlo*, bool> m_mOwner;
  192. void setCollectionName(std::string value) {m_sCollectionName=value;};
  193. void setCollectionClassType(std::string value) {m_sCollectionClassType=value;};
  194. virtual ISlo *newElement()=0;
  195. public:
  196. virtual ~CollectionBase()
  197. {
  198. m_sCollectionName.clear(); m_sCollectionClassType.clear(); m_vCollection.clear(); m_mOwner.clear();
  199. };
  200. std::string getCollectionName() { return m_sCollectionName; };
  201. size_t size() {return m_vCollection.size();};
  202. ISlo *getItem(int itemID) { return m_vCollection.at(itemID);};
  203. void Clear();
  204. };
  205. /**
  206. Class-collection template
  207. */
  208. template <typename T>
  209. class __declspec(dllexport) Collection: public CollectionBase
  210. {
  211. //friend class Slo;
  212. public:
  213. ~Collection() {Clear(); };
  214. T*newElement();
  215. void addItem(T*item) { m_vCollection.push_back(item);/* m_mOwner[item] = false;*/ };
  216. T*getItem(int itemID) { return (T*)m_vCollection.at(itemID); };
  217. };
  218. /**
  219. create new element of type T
  220. @return empty object of type T
  221. */
  222. template <typename T>
  223. T* Collection<T>::newElement()
  224. {
  225. T* newItem = new T();
  226. m_vCollection.push_back(newItem);
  227. //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
  228. //after we created the object we'll put them to in a smartpointer.then the smartpointer will manage it.
  229. m_mOwner[newItem]=false;//m_mOwner[newItem]=true
  230. return newItem;
  231. };
  232. /**
  233. Serializeable base class
  234. derive your serializable class from Serializable
  235. */
  236. class __declspec(dllexport) ISlo
  237. {
  238. public:
  239. virtual void Serialize(bool isStoring, tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode) = 0;
  240. virtual ~ISlo() = default;
  241. };
  242. typedef std::vector<Slo*>::iterator __declspec(dllexport) SerializableIterator;
  243. class __declspec(dllexport) Slo:public ISlo
  244. {
  245. friend class CollectionBase;
  246. private:
  247. Slo(Slo const &s) { }
  248. Slo operator=(Slo const &s) {return *this;};
  249. static std::string strReplaceAll(std::string source, const std::string from, const std::string to);
  250. void toXML(tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode);
  251. void fromXML(tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode);
  252. public:
  253. std::string m_sXML;
  254. std::string m_sClassName;
  255. std::string m_sVersion;
  256. std::map<std::string,MemberBase*> m_AttributeMappings;
  257. std::map<std::string,ISlo*> m_MemberMappings;
  258. std::map<std::string,CollectionBase*> m_MemberCollections;
  259. void setClassName(std::string ClassName) { m_sClassName = ClassName; };
  260. Slo();
  261. virtual ~Slo();
  262. public:
  263. void Register(std::string MemberName, MemberBase *Member, std::string DefaultValue = "");
  264. void Register(std::string MemberName, ISlo *Member);
  265. void Register(std::string CollectionName, CollectionBase *SubclassCollection);
  266. virtual void Serialize(bool isStoring, tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode)
  267. {
  268. if (isStoring)
  269. {
  270. toXML(classDoc, rootNode);
  271. }
  272. else
  273. {
  274. fromXML(classDoc, rootNode);
  275. }
  276. };
  277. static std::string IdentifyClass(std::string XMLSource);
  278. static std::string IdentifyClassVersion(std::string XMLSource);
  279. std::string getClassName(){return m_sClassName;};
  280. void setVersion(std::string value) {m_sVersion=value;};
  281. std::string getVersion() {return m_sVersion;};
  282. void Clear();
  283. };
  284. void __declspec(dllexport) SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c);
  285. std::vector<std::string> __declspec(dllexport) SplitString(const std::string& s, const std::string& c);
  286. void __declspec(dllexport) ReplaceAll(std::string& content, std::string searchFor, std::string replaceWith);
  287. void __declspec(dllexport) ReplaceFirst(std::string& content, std::string searchFor, std::string replaceWith);
  288. }