serialize_xml.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // Copyright (C) 2014 Christian Sch�ller <schuellchr@gmail.com>
  3. //
  4. // This Source Code Form is subject to the terms of the Mozilla Public License
  5. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  6. // obtain one at http://mozilla.org/MPL/2.0/.
  7. // -----------------------------------------------------------------------------
  8. // Functions to save and load a serialization of fundamental c++ data types to
  9. // and from a xml file. STL containers, Eigen matrix types and nested data
  10. // structures are also supported. To serialize a user defined class implement
  11. // the interface XMLSerializable or XMLSerializableBase.
  12. //
  13. // See also: serialize.h
  14. // -----------------------------------------------------------------------------
  15. #ifndef IGL_SERIALIZABLE_XML_H
  16. #define IGL_SERIALIZABLE_XML_H
  17. #include "../igl_inline.h"
  18. #include <type_traits>
  19. #include <iostream>
  20. #include <vector>
  21. #include <set>
  22. #include <map>
  23. #include <memory>
  24. #include <Eigen/Dense>
  25. #include <Eigen/Sparse>
  26. #include <igl/serialize.h>
  27. #include "tinyxml2.h"
  28. //#define SERIALIZE_XML(x) igl::serialize_xml(x,#x,doc,element);
  29. //#define DESERIALIZE_XML(x) igl::deserialize_xml(x,#x,,doc,element);
  30. namespace igl
  31. {
  32. // serializes the given object either to a xml file or to the provided doc data
  33. //
  34. // Templates:
  35. // T type of the object to serialize
  36. // Inputs:
  37. // obj object to serialize
  38. // objectName unique object name,used for the identification
  39. // filename name of the file containing the serialization
  40. // binary set to true to serialize the object in binary format (faster for big data)
  41. // overwrite set to true to overwrite an existing xml file
  42. // element tinyxml2 virtual representation of the current xml node
  43. // Outputs:
  44. // doc contains current tinyxml2 virtual representation of the xml data
  45. //
  46. template <typename T>
  47. IGL_INLINE void serialize_xml(const T& obj,const std::string& filename);
  48. template <typename T>
  49. IGL_INLINE void serialize_xml(const T& obj,const std::string& objectName,const std::string& filename, bool binary = false,bool overwrite = false);
  50. template <typename T>
  51. IGL_INLINE void serialize_xml(const T& obj,const std::string& objectName,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,bool binary = false);
  52. // deserializes the given data from a xml file or doc data back to the provided object
  53. //
  54. // Templates:
  55. // T type of the object to serialize
  56. // Inputs:
  57. //
  58. // objectName unique object name,used for the identification
  59. // filename name of the file containing the serialization
  60. // binary set to true to serialize the object in binary format (faster for big data)
  61. // overwrite set to true to overwrite an existing xml file
  62. // doc contains current tinyxml2 virtual representation of the xml data
  63. // element tinyxml2 virtual representation of the current xml node
  64. // Outputs:
  65. // obj object to load back serialization to
  66. //
  67. template <typename T>
  68. IGL_INLINE void deserialize_xml(T& obj,const std::string& filename);
  69. template <typename T>
  70. IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const std::string& filename);
  71. template <typename T>
  72. IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element);
  73. // interface for user defined types
  74. struct XMLSerializableBase : public SerializableBase
  75. {
  76. virtual void Serialize(std::vector<char>& buffer) const = 0;
  77. virtual void Deserialize(const std::vector<char>& buffer) = 0;
  78. virtual void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const = 0;
  79. virtual void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element) = 0;
  80. };
  81. // Convenient interface for user defined types
  82. class XMLSerializable: public XMLSerializableBase
  83. {
  84. private:
  85. template <typename T>
  86. struct XMLSerializationObject: public XMLSerializableBase
  87. {
  88. bool Binary;
  89. std::string Name;
  90. T* Object;
  91. void Serialize(std::vector<char>& buffer) const {
  92. igl::serialize(*Object,Name,buffer);
  93. }
  94. void Deserialize(const std::vector<char>& buffer) {
  95. igl::deserialize(*Object,Name,buffer);
  96. }
  97. void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const {
  98. igl::serialize_xml(*Object,Name,doc,element,Binary);
  99. }
  100. void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element) {
  101. igl::deserialize_xml(*Object,Name,doc,element);
  102. }
  103. };
  104. mutable bool initialized;
  105. mutable std::vector<XMLSerializableBase*> objects;
  106. public:
  107. // Override this function to add your member variables which should be serialized
  108. IGL_INLINE virtual void InitSerialization() = 0;
  109. // Following functions can be overridden to handle the specific events.
  110. // Return false to prevent the de-/serialization of an object.
  111. IGL_INLINE virtual bool PreSerialization() const;
  112. IGL_INLINE virtual void PostSerialization() const;
  113. IGL_INLINE virtual bool PreDeserialization();
  114. IGL_INLINE virtual void PostDeserialization();
  115. // Default implementation of XMLSerializableBase interface
  116. IGL_INLINE void Serialize(std::vector<char>& buffer) const;
  117. IGL_INLINE void Deserialize(const std::vector<char>& buffer);
  118. IGL_INLINE void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const;
  119. IGL_INLINE void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element);
  120. // Default constructor, destructor, assignment and copy constructor
  121. IGL_INLINE XMLSerializable();
  122. IGL_INLINE XMLSerializable(const XMLSerializable& obj);
  123. IGL_INLINE ~XMLSerializable();
  124. IGL_INLINE XMLSerializable& operator=(const XMLSerializable& obj);
  125. // Use this function to add your variables which should be serialized
  126. template <typename T>
  127. IGL_INLINE void Add(T& obj,std::string name,bool binary = false);
  128. };
  129. // internal functions
  130. namespace serialization_xml
  131. {
  132. // fundamental types
  133. template <typename T>
  134. IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  135. template <typename T>
  136. IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  137. // std::string
  138. IGL_INLINE void serialize(const std::string& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  139. IGL_INLINE void deserialize(std::string& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  140. // XMLSerializableBase
  141. template <typename T>
  142. IGL_INLINE typename std::enable_if<std::is_base_of<XMLSerializableBase,T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  143. template <typename T>
  144. IGL_INLINE typename std::enable_if<std::is_base_of<XMLSerializableBase,T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  145. // STL containers
  146. template <typename T1, typename T2>
  147. IGL_INLINE void serialize(const std::pair<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  148. template <typename T1,typename T2>
  149. IGL_INLINE void deserialize(std::pair<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  150. template <typename T1,typename T2>
  151. IGL_INLINE void serialize(const std::vector<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  152. template <typename T1,typename T2>
  153. IGL_INLINE void deserialize(std::vector<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  154. template <typename T>
  155. IGL_INLINE void serialize(const std::set<T>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  156. template <typename T>
  157. IGL_INLINE void deserialize(std::set<T>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  158. template <typename T1,typename T2>
  159. IGL_INLINE void serialize(const std::map<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  160. template <typename T1,typename T2>
  161. IGL_INLINE void deserialize(std::map<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  162. // Eigen types
  163. template<typename T,int R,int C,int P,int MR,int MC>
  164. IGL_INLINE void serialize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  165. template<typename T,int R,int C,int P,int MR,int MC>
  166. IGL_INLINE void deserialize(Eigen::Matrix<T,R,C,P,MR,MC>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  167. template<typename T,int P,typename I>
  168. IGL_INLINE void serialize(const Eigen::SparseMatrix<T,P,I>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  169. template<typename T,int P,typename I>
  170. IGL_INLINE void deserialize(Eigen::SparseMatrix<T,P,I>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  171. // raw pointers
  172. template <typename T>
  173. IGL_INLINE typename std::enable_if<std::is_pointer<T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  174. template <typename T>
  175. IGL_INLINE typename std::enable_if<std::is_pointer<T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  176. // helper functions
  177. tinyxml2::XMLElement* getElement(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  178. IGL_INLINE void getAttribute(const char* src,bool& dest);
  179. IGL_INLINE void getAttribute(const char* scr,char& dest);
  180. IGL_INLINE void getAttribute(const char* src,std::string& dest);
  181. IGL_INLINE void getAttribute(const char* src,float& dest);
  182. IGL_INLINE void getAttribute(const char* src,double& dest);
  183. template<typename T>
  184. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest);
  185. template<typename T>
  186. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && !std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest);
  187. IGL_INLINE void replaceSubString(std::string& str,const std::string& search,const std::string& replace);
  188. IGL_INLINE void encodeXMLElementName(std::string& name);
  189. IGL_INLINE void decodeXMLElementName(std::string& name);
  190. IGL_INLINE std::string base64_encode(unsigned char const* bytes_to_encode,unsigned int in_len);
  191. IGL_INLINE std::string base64_decode(std::string const& encoded_string);
  192. // compile time type serializable check
  193. template <typename T>
  194. struct is_stl_container { static const bool value = false; };
  195. template <typename T1,typename T2>
  196. struct is_stl_container<std::pair<T1,T2> > { static const bool value = true; };
  197. template <typename T1,typename T2>
  198. struct is_stl_container<std::vector<T1,T2> > { static const bool value = true; };
  199. template <typename T>
  200. struct is_stl_container<std::set<T> > { static const bool value = true; };
  201. template <typename T1,typename T2>
  202. struct is_stl_container<std::map<T1,T2> > { static const bool value = true; };
  203. template <typename T>
  204. struct is_eigen_type { static const bool value = false; };
  205. template <typename T,int R,int C,int P,int MR,int MC>
  206. struct is_eigen_type<Eigen::Matrix<T,R,C,P,MR,MC> > { static const bool value = true; };
  207. template <typename T,int P,typename I>
  208. struct is_eigen_type<Eigen::SparseMatrix<T,P,I> > { static const bool value = true; };
  209. template <typename T>
  210. struct is_serializable {
  211. using T0 = typename std::remove_pointer<T>::type;
  212. static const bool value = std::is_fundamental<T0>::value || std::is_same<std::string,T0>::value || std::is_base_of<XMLSerializableBase,T0>::value
  213. || is_stl_container<T0>::value || is_eigen_type<T0>::value;
  214. };
  215. }
  216. }
  217. #ifndef IGL_STATIC_LIBRARY
  218. #include "serialize_xml.cpp"
  219. #endif
  220. #endif