serialize.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 binary file. STL containers, Eigen matrix types and nested data
  10. // structures are also supported. To serialize a user defined class implement
  11. // the interface Serializable or SerializableBase.
  12. //
  13. // See also: xml/serialize_xml.h
  14. // -----------------------------------------------------------------------------
  15. // TODOs:
  16. // * loops of pointers and from multiple objects
  17. // * cross-platform compatibility (big-, little-endian)
  18. // -----------------------------------------------------------------------------
  19. #ifndef IGL_SERIALIZE_H
  20. #define IGL_SERIALIZE_H
  21. #include <type_traits>
  22. #include <iostream>
  23. #include <fstream>
  24. #include <cstdint>
  25. #include <numeric>
  26. #include <vector>
  27. #include <set>
  28. #include <map>
  29. #include <Eigen/Dense>
  30. #include <Eigen/Sparse>
  31. #include "igl_inline.h"
  32. //#define SERIALIZE(x) igl::serialize(x,#x,buffer);
  33. //#define DESERIALIZE(x) igl::deserialize(x,#x,buffer);
  34. namespace igl
  35. {
  36. // Serializes the given object either to a file or to a provided buffer
  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. // overwrite set to true to overwrite an existing file
  43. // filename name of the file containing the serialization
  44. // Outputs:
  45. // buffer binary serialization
  46. //
  47. template <typename T>
  48. IGL_INLINE bool serialize(const T& obj,const std::string& filename);
  49. template <typename T>
  50. IGL_INLINE bool serialize(const T& obj,const std::string& objectName,const std::string& filename,bool overwrite = false);
  51. template <typename T>
  52. IGL_INLINE bool serialize(const T& obj,const std::string& objectName,std::vector<char>& buffer);
  53. // Deserializes the given data from a file or buffer back to the provided object
  54. //
  55. // Templates:
  56. // T type of the object to serialize
  57. // Inputs:
  58. // buffer binary serialization
  59. // objectName unique object name, used for the identification
  60. // filename name of the file containing the serialization
  61. // Outputs:
  62. // obj object to load back serialization to
  63. //
  64. template <typename T>
  65. IGL_INLINE bool deserialize(T& obj,const std::string& filename);
  66. template <typename T>
  67. IGL_INLINE bool deserialize(T& obj,const std::string& objectName,const std::string& filename);
  68. template <typename T>
  69. IGL_INLINE bool deserialize(T& obj,const std::string& objectName,const std::vector<char>& buffer);
  70. // User defined types have to derive from the class Serializable
  71. // and add their member variables in InitSerialization like the
  72. // following:
  73. //
  74. // class Test : public igl::Serializable {
  75. //
  76. // int var;
  77. //
  78. // void InitSerialization() {
  79. // this->Add(var,"var");
  80. // }
  81. // };
  82. // Base interface for user defined types
  83. struct SerializableBase
  84. {
  85. virtual void Serialize(std::vector<char>& buffer) const = 0;
  86. virtual void Deserialize(const std::vector<char>& buffer) = 0;
  87. };
  88. // Convenient interface for user defined types
  89. class Serializable: public SerializableBase
  90. {
  91. private:
  92. template <typename T>
  93. struct SerializationObject : public SerializableBase
  94. {
  95. bool Binary;
  96. std::string Name;
  97. T* Object;
  98. void Serialize(std::vector<char>& buffer) const {
  99. igl::serialize(*Object,Name,buffer);
  100. }
  101. void Deserialize(const std::vector<char>& buffer) {
  102. igl::deserialize(*Object,Name,buffer);
  103. }
  104. };
  105. mutable bool initialized;
  106. mutable std::vector<SerializableBase*> objects;
  107. public:
  108. // Override this function to add your member variables which should be serialized
  109. IGL_INLINE virtual void InitSerialization() = 0;
  110. // Following functions can be overridden to handle the specific events.
  111. // Return false to prevent the de-/serialization of an object.
  112. IGL_INLINE virtual bool PreSerialization() const;
  113. IGL_INLINE virtual void PostSerialization() const;
  114. IGL_INLINE virtual bool PreDeserialization();
  115. IGL_INLINE virtual void PostDeserialization();
  116. // Default implementation of SerializableBase interface
  117. IGL_INLINE void Serialize(std::vector<char>& buffer) const;
  118. IGL_INLINE void Deserialize(const std::vector<char>& buffer);
  119. // Default constructor, destructor, assignment and copy constructor
  120. IGL_INLINE Serializable();
  121. IGL_INLINE Serializable(const Serializable& obj);
  122. IGL_INLINE ~Serializable();
  123. IGL_INLINE Serializable& operator=(const Serializable& 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
  130. {
  131. // fundamental types
  132. template <typename T>
  133. IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value,size_t>::type getByteSize(const T& obj);
  134. template <typename T>
  135. IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value>::type serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  136. template <typename T>
  137. IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter);
  138. // std::string
  139. IGL_INLINE size_t getByteSize(const std::string& obj);
  140. IGL_INLINE void serialize(const std::string& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  141. IGL_INLINE void deserialize(std::string& obj,std::vector<char>::const_iterator& iter);
  142. // SerializableBase
  143. template <typename T>
  144. IGL_INLINE typename std::enable_if<std::is_base_of<SerializableBase,T>::value,size_t>::type getByteSize(const T& obj);
  145. template <typename T>
  146. IGL_INLINE typename std::enable_if<std::is_base_of<SerializableBase,T>::value>::type serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  147. template <typename T>
  148. IGL_INLINE typename std::enable_if<std::is_base_of<SerializableBase,T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter);
  149. // stl containers
  150. // std::pair
  151. template <typename T1,typename T2>
  152. IGL_INLINE size_t getByteSize(const std::pair<T1,T2>& obj);
  153. template <typename T1,typename T2>
  154. IGL_INLINE void serialize(const std::pair<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  155. template <typename T1,typename T2>
  156. IGL_INLINE void deserialize(std::pair<T1,T2>& obj,std::vector<char>::const_iterator& iter);
  157. // std::vector
  158. template <typename T1,typename T2>
  159. IGL_INLINE size_t getByteSize(const std::vector<T1,T2>& obj);
  160. template <typename T1,typename T2>
  161. IGL_INLINE void serialize(const std::vector<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  162. template <typename T1,typename T2>
  163. IGL_INLINE void deserialize(std::vector<T1,T2>& obj,std::vector<char>::const_iterator& iter);
  164. // std::set
  165. template <typename T>
  166. IGL_INLINE size_t getByteSize(const std::set<T>& obj);
  167. template <typename T>
  168. IGL_INLINE void serialize(const std::set<T>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  169. template <typename T>
  170. IGL_INLINE void deserialize(std::set<T>& obj,std::vector<char>::const_iterator& iter);
  171. // std::map
  172. template <typename T1,typename T2>
  173. IGL_INLINE size_t getByteSize(const std::map<T1,T2>& obj);
  174. template <typename T1,typename T2>
  175. IGL_INLINE void serialize(const std::map<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  176. template <typename T1,typename T2>
  177. IGL_INLINE void deserialize(std::map<T1,T2>& obj,std::vector<char>::const_iterator& iter);
  178. // Eigen types
  179. template<typename T,int R,int C,int P,int MR,int MC>
  180. IGL_INLINE size_t getByteSize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj);
  181. template<typename T,int R,int C,int P,int MR,int MC>
  182. IGL_INLINE void serialize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  183. template<typename T,int R,int C,int P,int MR,int MC>
  184. IGL_INLINE void deserialize(Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>::const_iterator& iter);
  185. template<typename T,int P,typename I>
  186. IGL_INLINE size_t getByteSize(const Eigen::SparseMatrix<T,P,I>& obj);
  187. template<typename T,int P,typename I>
  188. IGL_INLINE void serialize(const Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  189. template<typename T,int P,typename I>
  190. IGL_INLINE void deserialize(Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>::const_iterator& iter);
  191. // pointers
  192. template <typename T>
  193. IGL_INLINE typename std::enable_if<std::is_pointer<T>::value,size_t>::type getByteSize(const T& obj);
  194. template <typename T>
  195. IGL_INLINE typename std::enable_if<std::is_pointer<T>::value>::type serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  196. template <typename T>
  197. IGL_INLINE typename std::enable_if<std::is_pointer<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter);
  198. // compile time type serializable check
  199. template <typename T>
  200. struct is_stl_container { static const bool value = false; };
  201. template <typename T1,typename T2>
  202. struct is_stl_container<std::pair<T1,T2> > { static const bool value = true; };
  203. template <typename T1,typename T2>
  204. struct is_stl_container<std::vector<T1,T2> > { static const bool value = true; };
  205. template <typename T>
  206. struct is_stl_container<std::set<T> > { static const bool value = true; };
  207. template <typename T1,typename T2>
  208. struct is_stl_container<std::map<T1,T2> > { static const bool value = true; };
  209. template <typename T>
  210. struct is_eigen_type { static const bool value = false; };
  211. template <typename T,int R,int C,int P,int MR,int MC>
  212. struct is_eigen_type<Eigen::Matrix<T,R,C,P,MR,MC> > { static const bool value = true; };
  213. template <typename T,int P,typename I>
  214. struct is_eigen_type<Eigen::SparseMatrix<T,P,I> > { static const bool value = true; };
  215. template <typename T>
  216. struct is_serializable {
  217. using T0 = typename std::remove_pointer<T>::type;
  218. static const bool value = std::is_fundamental<T0>::value || std::is_same<std::string,T0>::value || std::is_base_of<SerializableBase,T0>::value
  219. || is_stl_container<T0>::value || is_eigen_type<T0>::value;
  220. };
  221. }
  222. }
  223. #ifndef IGL_STATIC_LIBRARY
  224. #include "serialize.cpp"
  225. #endif
  226. #endif