serialize.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 binary file. STL containers, Eigen matrix types and nested data
  14. // structures are also supported. To serialize a user defined class implement
  15. // the interface Serializable.
  16. //
  17. // See also: xml/serialize_xml.h
  18. // -----------------------------------------------------------------------------
  19. // TODOs:
  20. // * loops of pointers and from multiple objects
  21. // * cross-platform compatibility (big-, little-endian)
  22. // -----------------------------------------------------------------------------
  23. #ifndef IGL_SERIALIZE_H
  24. #define IGL_SERIALIZE_H
  25. #include <type_traits>
  26. #include <iostream>
  27. #include <numeric>
  28. #include <vector>
  29. #include <set>
  30. #include <map>
  31. #include <Eigen/Dense>
  32. #include <Eigen/Sparse>
  33. //#define SERIALIZE(x) igl::serialize(x,#x,buffer);
  34. //#define DESERIALIZE(x) igl::deserialize(x,#x,buffer);
  35. namespace igl
  36. {
  37. // serializes the given object either to a file or to a provided buffer
  38. // Templates:
  39. // T type of the object to serialize
  40. // Inputs:
  41. // obj object to serialize
  42. // objectName unique object name,used for the identification
  43. // filename name of the file containing the serialization
  44. // Outputs:
  45. // buffer binary serialization
  46. //
  47. template <typename T>
  48. void serialize(const T& obj,const std::string& filename);
  49. template <typename T>
  50. void serialize(const T& obj,const std::string& objectName,std::vector<char>& buffer);
  51. // deserializes the given data from a file or buffer back to the provided object
  52. //
  53. // Templates:
  54. // T type of the object to serialize
  55. // Inputs:
  56. // buffer binary serialization
  57. // objectName unique object name, used for the identification
  58. // filename name of the file containing the serialization
  59. // Outputs:
  60. // obj object to load back serialization to
  61. //
  62. template <typename T>
  63. void deserialize(T& obj,const std::string& filename);
  64. template <typename T>
  65. void deserialize(T& obj,const std::string& objectName,const std::vector<char>& buffer);
  66. // interface for user defined types
  67. struct Serializable
  68. {
  69. virtual void Serialize(std::vector<char>& buffer) const = 0;
  70. virtual void Deserialize(const std::vector<char>& buffer) = 0;
  71. };
  72. // example:
  73. //
  74. // class Test : public igl::Serializable {
  75. //
  76. // int var;
  77. //
  78. // void Serialize(std::vector<char>& buffer) {
  79. // serialize(var,"var1",buffer);
  80. // }
  81. // void Deserialize(const std::vector<char>& buffer) {
  82. // deserialize(var,"var1",buffer);
  83. // }
  84. // }
  85. // internal functions
  86. namespace detail
  87. {
  88. // fundamental types
  89. template <typename T>
  90. std::enable_if_t<std::is_fundamental<T>::value,size_t> getByteSize(const T& obj);
  91. template <typename T>
  92. std::enable_if_t<std::is_fundamental<T>::value> serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  93. template <typename T>
  94. std::enable_if_t<std::is_fundamental<T>::value> deserialize(T& obj,std::vector<char>::const_iterator& iter);
  95. // std::string
  96. size_t getByteSize(const std::string& obj);
  97. void serialize(const std::string& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  98. void deserialize(std::string& obj,std::vector<char>::iterator& iter);
  99. // Serializable
  100. template <typename T>
  101. std::enable_if_t<std::is_base_of<Serializable,T>::value,size_t> getByteSize(const T& obj);
  102. template <typename T>
  103. std::enable_if_t<std::is_base_of<Serializable,T>::value> serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  104. template <typename T>
  105. std::enable_if_t<std::is_base_of<Serializable,T>::value> deserialize(T& obj,std::vector<char>::const_iterator& iter);
  106. // stl containers
  107. // std::pair
  108. template <typename T1,typename T2>
  109. size_t getByteSize(const std::pair<T1,T2>& obj);
  110. template <typename T1,typename T2>
  111. void serialize(const std::pair<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  112. template <typename T1,typename T2>
  113. void deserialize(std::pair<T1,T2>& obj,std::vector<char>::const_iterator& iter);
  114. // std::vector
  115. template <typename T1,typename T2>
  116. size_t getByteSize(const std::vector<T1,T2>& obj);
  117. template <typename T1,typename T2>
  118. void serialize(const std::vector<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  119. template <typename T1,typename T2>
  120. void deserialize(std::vector<T1,T2>& obj,std::vector<char>::const_iterator& iter);
  121. // std::set
  122. template <typename T>
  123. size_t getByteSize(const std::set<T>& obj);
  124. template <typename T>
  125. void serialize(const std::set<T>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  126. template <typename T>
  127. void deserialize(std::set<T>& obj,std::vector<char>::const_iterator& iter);
  128. // std::map
  129. template <typename T1,typename T2>
  130. size_t getByteSize(const std::map<T1,T2>& obj);
  131. template <typename T1,typename T2>
  132. void serialize(const std::map<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  133. template <typename T1,typename T2>
  134. void deserialize(std::map<T1,T2>& obj,std::vector<char>::const_iterator& iter);
  135. // Eigen types
  136. template<typename T,int R,int C,int P,int MR,int MC>
  137. size_t getByteSize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj);
  138. template<typename T,int R,int C,int P,int MR,int MC>
  139. void serialize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  140. template<typename T,int R,int C,int P,int MR,int MC>
  141. void deserialize(Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>::const_iterator& iter);
  142. template<typename T,int P,typename I>
  143. size_t getByteSize(const Eigen::SparseMatrix<T,P,I>& obj);
  144. template<typename T,int P,typename I>
  145. void serialize(const Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  146. template<typename T,int P,typename I>
  147. void deserialize(Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>::const_iterator& iter);
  148. // pointers
  149. template <typename T>
  150. std::enable_if_t<std::is_pointer<T>::value,size_t> getByteSize(const T& obj);
  151. template <typename T>
  152. std::enable_if_t<std::is_pointer<T>::value> serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  153. template <typename T>
  154. std::enable_if_t<std::is_pointer<T>::value> deserialize(T& obj,std::vector<char>::const_iterator& iter);
  155. // compile time type serializable check
  156. template <typename T>
  157. struct is_stl_container { static const bool value = false; };
  158. template <typename T1,typename T2>
  159. struct is_stl_container<std::pair<T1,T2> > { static const bool value = true; };
  160. template <typename T1,typename T2>
  161. struct is_stl_container<std::vector<T1,T2> > { static const bool value = true; };
  162. template <typename T>
  163. struct is_stl_container<std::set<T> > { static const bool value = true; };
  164. template <typename T1,typename T2>
  165. struct is_stl_container<std::map<T1,T2> > { static const bool value = true; };
  166. template <typename T>
  167. struct is_eigen_type { static const bool value = false; };
  168. template <typename T,int R,int C,int P,int MR,int MC>
  169. struct is_eigen_type<Eigen::Matrix<T,R,C,P,MR,MC> > { static const bool value = true; };
  170. template <typename T,int P,typename I>
  171. struct is_eigen_type<Eigen::SparseMatrix<T,P,I> > { static const bool value = true; };
  172. template <typename T>
  173. struct is_serializable {
  174. using T0 = typename std::remove_pointer<T>::type;
  175. static const bool value = std::is_fundamental<T0>::value || std::is_same<std::string,T0>::value || std::is_base_of<Serializable,T0>::value
  176. || is_stl_container<T0>::value || is_eigen_type<T0>::value;
  177. };
  178. }
  179. }
  180. #include "serialize.cpp"
  181. #endif