serialize_xml.h 12 KB

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