serialize_xml.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // Copyright (C) 2014 Christian Sch�ller <[email protected]>
  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. /// Serialize object to file
  36. /// @tparam T type of the object to serialize
  37. /// @param[in] obj object to serialize
  38. /// @param[in] filename name of the file containing the serialization
  39. /// Serialize object to file
  40. ///
  41. /// @tparam T type of the object to serialize
  42. /// @param[in] obj object to serialize
  43. /// @param[in] objectName unique object name,used for the identification
  44. /// @param[in] filename name of the file containing the serialization
  45. /// @param[in] binary set to true to serialize the object in binary format (faster for big data)
  46. /// @param[in] overwrite set to true to overwrite an existing xml file
  47. template <typename T>
  48. IGL_INLINE void serialize_xml(
  49. const T& obj,
  50. const std::string& objectName,
  51. const std::string& filename,
  52. bool binary = false,
  53. bool overwrite = false);
  54. /// \overload
  55. template <typename T>
  56. IGL_INLINE void serialize_xml(const T& obj,const std::string& filename);
  57. /// \overload
  58. ///
  59. /// @param[in,out] doc contains current tinyxml2 virtual representation of the xml data
  60. /// @param[in,out] element tinyxml2 virtual representation of the current xml node
  61. template <typename T>
  62. IGL_INLINE void serialize_xml(
  63. const T& obj,
  64. const std::string& objectName,
  65. tinyxml2::XMLDocument* doc,
  66. tinyxml2::XMLElement* element,
  67. bool binary = false);
  68. /// deserialize object to file
  69. ///
  70. /// @tparam T type of the object to serialize
  71. /// @param[in] obj object to serialize
  72. /// @param[in] objectName unique object name,used for the identification
  73. /// @param[in] filename name of the file containing the serialization
  74. /// @param[in] binary set to true to serialize the object in binary format (faster for big data)
  75. /// @param[in] overwrite set to true to overwrite an existing xml file
  76. ///
  77. /// \fileinfo
  78. template <typename T>
  79. IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const std::string& filename);
  80. /// \overload
  81. ///
  82. /// \fileinfo
  83. template <typename T>
  84. IGL_INLINE void deserialize_xml(T& obj,const std::string& filename);
  85. /// Deserialize to xml doc
  86. ///
  87. /// @param[in,out] doc contains current tinyxml2 virtual representation of the xml data
  88. /// @param[in,out] element tinyxml2 virtual representation of the current xml node
  89. ///
  90. /// \fileinfo
  91. template <typename T>
  92. IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element);
  93. // internal functions
  94. /// @private
  95. namespace serialization_xml
  96. {
  97. // fundamental types
  98. template <typename T>
  99. 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);
  100. template <typename T>
  101. 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);
  102. // std::string
  103. IGL_INLINE void serialize(const std::string& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  104. IGL_INLINE void deserialize(std::string& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  105. // XMLSerializableBase
  106. template <typename T>
  107. 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);
  108. template <typename T>
  109. 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);
  110. // STL containers
  111. template <typename T1, typename T2>
  112. IGL_INLINE void serialize(const std::pair<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  113. template <typename T1,typename T2>
  114. IGL_INLINE void deserialize(std::pair<T1,T2>& 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::vector<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::vector<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  119. template <typename T>
  120. IGL_INLINE void serialize(const std::set<T>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  121. template <typename T>
  122. IGL_INLINE void deserialize(std::set<T>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  123. template <typename T1,typename T2>
  124. IGL_INLINE void serialize(const std::map<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  125. template <typename T1,typename T2>
  126. IGL_INLINE void deserialize(std::map<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  127. // Eigen types
  128. // Serialize a Dense Eigen Matrix to xml (in the matrix= attribute,
  129. // awkward...)
  130. //
  131. // Inputs:
  132. // obj MR by MC matrix of T types
  133. // name name of matrix
  134. // to_string function converting T to string
  135. // Outputs:
  136. // doc pointer to xml document
  137. // element pointer to xml element
  138. //
  139. template<typename T,int R,int C,int P,int MR,int MC>
  140. IGL_INLINE void serialize(
  141. const Eigen::Matrix<T,R,C,P,MR,MC>& obj,
  142. const std::string& name,
  143. const std::function<std::string(const T &) >& to_string,
  144. tinyxml2::XMLDocument* doc,
  145. tinyxml2::XMLElement* element);
  146. // De-Serialize a Dense Eigen Matrix from xml (in the matrix= attribute,
  147. // awkward...)
  148. //
  149. // Inputs:
  150. // doc pointer to xml document
  151. // element pointer to xml element
  152. // name name of matrix
  153. // from_string function string to T
  154. // Outputs:
  155. // obj MR by MC matrix of T types
  156. template<typename T,int R,int C,int P,int MR,int MC>
  157. IGL_INLINE void deserialize(
  158. const tinyxml2::XMLDocument* doc,
  159. const tinyxml2::XMLElement* element,
  160. const std::string& name,
  161. const std::function<void(const std::string &,T &)> & from_string,
  162. Eigen::Matrix<T,R,C,P,MR,MC>& obj);
  163. // Legacy APIs
  164. template<typename T,int R,int C,int P,int MR,int MC>
  165. IGL_INLINE void serialize(
  166. const Eigen::Matrix<T,R,C,P,MR,MC>& obj,
  167. tinyxml2::XMLDocument* doc,
  168. tinyxml2::XMLElement* element,
  169. const std::string& name);
  170. template<typename T,int R,int C,int P,int MR,int MC>
  171. IGL_INLINE void deserialize(
  172. Eigen::Matrix<T,R,C,P,MR,MC>& obj,
  173. const tinyxml2::XMLDocument* doc,
  174. const tinyxml2::XMLElement* element,
  175. const std::string& name);
  176. template<typename T,int P,typename I>
  177. IGL_INLINE void serialize(const Eigen::SparseMatrix<T,P,I>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  178. template<typename T,int P,typename I>
  179. IGL_INLINE void deserialize(Eigen::SparseMatrix<T,P,I>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  180. // raw pointers
  181. template <typename T>
  182. 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);
  183. template <typename T>
  184. 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);
  185. // helper functions
  186. tinyxml2::XMLElement* getElement(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  187. IGL_INLINE void getAttribute(const char* src,bool& dest);
  188. IGL_INLINE void getAttribute(const char* scr,char& dest);
  189. IGL_INLINE void getAttribute(const char* src,std::string& dest);
  190. IGL_INLINE void getAttribute(const char* src,float& dest);
  191. IGL_INLINE void getAttribute(const char* src,double& dest);
  192. template<typename T>
  193. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest);
  194. template<typename T>
  195. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && !std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest);
  196. IGL_INLINE void replaceSubString(std::string& str,const std::string& search,const std::string& replace);
  197. IGL_INLINE void encodeXMLElementName(std::string& name);
  198. IGL_INLINE void decodeXMLElementName(std::string& name);
  199. IGL_INLINE std::string base64_encode(unsigned char const* bytes_to_encode,unsigned int in_len);
  200. IGL_INLINE std::string base64_decode(std::string const& encoded_string);
  201. // compile time type serializable check
  202. template <typename T>
  203. struct is_stl_container { static const bool value = false; };
  204. template <typename T1,typename T2>
  205. struct is_stl_container<std::pair<T1,T2> > { static const bool value = true; };
  206. template <typename T1,typename T2>
  207. struct is_stl_container<std::vector<T1,T2> > { static const bool value = true; };
  208. template <typename T>
  209. struct is_stl_container<std::set<T> > { static const bool value = true; };
  210. template <typename T1,typename T2>
  211. struct is_stl_container<std::map<T1,T2> > { static const bool value = true; };
  212. template <typename T>
  213. struct is_eigen_type { static const bool value = false; };
  214. template <typename T,int R,int C,int P,int MR,int MC>
  215. struct is_eigen_type<Eigen::Matrix<T,R,C,P,MR,MC> > { static const bool value = true; };
  216. template <typename T,int P,typename I>
  217. struct is_eigen_type<Eigen::SparseMatrix<T,P,I> > { static const bool value = true; };
  218. template <typename T>
  219. struct is_serializable {
  220. using T0 = typename std::remove_pointer<T>::type;
  221. static const bool value = std::is_fundamental<T0>::value || std::is_same<std::string,T0>::value || std::is_base_of<XMLSerializableBase,T0>::value
  222. || is_stl_container<T0>::value || is_eigen_type<T0>::value;
  223. };
  224. }
  225. }
  226. }
  227. #ifndef IGL_STATIC_LIBRARY
  228. #include "serialize_xml.cpp"
  229. #endif
  230. #endif