serialize_xml.h 10 KB

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