serialize_xml.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #ifndef IGL_XML_SERIALIZABLE_XML_H
  8. #define IGL_XML_SERIALIZABLE_XML_H
  9. // -----------------------------------------------------------------------------
  10. // Functions to save and load a serialization of fundamental c++ data types to
  11. // and from a xml file. STL containers, Eigen matrix types and nested data
  12. // structures are also supported. To serialize a user defined class implement
  13. // the interface XMLSerializable or XMLSerializableBase.
  14. //
  15. // See also: serialize.h
  16. // -----------------------------------------------------------------------------
  17. #include "../igl_inline.h"
  18. #include <Eigen/Dense>
  19. #include <Eigen/Sparse>
  20. #include <tinyxml2.h>
  21. #include <type_traits>
  22. #include <functional>
  23. #include <iostream>
  24. #include <vector>
  25. #include <set>
  26. #include <map>
  27. #include <memory>
  28. //#define SERIALIZE_XML(x) igl::xml::serialize_xml(x,#x,doc,element);
  29. //#define DESERIALIZE_XML(x) igl::xml::deserialize_xml(x,#x,,doc,element);
  30. namespace igl
  31. {
  32. namespace xml
  33. {
  34. struct XMLSerializableBase;
  35. // serializes the given object either to a xml file or to the provided doc data
  36. //
  37. // Templates:
  38. // T type of the object to serialize
  39. // Inputs:
  40. // obj object to serialize
  41. // objectName unique object name,used for the identification
  42. // filename name of the file containing the serialization
  43. // binary set to true to serialize the object in binary format (faster for big data)
  44. // overwrite set to true to overwrite an existing xml file
  45. // element tinyxml2 virtual representation of the current xml node
  46. // Outputs:
  47. // doc contains current tinyxml2 virtual representation of the xml data
  48. //
  49. template <typename T>
  50. IGL_INLINE void serialize_xml(const T& obj,const std::string& filename);
  51. template <typename T>
  52. IGL_INLINE void serialize_xml(
  53. const T& obj,
  54. const std::string& objectName,
  55. const std::string& filename,
  56. bool binary = false,
  57. bool overwrite = false);
  58. template <typename T>
  59. IGL_INLINE void serialize_xml(
  60. const T& obj,
  61. const std::string& objectName,
  62. tinyxml2::XMLDocument* doc,
  63. tinyxml2::XMLElement* element,
  64. bool binary = false);
  65. // deserializes the given data from a xml file or doc data back to the provided object
  66. //
  67. // Templates:
  68. // T type of the object to serialize
  69. // Inputs:
  70. //
  71. // objectName unique object name,used for the identification
  72. // filename name of the file containing the serialization
  73. // binary set to true to serialize the object in binary format (faster for big data)
  74. // overwrite set to true to overwrite an existing xml file
  75. // doc contains current tinyxml2 virtual representation of the xml data
  76. // element tinyxml2 virtual representation of the current xml node
  77. // Outputs:
  78. // obj object to load back serialization to
  79. //
  80. template <typename T>
  81. IGL_INLINE void deserialize_xml(T& obj,const std::string& filename);
  82. template <typename T>
  83. IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const std::string& filename);
  84. template <typename T>
  85. IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element);
  86. // internal functions
  87. namespace serialization_xml
  88. {
  89. // fundamental types
  90. template <typename T>
  91. 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);
  92. template <typename T>
  93. 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);
  94. // std::string
  95. IGL_INLINE void serialize(const std::string& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  96. IGL_INLINE void deserialize(std::string& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  97. // XMLSerializableBase
  98. template <typename T>
  99. 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);
  100. template <typename T>
  101. 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);
  102. // STL containers
  103. template <typename T1, typename T2>
  104. IGL_INLINE void serialize(const std::pair<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  105. template <typename T1,typename T2>
  106. IGL_INLINE void deserialize(std::pair<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  107. template <typename T1,typename T2>
  108. IGL_INLINE void serialize(const std::vector<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  109. template <typename T1,typename T2>
  110. IGL_INLINE void deserialize(std::vector<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  111. template <typename T>
  112. IGL_INLINE void serialize(const std::set<T>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  113. template <typename T>
  114. IGL_INLINE void deserialize(std::set<T>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  115. template <typename T1,typename T2>
  116. IGL_INLINE void serialize(const std::map<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  117. template <typename T1,typename T2>
  118. IGL_INLINE void deserialize(std::map<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  119. // Eigen types
  120. // Serialize a Dense Eigen Matrix to xml (in the matrix= attribute,
  121. // awkward...)
  122. //
  123. // Inputs:
  124. // obj MR by MC matrix of T types
  125. // name name of matrix
  126. // to_string function converting T to string
  127. // Outputs:
  128. // doc pointer to xml document
  129. // element pointer to xml element
  130. //
  131. template<typename T,int R,int C,int P,int MR,int MC>
  132. IGL_INLINE void serialize(
  133. const Eigen::Matrix<T,R,C,P,MR,MC>& obj,
  134. const std::string& name,
  135. const std::function<std::string(const T &) >& to_string,
  136. tinyxml2::XMLDocument* doc,
  137. tinyxml2::XMLElement* element);
  138. // De-Serialize a Dense Eigen Matrix from xml (in the matrix= attribute,
  139. // awkward...)
  140. //
  141. // Inputs:
  142. // doc pointer to xml document
  143. // element pointer to xml element
  144. // name name of matrix
  145. // from_string function string to T
  146. // Outputs:
  147. // obj MR by MC matrix of T types
  148. template<typename T,int R,int C,int P,int MR,int MC>
  149. IGL_INLINE void deserialize(
  150. const tinyxml2::XMLDocument* doc,
  151. const tinyxml2::XMLElement* element,
  152. const std::string& name,
  153. const std::function<void(const std::string &,T &)> & from_string,
  154. Eigen::Matrix<T,R,C,P,MR,MC>& obj);
  155. // Legacy APIs
  156. template<typename T,int R,int C,int P,int MR,int MC>
  157. IGL_INLINE void serialize(
  158. const Eigen::Matrix<T,R,C,P,MR,MC>& obj,
  159. tinyxml2::XMLDocument* doc,
  160. tinyxml2::XMLElement* element,
  161. const std::string& name);
  162. template<typename T,int R,int C,int P,int MR,int MC>
  163. IGL_INLINE void deserialize(
  164. Eigen::Matrix<T,R,C,P,MR,MC>& obj,
  165. const tinyxml2::XMLDocument* doc,
  166. const tinyxml2::XMLElement* element,
  167. const std::string& name);
  168. template<typename T,int P,typename I>
  169. IGL_INLINE void serialize(const Eigen::SparseMatrix<T,P,I>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  170. template<typename T,int P,typename I>
  171. IGL_INLINE void deserialize(Eigen::SparseMatrix<T,P,I>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  172. // raw pointers
  173. template <typename T>
  174. 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);
  175. template <typename T>
  176. 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);
  177. // helper functions
  178. tinyxml2::XMLElement* getElement(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  179. IGL_INLINE void getAttribute(const char* src,bool& dest);
  180. IGL_INLINE void getAttribute(const char* scr,char& dest);
  181. IGL_INLINE void getAttribute(const char* src,std::string& dest);
  182. IGL_INLINE void getAttribute(const char* src,float& dest);
  183. IGL_INLINE void getAttribute(const char* src,double& 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. template<typename T>
  187. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && !std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest);
  188. IGL_INLINE void replaceSubString(std::string& str,const std::string& search,const std::string& replace);
  189. IGL_INLINE void encodeXMLElementName(std::string& name);
  190. IGL_INLINE void decodeXMLElementName(std::string& name);
  191. IGL_INLINE std::string base64_encode(unsigned char const* bytes_to_encode,unsigned int in_len);
  192. IGL_INLINE std::string base64_decode(std::string const& encoded_string);
  193. // compile time type serializable check
  194. template <typename T>
  195. struct is_stl_container { static const bool value = false; };
  196. template <typename T1,typename T2>
  197. struct is_stl_container<std::pair<T1,T2> > { static const bool value = true; };
  198. template <typename T1,typename T2>
  199. struct is_stl_container<std::vector<T1,T2> > { static const bool value = true; };
  200. template <typename T>
  201. struct is_stl_container<std::set<T> > { static const bool value = true; };
  202. template <typename T1,typename T2>
  203. struct is_stl_container<std::map<T1,T2> > { static const bool value = true; };
  204. template <typename T>
  205. struct is_eigen_type { static const bool value = false; };
  206. template <typename T,int R,int C,int P,int MR,int MC>
  207. struct is_eigen_type<Eigen::Matrix<T,R,C,P,MR,MC> > { static const bool value = true; };
  208. template <typename T,int P,typename I>
  209. struct is_eigen_type<Eigen::SparseMatrix<T,P,I> > { static const bool value = true; };
  210. template <typename T>
  211. struct is_serializable {
  212. using T0 = typename std::remove_pointer<T>::type;
  213. static const bool value = std::is_fundamental<T0>::value || std::is_same<std::string,T0>::value || std::is_base_of<XMLSerializableBase,T0>::value
  214. || is_stl_container<T0>::value || is_eigen_type<T0>::value;
  215. };
  216. }
  217. }
  218. }
  219. #ifndef IGL_STATIC_LIBRARY
  220. #include "serialize_xml.cpp"
  221. #endif
  222. #endif