XMLSerializer.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* ---------------------------------------------------------------------------
  2. // XMLSerializer.h
  3. // Author: Christian Schüller on 08/05/13.
  4. ------------------------------------------------------------------------------
  5. This class allows to save and load a serialization of basic c++ data types like
  6. char, char*, std::string, bool, uint, int, float, double to and from a xml file.
  7. Containers like std::vector, std::std::pair, Eigen dense and sparse matrices are supported
  8. as well as combination of them (like vector<pair<string,bool>> or vector<vector<int>>).
  9. To serialize an arbitary object use the XMLSerializable interface.
  10. The serialized objects are organised in groups in the xml file and have
  11. their own names which must be unique within one group.
  12. You can find examples how to use it in the test case class XMLSerializerTest.
  13. ----------------------------------------------------------------------------*/
  14. #ifndef XML_SERIALIZER_H
  15. #define XML_SERIALIZER_H
  16. #include <iostream>
  17. #include <array>
  18. #include <vector>
  19. #include <map>
  20. #include <Eigen/Dense>
  21. #include <Eigen/Sparse>
  22. #include "tinyxml2.h"
  23. namespace igl
  24. {
  25. void EncodeXMLElementName(std::string& name);
  26. //void DecodeXMLElementName(std::string& name);
  27. void ReplaceSubString(std::string& str, const std::string& search, const std::string& replace);
  28. /**
  29. * interface XMLSerializable
  30. * Inherit from this interface to have full control over the serialization of you user defined class.
  31. */
  32. class XMLSerializable
  33. {
  34. public:
  35. std::string Name;
  36. /**
  37. * This function gets called if the objects were not found during deserialization.
  38. * Initialize your objects as you like.
  39. */
  40. virtual void Init() = 0;
  41. /**
  42. * Serialize your stuff within this function.
  43. * It contains the current serialization xml file. You can use SaveToXMLDoc or SaveGroupToXMLElement to add your objects.
  44. */
  45. virtual bool Serialize(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element) = 0;
  46. /**
  47. * Deserialize your stuff within this function.
  48. * It contains the current serialization xml file. You can use LoadFromXMLDoc or LoadGroupFromXMLElement to read out your objects.
  49. */
  50. virtual bool Deserialize(tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element) = 0;
  51. };
  52. /**
  53. * class XMLSerialization
  54. * Inherit from this class to have the easiest way to serialize your user defined class.
  55. */
  56. class XMLSerialization : public XMLSerializable
  57. {
  58. public:
  59. igl::XMLSerializer* xmlSerializer;
  60. /**
  61. * Default implementation of XMLSerializable interface
  62. */
  63. virtual void Init();
  64. virtual bool Serialize(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element);
  65. virtual bool Deserialize(tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element);
  66. XMLSerialization(const std::string& name);
  67. ~XMLSerialization();
  68. /**
  69. * Following functions can be overwritten to handle the specific events.
  70. * Return false to prevent serialization of object.
  71. */
  72. virtual bool BeforeSerialization();
  73. virtual void AfterSerialization();
  74. virtual bool BeforeDeserialization();
  75. virtual void AfterDeserialization();
  76. };
  77. /**
  78. * class XMLSerializableObject
  79. * internal usage
  80. */
  81. class XMLSerializableObject : public XMLSerializable
  82. {
  83. public:
  84. XMLSerializableObject(const std::string& name, const std::string& group);
  85. virtual ~XMLSerializableObject();
  86. // set attribute conversion functions
  87. void SetAttribute(tinyxml2::XMLElement* element, const char* name, char& dest);
  88. void SetAttribute(tinyxml2::XMLElement* element, const char* name, char*& dest);
  89. void SetAttribute(tinyxml2::XMLElement* element, const char* name, std::string& dest);
  90. void SetAttribute(tinyxml2::XMLElement* element, const char* name, bool& dest);
  91. void SetAttribute(tinyxml2::XMLElement* element, const char* name, unsigned int& dest);
  92. void SetAttribute(tinyxml2::XMLElement* element, const char* name, int& dest);
  93. void SetAttribute(tinyxml2::XMLElement* element, const char* name, float& dest);
  94. void SetAttribute(tinyxml2::XMLElement* element, const char* name, double& dest);
  95. // get attribute conversion functions
  96. void GetAttribute(const char* src, char& dest);
  97. void GetAttribute(const char* src, char*& dest);
  98. void GetAttribute(const char* src, std::string& dest);
  99. void GetAttribute(const char* src, bool& dest);
  100. void GetAttribute(const char* src, unsigned int& dest);
  101. void GetAttribute(const char* src, int& dest);
  102. void GetAttribute(const char* src, float& dest);
  103. void GetAttribute(const char* src, double& dest);
  104. // initialize objects
  105. template<typename T>
  106. void Init(T& obj);
  107. template<typename T>
  108. void Init(T*& obj);
  109. template<typename T, int S>
  110. void Init(std::array<T,S>& obj);
  111. template<typename T0, typename T1>
  112. void Init(std::pair<T0,T1>& obj);
  113. template<typename T>
  114. void Init(std::vector<T>& obj);
  115. template<typename T, int R, int C>
  116. void Init(Eigen::Matrix<T,R,C>& obj);
  117. template<typename T>
  118. void Init(Eigen::SparseMatrix<T>& obj);
  119. // base types and XMLSerializable objects
  120. template<typename T>
  121. bool Serialize(T& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  122. template<typename T>
  123. bool Serialize(T*& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  124. template<typename T>
  125. bool Deserialize(T& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  126. template<typename T>
  127. bool Deserialize(T*& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  128. // std::array<T>
  129. template<typename T, int S>
  130. bool Serialize(std::array<T,S>& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  131. template<typename T, int S>
  132. bool Serialize(std::array<T,S>*& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  133. template<typename T, int S>
  134. bool Deserialize(std::array<T,S>& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  135. template<typename T, int S>
  136. bool Deserialize(std::array<T,S>*& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  137. // std::pair<T0,T1>
  138. template<typename T0, typename T1>
  139. bool Serialize(std::pair<T0,T1>& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  140. template<typename T0, typename T1>
  141. bool Serialize(std::pair<T0,T1>*& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  142. template<typename T0, typename T1>
  143. bool Deserialize(std::pair<T0,T1>& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  144. template<typename T0, typename T1>
  145. bool Deserialize(std::pair<T0,T1>*& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  146. // std::vector<T>
  147. template<typename T>
  148. bool Serialize(std::vector<T>& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  149. template<typename T>
  150. bool Serialize(std::vector<T>*& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  151. template<typename T>
  152. bool Deserialize(std::vector<T>& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  153. template<typename T>
  154. bool Deserialize(std::vector<T>*& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  155. // Eigen dense matrix
  156. template<typename T, int R, int C>
  157. bool Serialize(Eigen::Matrix<T,R,C>& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  158. template<typename T, int R, int C>
  159. bool Serialize(Eigen::Matrix<T,R,C>*& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  160. template<typename T, int R, int C>
  161. bool Deserialize(Eigen::Matrix<T,R,C>& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  162. template<typename T, int R, int C>
  163. bool Deserialize(Eigen::Matrix<T,R,C>*& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  164. // Eigen sparse matrix
  165. template<typename T>
  166. bool Serialize(Eigen::SparseMatrix<T>& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  167. template<typename T>
  168. bool Serialize(Eigen::SparseMatrix<T>*& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  169. template<typename T>
  170. bool Deserialize(Eigen::SparseMatrix<T>& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  171. template<typename T>
  172. bool Deserialize(Eigen::SparseMatrix<T>*& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  173. private:
  174. template<typename T>
  175. bool setElementAttribute(T& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  176. template<typename T>
  177. bool getElementAttribute(T& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  178. };
  179. /**
  180. * class XMLSerializableInstance
  181. * internal usage
  182. */
  183. template<typename T>
  184. class XMLSerializableInstance : public XMLSerializableObject
  185. {
  186. public:
  187. T& Object;
  188. T DefaultValue;
  189. XMLSerializableInstance(T& obj, const std::string& name, const std::string group);
  190. XMLSerializableInstance(T& obj, const std::string& name, const std::string group, T defaultValue);
  191. ~XMLSerializableInstance();
  192. // XMLSerializable interface implementation
  193. void Init();
  194. bool Serialize(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element);
  195. bool Deserialize(tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element);
  196. };
  197. /**
  198. * struct XMLSerializerGroup
  199. * internal usage
  200. */
  201. struct XMLSerializerGroup
  202. {
  203. std::string Name;
  204. std::vector<XMLSerializable*>* Objects;
  205. };
  206. /**
  207. * class XMLSerializer
  208. * This is the core class which takes care of saving and loading of serialization of object structures.
  209. */
  210. class XMLSerializer
  211. {
  212. public:
  213. /**
  214. * Serializes an object to a file
  215. */
  216. template<typename T>
  217. static bool SaveObject(T& object, const char* filename);
  218. template<typename T>
  219. static bool SaveObject(T& object, const std::string& objectName, const std::string& groupName, const char* filename, bool overwrite);
  220. /**
  221. * Loads the serialization of an object from a file.
  222. */
  223. template<typename T>
  224. static bool LoadObject(T& object, const char* filename);
  225. template<typename T>
  226. static bool LoadObject(T& object, const std::string& objectName, const std::string& groupName, const char* filename);
  227. /**
  228. * Constructor which sets the default group
  229. */
  230. XMLSerializer(const std::string& defaultGroup);
  231. ~XMLSerializer();
  232. /**
  233. * Save the serialization of all groups to file.
  234. * Parameter overwrite specifies if file gets overwritten or updated
  235. */
  236. bool Save(const char* filename, bool overwrite);
  237. bool Save(const std::string& groupName, const char* filename, bool overwrite);
  238. bool Save(const std::string& objectName, const std::string& groupName, const char* filename, bool overwrite);
  239. /**
  240. * Save the serialization of all groups to a XMLDocument instance.
  241. */
  242. bool SaveToXMLDoc(tinyxml2::XMLDocument* doc);
  243. bool SaveToXMLDoc(const std::string& groupName, tinyxml2::XMLDocument* doc);
  244. bool SaveToXMLDoc(const std::string& objectName, const std::string& groupName, tinyxml2::XMLDocument* doc);
  245. /**
  246. * Save the serialization of a group with a new provided name to given XMLElement instance.
  247. */
  248. bool SaveGroupToXMLElement(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  249. bool SaveGroupToXMLElement(const std::string& groupName, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  250. /**
  251. * Load the serialization from a file.
  252. */
  253. bool Load(const char* filename);
  254. bool Load(const std::string& groupName, const char* filename);
  255. bool Load(const std::string& objectName, const std::string& groupName, const char* filename);
  256. /**
  257. * Load the serialization from an XMLDocument instance.
  258. */
  259. bool LoadFromXMLDoc(tinyxml2::XMLDocument* doc);
  260. bool LoadFromXMLDoc(const std::string& groupName, tinyxml2::XMLDocument* doc);
  261. bool LoadFromXMLDoc(const std::string& objectName, const std::string& groupName, tinyxml2::XMLDocument* doc);
  262. /**
  263. * Load the serialization from a XMLElement instance to given group.
  264. */
  265. bool LoadGroupFromXMLElement(const std::string& groupName, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element);
  266. /**
  267. * Set/Get current group. Every object which is added afterwards will be in this group, except it specifies another group.
  268. */
  269. void SetCurrentGroup(const std::string& group);
  270. std::string GetCurrentGroup();
  271. /**
  272. * Add an object to the serializer. Can be simple types like char, char*, string, unsigned int, int, float, double or containers like std::array, std::pair, std::vector.
  273. * Also Eigen dense or sparse matrices are supported and all objects of type Serializable* and combinations of thoses types like vector<vector>, vector<pair> or even vector<pair<vector,Serializable*>>>.
  274. * Also pointers to those objects can be used (for instance like vector<vector<pair<int,float>*>*>).
  275. * char* is also possible as base type and represents a array of chars, but be carefull that the pointer is not just a copy but a valid instance in the current programm scope.
  276. */
  277. template<typename T>
  278. bool Add(T& object, const std::string& name);
  279. template<typename T>
  280. bool Add(T& object, const std::string& name, T defaultValue);
  281. // stl containers
  282. template<typename T, int S>
  283. bool Add(std::array<T,S>& obj, const std::string& name);
  284. template<typename T0, typename T1>
  285. bool Add(std::pair<T0,T1>& obj, const std::string& name);
  286. template<typename T>
  287. bool Add(std::vector<T>& obj, const std::string& name);
  288. template<typename T, int R, int C>
  289. // eigen matrices
  290. bool Add(Eigen::Matrix<T,R,C>& obj, const std::string& name);
  291. template<typename T>
  292. bool Add(Eigen::SparseMatrix<T>& obj, const std::string& name);
  293. private:
  294. std::map<std::string,XMLSerializerGroup*>::iterator currentGroup;
  295. std::map<std::string,XMLSerializerGroup*> groups;
  296. template<typename T>
  297. bool add(T& object, const std::string& name);
  298. template<typename T>
  299. bool add(T& object, const std::string& name, T defaultValue);
  300. bool addObjectToGroup(XMLSerializable* object, const std::string& group);
  301. bool addObjectToGroup(XMLSerializable* object, std::map<std::string,XMLSerializerGroup*>::iterator it);
  302. std::map<std::string,XMLSerializerGroup*>::iterator setGetGroup(const std::string& group);
  303. tinyxml2::XMLDocument* openDoc(const char* filename);
  304. tinyxml2::XMLElement* findAddGroup(tinyxml2::XMLDocument* doc, const char* groupName);
  305. };
  306. /**
  307. * class XMLSerializerTest
  308. * Used to test the functionality of the library and also shows howto use it.
  309. */
  310. class XMLSerializerTest : public igl::XMLSerialization
  311. {
  312. public:
  313. int testInt;
  314. std::vector<float> testVector;
  315. XMLSerializerTest();
  316. bool Test();
  317. };
  318. }
  319. #ifdef IGL_HEADER_ONLY
  320. #include "XMLSerializer.cpp"
  321. #endif
  322. #endif