serialize_xml.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.
  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 overwrite = 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 XMLSerializable : public Serializable
  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. // example:
  81. //
  82. // class Test : public igl::Serializable {
  83. //
  84. // int var;
  85. //
  86. // void Serialize(std::vector<char>& buffer) {
  87. // serialize(var,"var1",buffer);
  88. // }
  89. // void Deserialize(const std::vector<char>& buffer) {
  90. // deserialize(var,"var1",buffer);
  91. // }
  92. // void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const {
  93. // serialize_xml(var,"var1",doc,element);
  94. // }
  95. // void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element) {
  96. // deserialize_xml(var,"var1",doc,element);
  97. // }
  98. // }
  99. // internal functions
  100. namespace detail_xml
  101. {
  102. // fundamental types
  103. template <typename T>
  104. 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);
  105. template <typename T>
  106. 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);
  107. // std::string
  108. IGL_INLINE void serialize(const std::string& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  109. IGL_INLINE void deserialize(std::string& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  110. // Serializable
  111. template <typename T>
  112. IGL_INLINE typename std::enable_if<std::is_base_of<XMLSerializable,T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  113. template <typename T>
  114. IGL_INLINE typename std::enable_if<std::is_base_of<XMLSerializable,T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  115. // STL containers
  116. template <typename T1, typename T2>
  117. IGL_INLINE void serialize(const std::pair<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  118. template <typename T1,typename T2>
  119. IGL_INLINE void deserialize(std::pair<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  120. template <typename T1,typename T2>
  121. IGL_INLINE void serialize(const std::vector<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  122. template <typename T1,typename T2>
  123. IGL_INLINE void deserialize(std::vector<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  124. template <typename T>
  125. IGL_INLINE void serialize(const std::set<T>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  126. template <typename T>
  127. IGL_INLINE void deserialize(std::set<T>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  128. template <typename T1,typename T2>
  129. IGL_INLINE void serialize(const std::map<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  130. template <typename T1,typename T2>
  131. IGL_INLINE void deserialize(std::map<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  132. // Eigen types
  133. template<typename T,int R,int C,int P,int MR,int MC>
  134. IGL_INLINE void serialize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  135. template<typename T,int R,int C,int P,int MR,int MC>
  136. 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);
  137. template<typename T,int P,typename I>
  138. IGL_INLINE void serialize(const Eigen::SparseMatrix<T,P,I>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  139. template<typename T,int P,typename I>
  140. IGL_INLINE void deserialize(Eigen::SparseMatrix<T,P,I>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
  141. // pointers
  142. template <typename T>
  143. 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);
  144. template <typename T>
  145. 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);
  146. // helper functions
  147. tinyxml2::XMLElement* getElement(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
  148. IGL_INLINE void getAttribute(const char* src,bool& dest);
  149. IGL_INLINE void getAttribute(const char* scr,char& dest);
  150. IGL_INLINE void getAttribute(const char* src,std::string& dest);
  151. IGL_INLINE void getAttribute(const char* src,float& dest);
  152. IGL_INLINE void getAttribute(const char* src,double& dest);
  153. template<typename T>
  154. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest);
  155. template<typename T>
  156. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && !std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest);
  157. IGL_INLINE void replaceSubString(std::string& str,const std::string& search,const std::string& replace);
  158. IGL_INLINE void encodeXMLElementName(std::string& name);
  159. IGL_INLINE void decodeXMLElementName(std::string& name);
  160. IGL_INLINE std::string base64_encode(unsigned char const* bytes_to_encode,unsigned int in_len);
  161. IGL_INLINE std::string base64_decode(std::string const& encoded_string);
  162. // compile time type serializable check
  163. template <typename T>
  164. struct is_stl_container { static const bool value = false; };
  165. template <typename T1,typename T2>
  166. struct is_stl_container<std::pair<T1,T2> > { static const bool value = true; };
  167. template <typename T1,typename T2>
  168. struct is_stl_container<std::vector<T1,T2> > { static const bool value = true; };
  169. template <typename T>
  170. struct is_stl_container<std::set<T> > { static const bool value = true; };
  171. template <typename T1,typename T2>
  172. struct is_stl_container<std::map<T1,T2> > { static const bool value = true; };
  173. template <typename T>
  174. struct is_eigen_type { static const bool value = false; };
  175. template <typename T,int R,int C,int P,int MR,int MC>
  176. struct is_eigen_type<Eigen::Matrix<T,R,C,P,MR,MC> > { static const bool value = true; };
  177. template <typename T,int P,typename I>
  178. struct is_eigen_type<Eigen::SparseMatrix<T,P,I> > { static const bool value = true; };
  179. template <typename T>
  180. struct is_serializable {
  181. using T0 = typename std::remove_pointer<T>::type;
  182. static const bool value = std::is_fundamental<T0>::value || std::is_same<std::string,T0>::value || std::is_base_of<Serializable,T0>::value
  183. || is_stl_container<T0>::value || is_eigen_type<T0>::value;
  184. };
  185. }
  186. }
  187. #ifndef IGL_STATIC_LIBRARY
  188. #include "serialize_xml.cpp";
  189. #endif
  190. #endif