serialize.h 8.1 KB

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