XMLSerializer.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. // Forward declaration
  29. class XMLSerializer;
  30. /**
  31. * interface XMLSerializable
  32. * Inherit from this interface to have full control over the serialization of you user defined class.
  33. */
  34. class XMLSerializable
  35. {
  36. public:
  37. std::string Name;
  38. /**
  39. * This function gets called if the objects were not found during deserialization.
  40. * Initialize your objects as you like.
  41. */
  42. virtual void Init() = 0;
  43. /**
  44. * Serialize your stuff within this function.
  45. * It contains the current serialization xml file. You can use SaveToXMLDoc or SaveGroupToXMLElement to add your objects.
  46. */
  47. virtual bool Serialize(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element) = 0;
  48. /**
  49. * Deserialize your stuff within this function.
  50. * It contains the current serialization xml file. You can use LoadFromXMLDoc or LoadGroupFromXMLElement to read out your objects.
  51. */
  52. virtual bool Deserialize(tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element) = 0;
  53. };
  54. /**
  55. * class XMLSerialization
  56. * Inherit from this class to have the easiest way to serialize your user defined class.
  57. */
  58. class XMLSerialization : public XMLSerializable
  59. {
  60. public:
  61. igl::XMLSerializer* xmlSerializer;
  62. /**
  63. * Default implementation of XMLSerializable interface
  64. */
  65. virtual void Init();
  66. virtual bool Serialize(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element);
  67. virtual bool Deserialize(tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element);
  68. XMLSerialization(const std::string& name);
  69. ~XMLSerialization();
  70. /**
  71. * Following functions can be overwritten to handle the specific events.
  72. * Return false to prevent serialization of object.
  73. */
  74. virtual bool BeforeSerialization();
  75. virtual void AfterSerialization();
  76. virtual bool BeforeDeserialization();
  77. virtual void AfterDeserialization();
  78. };
  79. /**
  80. * class XMLSerializableObject
  81. * internal usage
  82. */
  83. class XMLSerializableObject : public XMLSerializable
  84. {
  85. public:
  86. XMLSerializableObject(const std::string& name, const std::string& group);
  87. virtual ~XMLSerializableObject();
  88. // set attribute conversion functions
  89. void SetAttribute(tinyxml2::XMLElement* element, const char* name, char& dest);
  90. void SetAttribute(tinyxml2::XMLElement* element, const char* name, char*& dest);
  91. void SetAttribute(tinyxml2::XMLElement* element, const char* name, std::string& dest);
  92. void SetAttribute(tinyxml2::XMLElement* element, const char* name, bool& dest);
  93. void SetAttribute(tinyxml2::XMLElement* element, const char* name, unsigned int& dest);
  94. void SetAttribute(tinyxml2::XMLElement* element, const char* name, int& dest);
  95. void SetAttribute(tinyxml2::XMLElement* element, const char* name, float& dest);
  96. void SetAttribute(tinyxml2::XMLElement* element, const char* name, double& dest);
  97. // get attribute conversion functions
  98. void GetAttribute(const char* src, char& dest);
  99. void GetAttribute(const char* src, char*& dest);
  100. void GetAttribute(const char* src, std::string& dest);
  101. void GetAttribute(const char* src, bool& dest);
  102. void GetAttribute(const char* src, unsigned int& dest);
  103. void GetAttribute(const char* src, int& dest);
  104. void GetAttribute(const char* src, float& dest);
  105. void GetAttribute(const char* src, double& dest);
  106. // initialize objects
  107. template<typename T>
  108. void Init(T& obj);
  109. template<typename T>
  110. void Init(T*& obj);
  111. template<typename T, int S>
  112. void Init(std::array<T,S>& obj);
  113. template<typename T0, typename T1>
  114. void Init(std::pair<T0,T1>& obj);
  115. template<typename T>
  116. void Init(std::vector<T>& obj);
  117. template<typename T, int R, int C>
  118. void Init(Eigen::Matrix<T,R,C>& obj);
  119. template<typename T>
  120. void Init(Eigen::SparseMatrix<T>& obj);
  121. // base types and XMLSerializable objects
  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 Serialize(T*& obj, tinyxml2::XMLDocument* doc, 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. template<typename T>
  129. bool Deserialize(T*& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  130. // std::array<T>
  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 Serialize(std::array<T,S>*& obj, tinyxml2::XMLDocument* doc, 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. template<typename T, int S>
  138. bool Deserialize(std::array<T,S>*& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  139. // std::pair<T0,T1>
  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 Serialize(std::pair<T0,T1>*& obj, tinyxml2::XMLDocument* doc, 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. template<typename T0, typename T1>
  147. bool Deserialize(std::pair<T0,T1>*& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  148. // std::vector<T>
  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 Serialize(std::vector<T>*& obj, tinyxml2::XMLDocument* doc, 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. template<typename T>
  156. bool Deserialize(std::vector<T>*& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  157. // Eigen dense matrix
  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 Serialize(Eigen::Matrix<T,R,C>*& obj, tinyxml2::XMLDocument* doc, 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. template<typename T, int R, int C>
  165. bool Deserialize(Eigen::Matrix<T,R,C>*& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  166. // Eigen sparse matrix
  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 Serialize(Eigen::SparseMatrix<T>*& obj, tinyxml2::XMLDocument* doc, 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. template<typename T>
  174. bool Deserialize(Eigen::SparseMatrix<T>*& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  175. private:
  176. template<typename T>
  177. bool setElementAttribute(T& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  178. template<typename T>
  179. bool getElementAttribute(T& obj, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name);
  180. };
  181. /**
  182. * class XMLSerializableInstance
  183. * internal usage
  184. */
  185. template<typename T>
  186. class XMLSerializableInstance : public XMLSerializableObject
  187. {
  188. public:
  189. T& Object;
  190. T DefaultValue;
  191. XMLSerializableInstance(T& obj, const std::string& name, const std::string group);
  192. XMLSerializableInstance(T& obj, const std::string& name, const std::string group, T defaultValue);
  193. ~XMLSerializableInstance();
  194. // XMLSerializable interface implementation
  195. void Init();
  196. bool Serialize(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element);
  197. bool Deserialize(tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element);
  198. };
  199. /**
  200. * struct XMLSerializerGroup
  201. * internal usage
  202. */
  203. struct XMLSerializerGroup
  204. {
  205. std::string Name;
  206. std::vector<XMLSerializable*>* Objects;
  207. };
  208. /**
  209. * class XMLSerializer
  210. * This is the core class which takes care of saving and loading of serialization of object structures.
  211. */
  212. class XMLSerializer
  213. {
  214. public:
  215. /**
  216. * Serializes an object to a file
  217. */
  218. template<typename T>
  219. static bool SaveObject(T& object, const char* filename);
  220. template<typename T>
  221. static bool SaveObject(T& object, const std::string& objectName, const std::string& groupName, const char* filename, bool overwrite);
  222. /**
  223. * Loads the serialization of an object from a file.
  224. */
  225. template<typename T>
  226. static bool LoadObject(T& object, const char* filename);
  227. template<typename T>
  228. static bool LoadObject(T& object, const std::string& objectName, const std::string& groupName, const char* filename);
  229. /**
  230. * Constructor which sets the default group
  231. */
  232. XMLSerializer(const std::string& defaultGroup);
  233. ~XMLSerializer();
  234. /**
  235. * Save the serialization of all groups to file.
  236. * Parameter overwrite specifies if file gets overwritten or updated
  237. */
  238. bool Save(const char* filename, bool overwrite);
  239. bool Save(const std::string& groupName, const char* filename, bool overwrite);
  240. bool Save(const std::string& objectName, const std::string& groupName, const char* filename, bool overwrite);
  241. /**
  242. * Save the serialization of all groups to a XMLDocument instance.
  243. */
  244. bool SaveToXMLDoc(tinyxml2::XMLDocument* doc);
  245. bool SaveToXMLDoc(const std::string& groupName, tinyxml2::XMLDocument* doc);
  246. bool SaveToXMLDoc(const std::string& objectName, const std::string& groupName, tinyxml2::XMLDocument* doc);
  247. /**
  248. * Save the serialization of a group with a new provided name to given XMLElement instance.
  249. */
  250. bool SaveGroupToXMLElement(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  251. bool SaveGroupToXMLElement(const std::string& groupName, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name);
  252. /**
  253. * Load the serialization from a file.
  254. */
  255. bool Load(const char* filename);
  256. bool Load(const std::string& groupName, const char* filename);
  257. bool Load(const std::string& objectName, const std::string& groupName, const char* filename);
  258. /**
  259. * Load the serialization from an XMLDocument instance.
  260. */
  261. bool LoadFromXMLDoc(tinyxml2::XMLDocument* doc);
  262. bool LoadFromXMLDoc(const std::string& groupName, tinyxml2::XMLDocument* doc);
  263. bool LoadFromXMLDoc(const std::string& objectName, const std::string& groupName, tinyxml2::XMLDocument* doc);
  264. /**
  265. * Load the serialization from a XMLElement instance to given group.
  266. */
  267. bool LoadGroupFromXMLElement(const std::string& groupName, tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element);
  268. /**
  269. * Set/Get current group. Every object which is added afterwards will be in this group, except it specifies another group.
  270. */
  271. void SetCurrentGroup(const std::string& group);
  272. std::string GetCurrentGroup();
  273. /**
  274. * 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.
  275. * 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*>>>.
  276. * Also pointers to those objects can be used (for instance like vector<vector<pair<int,float>*>*>).
  277. * 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.
  278. */
  279. template<typename T>
  280. bool Add(T& object, const std::string& name);
  281. template<typename T>
  282. bool Add(T& object, const std::string& name, T defaultValue);
  283. // stl containers
  284. template<typename T, int S>
  285. bool Add(std::array<T,S>& obj, const std::string& name);
  286. template<typename T0, typename T1>
  287. bool Add(std::pair<T0,T1>& obj, const std::string& name);
  288. template<typename T>
  289. bool Add(std::vector<T>& obj, const std::string& name);
  290. template<typename T, int R, int C>
  291. // eigen matrices
  292. bool Add(Eigen::Matrix<T,R,C>& obj, const std::string& name);
  293. template<typename T>
  294. bool Add(Eigen::SparseMatrix<T>& obj, const std::string& name);
  295. private:
  296. std::map<std::string,XMLSerializerGroup*>::iterator currentGroup;
  297. std::map<std::string,XMLSerializerGroup*> groups;
  298. template<typename T>
  299. bool add(T& object, const std::string& name);
  300. template<typename T>
  301. bool add(T& object, const std::string& name, T defaultValue);
  302. bool addObjectToGroup(XMLSerializable* object, const std::string& group);
  303. bool addObjectToGroup(XMLSerializable* object, std::map<std::string,XMLSerializerGroup*>::iterator it);
  304. std::map<std::string,XMLSerializerGroup*>::iterator setGetGroup(const std::string& group);
  305. tinyxml2::XMLDocument* openDoc(const char* filename);
  306. tinyxml2::XMLElement* findAddGroup(tinyxml2::XMLDocument* doc, const char* groupName);
  307. };
  308. /**
  309. * class XMLSerializerTest
  310. * Used to test the functionality of the library and also shows howto use it.
  311. */
  312. class XMLSerializerTest : public igl::XMLSerialization
  313. {
  314. public:
  315. int testInt;
  316. std::vector<float> testVector;
  317. XMLSerializerTest();
  318. bool Test();
  319. };
  320. }
  321. #ifdef IGL_HEADER_ONLY
  322. #include "XMLSerializer.cpp"
  323. #endif
  324. #endif