serialize.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Christian Schüller <[email protected]>
  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. #ifndef IGL_SERIALIZE_H
  9. #define IGL_SERIALIZE_H
  10. // -----------------------------------------------------------------------------
  11. // Functions to save and load a serialization of fundamental c++ data types to
  12. // and from a binary file. STL containers, Eigen matrix types and nested data
  13. // structures are also supported. To serialize a user defined class implement
  14. // the interface Serializable or SerializableBase.
  15. //
  16. // See also: xml/serialize_xml.h
  17. // -----------------------------------------------------------------------------
  18. // TODOs:
  19. // * arbitrary pointer graph structures
  20. // -----------------------------------------------------------------------------
  21. // Known issues: This is not written in libigl-style so it isn't (easily)
  22. // "dualized" into the static library.
  23. //
  24. #include <type_traits>
  25. #include <iostream>
  26. #include <fstream>
  27. #include <cstdint>
  28. #include <numeric>
  29. #include <vector>
  30. #include <set>
  31. #include <map>
  32. #include <memory>
  33. #include <cstdint>
  34. #include <list>
  35. #include <Eigen/Dense>
  36. #include <Eigen/Sparse>
  37. #include "igl_inline.h"
  38. // non-intrusive serialization helper macros
  39. #define SERIALIZE_TYPE(Type,Params) \
  40. namespace igl { namespace serialization { \
  41. void _serialization(bool s,Type& obj,std::vector<char>& buffer) {Params} \
  42. template<> inline void serialize(const Type& obj,std::vector<char>& buffer) { \
  43. _serialization(true,const_cast<Type&>(obj),buffer); \
  44. } \
  45. template<> inline void deserialize(Type& obj,const std::vector<char>& buffer) { \
  46. _serialization(false,obj,const_cast<std::vector<char>&>(buffer)); \
  47. } \
  48. }}
  49. #define SERIALIZE_TYPE_SOURCE(Type,Params) \
  50. namespace igl { namespace serialization { \
  51. void _serialization(bool s,Type& obj,std::vector<char>& buffer) {Params} \
  52. void _serialize(const Type& obj,std::vector<char>& buffer) { \
  53. _serialization(true,const_cast<Type&>(obj),buffer); \
  54. } \
  55. void _deserialize(Type& obj,const std::vector<char>& buffer) { \
  56. _serialization(false,obj,const_cast<std::vector<char>&>(buffer)); \
  57. } \
  58. }}
  59. #define SERIALIZE_MEMBER(Object) igl::serializer(s,obj.Object,std::string(#Object),buffer);
  60. #define SERIALIZE_MEMBER_NAME(Object,Name) igl::serializer(s,obj.Object,std::string(Name),buffer);
  61. namespace igl
  62. {
  63. struct IndexedPointerBase;
  64. // Serializes the given object either to a file or to a provided buffer
  65. // Templates:
  66. // T type of the object to serialize
  67. // Inputs:
  68. // obj object to serialize
  69. // objectName unique object name,used for the identification
  70. // overwrite set to true to overwrite an existing file
  71. // filename name of the file containing the serialization
  72. // Outputs:
  73. // buffer binary serialization
  74. //
  75. template <typename T>
  76. inline bool serialize(const T& obj,const std::string& filename);
  77. template <typename T>
  78. inline bool serialize(const T& obj,const std::string& objectName,const std::string& filename,bool overwrite = false);
  79. template <typename T>
  80. inline bool serialize(const T& obj,const std::string& objectName,std::vector<char>& buffer);
  81. template <typename T>
  82. inline bool serialize(const T& obj,const std::string& objectName,std::vector<char>& buffer);
  83. // Deserializes the given data from a file or buffer back to the provided object
  84. //
  85. // Templates:
  86. // T type of the object to serialize
  87. // Inputs:
  88. // buffer binary serialization
  89. // objectName unique object name, used for the identification
  90. // filename name of the file containing the serialization
  91. // Outputs:
  92. // obj object to load back serialization to
  93. //
  94. template <typename T>
  95. inline bool deserialize(T& obj,const std::string& filename);
  96. template <typename T>
  97. inline bool deserialize(T& obj,const std::string& objectName,const std::string& filename);
  98. template <typename T>
  99. inline bool deserialize(T& obj,const std::string& objectName,const std::vector<char>& buffer);
  100. // Wrapper to expose both, the de- and serialization as one function
  101. //
  102. template <typename T>
  103. inline bool serializer(bool serialize,T& obj,const std::string& filename);
  104. template <typename T>
  105. inline bool serializer(bool serialize,T& obj,const std::string& objectName,const std::string& filename,bool overwrite = false);
  106. template <typename T>
  107. inline bool serializer(bool serialize,T& obj,const std::string& objectName,std::vector<char>& buffer);
  108. // User defined types have to either overload the function igl::serialization::serialize()
  109. // and igl::serialization::deserialize() for their type (non-intrusive serialization):
  110. //
  111. // namespace igl { namespace serialization
  112. // {
  113. // template<>
  114. // inline void serialize(const UserType& obj,std::vector<char>& buffer) {
  115. // ::igl::serialize(obj.var,"var",buffer);
  116. // }
  117. //
  118. // template<>
  119. // inline void deserialize(UserType& obj,const std::vector<char>& buffer) {
  120. // ::igl::deserialize(obj.var,"var",buffer);
  121. // }
  122. // }}
  123. //
  124. // or use this macro for convenience:
  125. //
  126. // SERIALIZE_TYPE(UserType,
  127. // SERIALIZE_MEMBER(var)
  128. // )
  129. //
  130. // or to derive from the class Serializable and add their the members
  131. // in InitSerialization like the following:
  132. //
  133. // class UserType : public igl::Serializable {
  134. //
  135. // int var;
  136. //
  137. // void InitSerialization() {
  138. // this->Add(var,"var");
  139. // }
  140. // };
  141. // Base interface for user defined types
  142. struct SerializableBase
  143. {
  144. virtual ~SerializableBase() = default;
  145. virtual void Serialize(std::vector<char>& buffer) const = 0;
  146. virtual void Deserialize(const std::vector<char>& buffer) = 0;
  147. };
  148. // Convenient interface for user defined types
  149. class Serializable: public SerializableBase
  150. {
  151. private:
  152. template <typename T>
  153. struct SerializationObject : public SerializableBase
  154. {
  155. bool Binary;
  156. std::string Name;
  157. std::unique_ptr<T> Object;
  158. void Serialize(std::vector<char>& buffer) const override {
  159. igl::serialize(*Object,Name,buffer);
  160. }
  161. void Deserialize(const std::vector<char>& buffer) override {
  162. igl::deserialize(*Object,Name,buffer);
  163. }
  164. };
  165. mutable bool initialized;
  166. mutable std::vector<SerializableBase*> objects;
  167. public:
  168. // You **MUST** Override this function to add your member variables which
  169. // should be serialized
  170. //
  171. // http://stackoverflow.com/a/6634382/148668
  172. virtual void InitSerialization() = 0;
  173. // Following functions can be overridden to handle the specific events.
  174. // Return false to prevent the de-/serialization of an object.
  175. inline virtual bool PreSerialization() const;
  176. inline virtual void PostSerialization() const;
  177. inline virtual bool PreDeserialization();
  178. inline virtual void PostDeserialization();
  179. // Default implementation of SerializableBase interface
  180. inline void Serialize(std::vector<char>& buffer) const override final;
  181. inline void Deserialize(const std::vector<char>& buffer) override final;
  182. // Default constructor, destructor, assignment and copy constructor
  183. inline Serializable();
  184. inline Serializable(const Serializable& obj);
  185. virtual inline ~Serializable();
  186. inline Serializable& operator=(const Serializable& obj);
  187. // Use this function to add your variables which should be serialized
  188. template <typename T>
  189. inline void Add(T& obj,std::string name,bool binary = false);
  190. };
  191. // structure for pointer handling
  192. struct IndexedPointerBase
  193. {
  194. enum { BEGIN,END } Type;
  195. size_t Index;
  196. };
  197. template<typename T>
  198. struct IndexedPointer: public IndexedPointerBase
  199. {
  200. const T* Object;
  201. };
  202. // internal functions
  203. namespace serialization
  204. {
  205. // compile time type checks
  206. template <typename T>
  207. struct is_stl_container { static const bool value = false; };
  208. template <typename T1,typename T2>
  209. struct is_stl_container<std::pair<T1,T2> > { static const bool value = true; };
  210. template <typename T1,typename T2>
  211. struct is_stl_container<std::vector<T1,T2> > { static const bool value = true; };
  212. template <typename T>
  213. struct is_stl_container<std::set<T> > { static const bool value = true; };
  214. template <typename T1,typename T2>
  215. struct is_stl_container<std::map<T1,T2> > { static const bool value = true; };
  216. template <typename T>
  217. struct is_stl_container<std::list<T> > { static const bool value = true; };
  218. template <typename T>
  219. struct is_eigen_type { static const bool value = false; };
  220. template <typename T,int R,int C,int P,int MR,int MC>
  221. struct is_eigen_type<Eigen::Matrix<T,R,C,P,MR,MC> > { static const bool value = true; };
  222. template <typename T,int R,int C,int P,int MR,int MC>
  223. struct is_eigen_type<Eigen::Array<T,R,C,P,MR,MC> > { static const bool value = true; };
  224. template <typename T,int P,typename I>
  225. struct is_eigen_type<Eigen::SparseMatrix<T,P,I> > { static const bool value = true; };
  226. template <typename T>
  227. struct is_smart_ptr { static const bool value = false; };
  228. template <typename T>
  229. struct is_smart_ptr<std::shared_ptr<T> > { static const bool value = true; };
  230. template <typename T>
  231. struct is_smart_ptr<std::unique_ptr<T> > { static const bool value = true; };
  232. template <typename T>
  233. struct is_smart_ptr<std::weak_ptr<T> > { static const bool value = true; };
  234. template <typename T>
  235. struct is_serializable {
  236. static const bool value = std::is_fundamental<T>::value || std::is_same<std::string,T>::value || std::is_enum<T>::value || std::is_base_of<SerializableBase,T>::value
  237. || is_stl_container<T>::value || is_eigen_type<T>::value || std::is_pointer<T>::value || serialization::is_smart_ptr<T>::value;
  238. };
  239. // non serializable types
  240. template <typename T>
  241. inline typename std::enable_if<!is_serializable<T>::value,size_t>::type getByteSize(const T& obj);
  242. template <typename T>
  243. inline typename std::enable_if<!is_serializable<T>::value>::type serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  244. template <typename T>
  245. inline typename std::enable_if<!is_serializable<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter);
  246. // fundamental types
  247. template <typename T>
  248. inline typename std::enable_if<std::is_fundamental<T>::value,size_t>::type getByteSize(const T& obj);
  249. template <typename T>
  250. inline typename std::enable_if<std::is_fundamental<T>::value>::type serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  251. template <typename T>
  252. inline typename std::enable_if<std::is_fundamental<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter);
  253. // std::string
  254. inline size_t getByteSize(const std::string& obj);
  255. inline void serialize(const std::string& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  256. inline void deserialize(std::string& obj,std::vector<char>::const_iterator& iter);
  257. // enum types
  258. template <typename T>
  259. inline typename std::enable_if<std::is_enum<T>::value,size_t>::type getByteSize(const T& obj);
  260. template <typename T>
  261. inline typename std::enable_if<std::is_enum<T>::value>::type serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  262. template <typename T>
  263. inline typename std::enable_if<std::is_enum<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter);
  264. // SerializableBase
  265. template <typename T>
  266. inline typename std::enable_if<std::is_base_of<SerializableBase,T>::value,size_t>::type getByteSize(const T& obj);
  267. template <typename T>
  268. 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);
  269. template <typename T>
  270. inline typename std::enable_if<std::is_base_of<SerializableBase,T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter);
  271. // stl containers
  272. // std::pair
  273. template <typename T1,typename T2>
  274. inline size_t getByteSize(const std::pair<T1,T2>& obj);
  275. template <typename T1,typename T2>
  276. inline void serialize(const std::pair<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  277. template <typename T1,typename T2>
  278. inline void deserialize(std::pair<T1,T2>& obj,std::vector<char>::const_iterator& iter);
  279. // std::vector
  280. template <typename T1,typename T2>
  281. inline size_t getByteSize(const std::vector<T1,T2>& obj);
  282. template <typename T1,typename T2>
  283. inline void serialize(const std::vector<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  284. template <typename T1,typename T2>
  285. inline void deserialize(std::vector<T1,T2>& obj,std::vector<char>::const_iterator& iter);
  286. template <typename T2>
  287. inline void deserialize(std::vector<bool,T2>& obj,std::vector<char>::const_iterator& iter);
  288. // std::set
  289. template <typename T>
  290. inline size_t getByteSize(const std::set<T>& obj);
  291. template <typename T>
  292. inline void serialize(const std::set<T>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  293. template <typename T>
  294. inline void deserialize(std::set<T>& obj,std::vector<char>::const_iterator& iter);
  295. // std::map
  296. template <typename T1,typename T2>
  297. inline size_t getByteSize(const std::map<T1,T2>& obj);
  298. template <typename T1,typename T2>
  299. inline void serialize(const std::map<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  300. template <typename T1,typename T2>
  301. inline void deserialize(std::map<T1,T2>& obj,std::vector<char>::const_iterator& iter);
  302. // std::list
  303. template <typename T>
  304. inline size_t getByteSize(const std::list<T>& obj);
  305. template <typename T>
  306. inline void serialize(const std::list<T>& obj, std::vector<char>& buffer, std::vector<char>::iterator& iter);
  307. template <typename T>
  308. inline void deserialize(std::list<T>& obj, std::vector<char>::const_iterator& iter);
  309. // Eigen types
  310. template<typename T,int R,int C,int P,int MR,int MC>
  311. inline size_t getByteSize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj);
  312. template<typename T,int R,int C,int P,int MR,int MC>
  313. inline void serialize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  314. template<typename T,int R,int C,int P,int MR,int MC>
  315. inline void deserialize(Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>::const_iterator& iter);
  316. template<typename T,int R,int C,int P,int MR,int MC>
  317. inline size_t getByteSize(const Eigen::Array<T,R,C,P,MR,MC>& obj);
  318. template<typename T,int R,int C,int P,int MR,int MC>
  319. inline void serialize(const Eigen::Array<T,R,C,P,MR,MC>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  320. template<typename T,int R,int C,int P,int MR,int MC>
  321. inline void deserialize(Eigen::Array<T,R,C,P,MR,MC>& obj,std::vector<char>::const_iterator& iter);
  322. template<typename T,int P,typename I>
  323. inline size_t getByteSize(const Eigen::SparseMatrix<T,P,I>& obj);
  324. template<typename T,int P,typename I>
  325. inline void serialize(const Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  326. template<typename T,int P,typename I>
  327. inline void deserialize(Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>::const_iterator& iter);
  328. template<typename T,int P>
  329. inline size_t getByteSize(const Eigen::Quaternion<T,P>& obj);
  330. template<typename T,int P>
  331. inline void serialize(const Eigen::Quaternion<T,P>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  332. template<typename T,int P>
  333. inline void deserialize(Eigen::Quaternion<T,P>& obj,std::vector<char>::const_iterator& iter);
  334. // raw pointers
  335. template <typename T>
  336. inline typename std::enable_if<std::is_pointer<T>::value,size_t>::type getByteSize(const T& obj);
  337. template <typename T>
  338. inline typename std::enable_if<std::is_pointer<T>::value>::type serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  339. template <typename T>
  340. inline typename std::enable_if<std::is_pointer<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter);
  341. // std::shared_ptr and std::unique_ptr
  342. template <typename T>
  343. inline typename std::enable_if<serialization::is_smart_ptr<T>::value,size_t>::type getByteSize(const T& obj);
  344. template <typename T>
  345. inline typename std::enable_if<serialization::is_smart_ptr<T>::value>::type serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  346. template <template<typename> class T0, typename T1>
  347. inline typename std::enable_if<serialization::is_smart_ptr<T0<T1> >::value>::type deserialize(T0<T1>& obj,std::vector<char>::const_iterator& iter);
  348. // std::weak_ptr
  349. template <typename T>
  350. inline size_t getByteSize(const std::weak_ptr<T>& obj);
  351. template <typename T>
  352. inline void serialize(const std::weak_ptr<T>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
  353. template <typename T>
  354. inline void deserialize(std::weak_ptr<T>& obj,std::vector<char>::const_iterator& iter);
  355. // functions to overload for non-intrusive serialization
  356. template <typename T>
  357. inline void serialize(const T& obj,std::vector<char>& buffer);
  358. template <typename T>
  359. inline void deserialize(T& obj,const std::vector<char>& buffer);
  360. // helper functions
  361. template <typename T>
  362. inline void updateMemoryMap(T& obj,size_t size);
  363. }
  364. }
  365. // Always include inlines for these functions
  366. // IMPLEMENTATION
  367. namespace igl
  368. {
  369. template <typename T>
  370. inline bool serialize(const T& obj,const std::string& filename)
  371. {
  372. return serialize(obj,"obj",filename,true);
  373. }
  374. template <typename T>
  375. inline bool serialize(const T& obj,const std::string& objectName,const std::string& filename,bool overwrite)
  376. {
  377. bool success = false;
  378. std::vector<char> buffer;
  379. std::ios_base::openmode mode = std::ios::out | std::ios::binary;
  380. if(overwrite)
  381. mode |= std::ios::trunc;
  382. else
  383. mode |= std::ios::app;
  384. std::ofstream file(filename.c_str(),mode);
  385. if(file.is_open())
  386. {
  387. serialize(obj,objectName,buffer);
  388. file.write(&buffer[0],buffer.size());
  389. file.close();
  390. success = true;
  391. }
  392. else
  393. {
  394. std::cerr << "serialization: file " << filename << " not found!" << std::endl;
  395. }
  396. return success;
  397. }
  398. template <typename T>
  399. inline bool serialize(const T& obj,const std::string& objectName,std::vector<char>& buffer)
  400. {
  401. // serialize object data
  402. size_t size = serialization::getByteSize(obj);
  403. std::vector<char> tmp(size);
  404. auto it = tmp.begin();
  405. serialization::serialize(obj,tmp,it);
  406. std::string objectType(typeid(obj).name());
  407. size_t newObjectSize = tmp.size();
  408. size_t newHeaderSize = serialization::getByteSize(objectName) + serialization::getByteSize(objectType) + sizeof(size_t);
  409. size_t curSize = buffer.size();
  410. size_t newSize = curSize + newHeaderSize + newObjectSize;
  411. buffer.resize(newSize);
  412. std::vector<char>::iterator iter = buffer.begin()+curSize;
  413. // serialize object header (name/type/size)
  414. serialization::serialize(objectName,buffer,iter);
  415. serialization::serialize(objectType,buffer,iter);
  416. serialization::serialize(newObjectSize,buffer,iter);
  417. // copy serialized data to buffer
  418. iter = std::copy(tmp.begin(),tmp.end(),iter);
  419. return true;
  420. }
  421. template <typename T>
  422. inline bool deserialize(T& obj,const std::string& filename)
  423. {
  424. return deserialize(obj,"obj",filename);
  425. }
  426. template <typename T>
  427. inline bool deserialize(T& obj,const std::string& objectName,const std::string& filename)
  428. {
  429. bool success = false;
  430. std::ifstream file(filename.c_str(),std::ios::binary);
  431. if(file.is_open())
  432. {
  433. file.seekg(0,std::ios::end);
  434. std::streamoff size = file.tellg();
  435. file.seekg(0,std::ios::beg);
  436. std::vector<char> buffer(size);
  437. file.read(&buffer[0],size);
  438. success = deserialize(obj, objectName, buffer);
  439. file.close();
  440. }
  441. else
  442. {
  443. std::cerr << "serialization: file " << filename << " not found!" << std::endl;
  444. }
  445. return success;
  446. }
  447. template <typename T>
  448. inline bool deserialize(T& obj,const std::string& objectName,const std::vector<char>& buffer)
  449. {
  450. bool success = false;
  451. // find suitable object header
  452. auto objectIter = buffer.cend();
  453. auto iter = buffer.cbegin();
  454. while(iter != buffer.end())
  455. {
  456. std::string name;
  457. std::string type;
  458. size_t size;
  459. serialization::deserialize(name,iter);
  460. serialization::deserialize(type,iter);
  461. serialization::deserialize(size,iter);
  462. if(name == objectName && type == typeid(obj).name())
  463. {
  464. objectIter = iter;
  465. //break; // find first suitable object header
  466. }
  467. iter+=size;
  468. }
  469. if(objectIter != buffer.end())
  470. {
  471. serialization::deserialize(obj,objectIter);
  472. success = true;
  473. }
  474. else
  475. {
  476. obj = T();
  477. }
  478. return success;
  479. }
  480. // Wrapper function which combines both, de- and serialization
  481. template <typename T>
  482. inline bool serializer(bool s,T& obj,const std::string& filename)
  483. {
  484. return s ? serialize(obj,filename) : deserialize(obj,filename);
  485. }
  486. template <typename T>
  487. inline bool serializer(bool s,T& obj,const std::string& objectName,const std::string& filename,bool overwrite)
  488. {
  489. return s ? serialize(obj,objectName,filename,overwrite) : deserialize(obj,objectName,filename);
  490. }
  491. template <typename T>
  492. inline bool serializer(bool s,T& obj,const std::string& objectName,std::vector<char>& buffer)
  493. {
  494. return s ? serialize(obj,objectName,buffer) : deserialize(obj,objectName,buffer);
  495. }
  496. inline bool Serializable::PreSerialization() const
  497. {
  498. return true;
  499. }
  500. inline void Serializable::PostSerialization() const
  501. {
  502. }
  503. inline bool Serializable::PreDeserialization()
  504. {
  505. return true;
  506. }
  507. inline void Serializable::PostDeserialization()
  508. {
  509. }
  510. inline void Serializable::Serialize(std::vector<char>& buffer) const
  511. {
  512. if(this->PreSerialization())
  513. {
  514. if(initialized == false)
  515. {
  516. objects.clear();
  517. (const_cast<Serializable*>(this))->InitSerialization();
  518. initialized = true;
  519. }
  520. for(const auto& v : objects)
  521. {
  522. v->Serialize(buffer);
  523. }
  524. this->PostSerialization();
  525. }
  526. }
  527. inline void Serializable::Deserialize(const std::vector<char>& buffer)
  528. {
  529. if(this->PreDeserialization())
  530. {
  531. if(initialized == false)
  532. {
  533. objects.clear();
  534. (const_cast<Serializable*>(this))->InitSerialization();
  535. initialized = true;
  536. }
  537. for(auto& v : objects)
  538. {
  539. v->Deserialize(buffer);
  540. }
  541. this->PostDeserialization();
  542. }
  543. }
  544. inline Serializable::Serializable()
  545. {
  546. initialized = false;
  547. }
  548. inline Serializable::Serializable(const Serializable& /*obj*/)
  549. {
  550. initialized = false;
  551. objects.clear();
  552. }
  553. inline Serializable::~Serializable()
  554. {
  555. initialized = false;
  556. objects.clear();
  557. }
  558. inline Serializable& Serializable::operator=(const Serializable& obj)
  559. {
  560. if(this != &obj)
  561. {
  562. if(initialized)
  563. {
  564. initialized = false;
  565. objects.clear();
  566. }
  567. }
  568. return *this;
  569. }
  570. template <typename T>
  571. inline void Serializable::Add(T& obj,const std::string name,bool binary)
  572. {
  573. auto object = new SerializationObject<T>();
  574. object->Binary = binary;
  575. object->Name = name;
  576. object->Object = std::unique_ptr<T>(&obj);
  577. objects.push_back(object);
  578. }
  579. namespace serialization
  580. {
  581. template <typename T>
  582. inline typename std::enable_if<!is_serializable<T>::value,size_t>::type getByteSize(const T& /*obj*/)
  583. {
  584. return sizeof(std::vector<char>::size_type);
  585. }
  586. template <typename T>
  587. inline typename std::enable_if<!is_serializable<T>::value>::type serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
  588. {
  589. // data
  590. std::vector<char> tmp;
  591. serialize<>(obj,tmp);
  592. // size
  593. size_t size = buffer.size();
  594. serialization::serialize(tmp.size(),buffer,iter);
  595. size_t cur = iter - buffer.begin();
  596. buffer.resize(size+tmp.size());
  597. iter = buffer.begin()+cur;
  598. iter = std::copy(tmp.begin(),tmp.end(),iter);
  599. }
  600. template <typename T>
  601. inline typename std::enable_if<!is_serializable<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter)
  602. {
  603. std::vector<char>::size_type size;
  604. serialization::deserialize<>(size,iter);
  605. std::vector<char> tmp;
  606. tmp.resize(size);
  607. std::copy(iter,iter+size,tmp.begin());
  608. deserialize<>(obj,tmp);
  609. iter += size;
  610. }
  611. // fundamental types
  612. template <typename T>
  613. inline typename std::enable_if<std::is_fundamental<T>::value,size_t>::type getByteSize(const T& /*obj*/)
  614. {
  615. return sizeof(T);
  616. }
  617. template <typename T>
  618. inline typename std::enable_if<std::is_fundamental<T>::value>::type serialize(const T& obj,std::vector<char>& /*buffer*/,std::vector<char>::iterator& iter)
  619. {
  620. //serialization::updateMemoryMap(obj,sizeof(T));
  621. const std::uint8_t* ptr = reinterpret_cast<const std::uint8_t*>(&obj);
  622. iter = std::copy(ptr,ptr+sizeof(T),iter);
  623. }
  624. template <typename T>
  625. inline typename std::enable_if<std::is_fundamental<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter)
  626. {
  627. std::uint8_t* ptr = reinterpret_cast<std::uint8_t*>(&obj);
  628. std::copy(iter,iter+sizeof(T),ptr);
  629. iter += sizeof(T);
  630. }
  631. // std::string
  632. inline size_t getByteSize(const std::string& obj)
  633. {
  634. return getByteSize(obj.length())+obj.length()*sizeof(std::uint8_t);
  635. }
  636. inline void serialize(const std::string& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
  637. {
  638. serialization::serialize(obj.length(),buffer,iter);
  639. for(const auto& cur : obj)
  640. {
  641. serialization::serialize(cur,buffer,iter);
  642. }
  643. }
  644. inline void deserialize(std::string& obj,std::vector<char>::const_iterator& iter)
  645. {
  646. size_t size;
  647. serialization::deserialize(size,iter);
  648. std::string str(size,'\0');
  649. for(size_t i=0; i<size; ++i)
  650. {
  651. serialization::deserialize(str.at(i),iter);
  652. }
  653. obj = str;
  654. }
  655. // enum types
  656. template <typename T>
  657. inline typename std::enable_if<std::is_enum<T>::value,size_t>::type getByteSize(const T& /*obj*/)
  658. {
  659. return sizeof(T);
  660. }
  661. template <typename T>
  662. inline typename std::enable_if<std::is_enum<T>::value>::type serialize(const T& obj,std::vector<char>& /*buffer*/,std::vector<char>::iterator& iter)
  663. {
  664. const std::uint8_t* ptr = reinterpret_cast<const std::uint8_t*>(&obj);
  665. iter = std::copy(ptr,ptr+sizeof(T),iter);
  666. }
  667. template <typename T>
  668. inline typename std::enable_if<std::is_enum<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter)
  669. {
  670. std::uint8_t* ptr = reinterpret_cast<std::uint8_t*>(&obj);
  671. std::copy(iter,iter+sizeof(T),ptr);
  672. iter += sizeof(T);
  673. }
  674. // SerializableBase
  675. template <typename T>
  676. inline typename std::enable_if<std::is_base_of<SerializableBase,T>::value,size_t>::type getByteSize(const T& /*obj*/)
  677. {
  678. return sizeof(std::vector<char>::size_type);
  679. }
  680. template <typename T>
  681. 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)
  682. {
  683. // data
  684. std::vector<char> tmp;
  685. obj.Serialize(tmp);
  686. // size
  687. size_t size = buffer.size();
  688. serialization::serialize(tmp.size(),buffer,iter);
  689. size_t cur = iter - buffer.begin();
  690. buffer.resize(size+tmp.size());
  691. iter = buffer.begin()+cur;
  692. iter = std::copy(tmp.begin(),tmp.end(),iter);
  693. }
  694. template <typename T>
  695. inline typename std::enable_if<std::is_base_of<SerializableBase,T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter)
  696. {
  697. std::vector<char>::size_type size;
  698. serialization::deserialize(size,iter);
  699. std::vector<char> tmp;
  700. tmp.resize(size);
  701. std::copy(iter,iter+size,tmp.begin());
  702. obj.Deserialize(tmp);
  703. iter += size;
  704. }
  705. // STL containers
  706. // std::pair
  707. template <typename T1,typename T2>
  708. inline size_t getByteSize(const std::pair<T1,T2>& obj)
  709. {
  710. return getByteSize(obj.first)+getByteSize(obj.second);
  711. }
  712. template <typename T1,typename T2>
  713. inline void serialize(const std::pair<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
  714. {
  715. serialization::serialize(obj.first,buffer,iter);
  716. serialization::serialize(obj.second,buffer,iter);
  717. }
  718. template <typename T1,typename T2>
  719. inline void deserialize(std::pair<T1,T2>& obj,std::vector<char>::const_iterator& iter)
  720. {
  721. serialization::deserialize(obj.first,iter);
  722. serialization::deserialize(obj.second,iter);
  723. }
  724. // std::vector
  725. template <typename T1,typename T2>
  726. inline size_t getByteSize(const std::vector<T1,T2>& obj)
  727. {
  728. return std::accumulate(obj.begin(),obj.end(),sizeof(size_t),[](const size_t& acc,const T1& cur) { return acc+getByteSize(cur); });
  729. }
  730. template <typename T1,typename T2>
  731. inline void serialize(const std::vector<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
  732. {
  733. size_t size = obj.size();
  734. serialization::serialize(size,buffer,iter);
  735. for(const T1& cur : obj)
  736. {
  737. serialization::serialize(cur,buffer,iter);
  738. }
  739. }
  740. template <typename T1,typename T2>
  741. inline void deserialize(std::vector<T1,T2>& obj,std::vector<char>::const_iterator& iter)
  742. {
  743. size_t size;
  744. serialization::deserialize(size,iter);
  745. obj.resize(size);
  746. for(T1& v : obj)
  747. {
  748. serialization::deserialize(v,iter);
  749. }
  750. }
  751. template <typename T2>
  752. inline void deserialize(std::vector<bool,T2>& obj,std::vector<char>::const_iterator& iter)
  753. {
  754. size_t size;
  755. serialization::deserialize(size,iter);
  756. obj.resize(size);
  757. for(int i=0;i<obj.size();i++)
  758. {
  759. bool val;
  760. serialization::deserialize(val,iter);
  761. obj[i] = val;
  762. }
  763. }
  764. //std::set
  765. template <typename T>
  766. inline size_t getByteSize(const std::set<T>& obj)
  767. {
  768. return std::accumulate(obj.begin(),obj.end(),getByteSize(obj.size()),[](const size_t& acc,const T& cur) { return acc+getByteSize(cur); });
  769. }
  770. template <typename T>
  771. inline void serialize(const std::set<T>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
  772. {
  773. serialization::serialize(obj.size(),buffer,iter);
  774. for(const T& cur : obj)
  775. {
  776. serialization::serialize(cur,buffer,iter);
  777. }
  778. }
  779. template <typename T>
  780. inline void deserialize(std::set<T>& obj,std::vector<char>::const_iterator& iter)
  781. {
  782. size_t size;
  783. serialization::deserialize(size,iter);
  784. obj.clear();
  785. for(size_t i=0; i<size; ++i)
  786. {
  787. T val;
  788. serialization::deserialize(val,iter);
  789. obj.insert(val);
  790. }
  791. }
  792. // std::map
  793. template <typename T1,typename T2>
  794. inline size_t getByteSize(const std::map<T1,T2>& obj)
  795. {
  796. return std::accumulate(obj.begin(),obj.end(),sizeof(size_t),[](const size_t& acc,const std::pair<T1,T2>& cur) { return acc+getByteSize(cur); });
  797. }
  798. template <typename T1,typename T2>
  799. inline void serialize(const std::map<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
  800. {
  801. serialization::serialize(obj.size(),buffer,iter);
  802. for(const auto& cur : obj)
  803. {
  804. serialization::serialize(cur,buffer,iter);
  805. }
  806. }
  807. template <typename T1,typename T2>
  808. inline void deserialize(std::map<T1,T2>& obj,std::vector<char>::const_iterator& iter)
  809. {
  810. size_t size;
  811. serialization::deserialize(size,iter);
  812. obj.clear();
  813. for(size_t i=0; i<size; ++i)
  814. {
  815. std::pair<T1,T2> pair;
  816. serialization::deserialize(pair,iter);
  817. obj.insert(pair);
  818. }
  819. }
  820. //std::list
  821. template <typename T>
  822. inline size_t getByteSize(const std::list<T>& obj)
  823. {
  824. return std::accumulate(obj.begin(), obj.end(), getByteSize(obj.size()), [](const size_t& acc, const T& cur) { return acc + getByteSize(cur); });
  825. }
  826. template <typename T>
  827. inline void serialize(const std::list<T>& obj, std::vector<char>& buffer, std::vector<char>::iterator& iter)
  828. {
  829. serialization::serialize(obj.size(), buffer, iter);
  830. for (const T& cur : obj)
  831. {
  832. serialization::serialize(cur, buffer, iter);
  833. }
  834. }
  835. template <typename T>
  836. inline void deserialize(std::list<T>& obj, std::vector<char>::const_iterator& iter)
  837. {
  838. size_t size;
  839. serialization::deserialize(size, iter);
  840. obj.clear();
  841. for (size_t i = 0; i < size; ++i)
  842. {
  843. T val;
  844. serialization::deserialize(val, iter);
  845. obj.emplace_back(val);
  846. }
  847. }
  848. // Eigen types
  849. template<typename T,int R,int C,int P,int MR,int MC>
  850. inline size_t getByteSize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj)
  851. {
  852. // space for numbers of rows,cols and data
  853. return 2*sizeof(typename Eigen::Matrix<T,R,C,P,MR,MC>::Index)+sizeof(T)*obj.rows()*obj.cols();
  854. }
  855. template<typename T,int R,int C,int P,int MR,int MC>
  856. inline void serialize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
  857. {
  858. serialization::serialize(obj.rows(),buffer,iter);
  859. serialization::serialize(obj.cols(),buffer,iter);
  860. size_t size = sizeof(T)*obj.rows()*obj.cols();
  861. auto ptr = reinterpret_cast<const std::uint8_t*>(obj.data());
  862. iter = std::copy(ptr,ptr+size,iter);
  863. }
  864. template<typename T,int R,int C,int P,int MR,int MC>
  865. inline void deserialize(Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>::const_iterator& iter)
  866. {
  867. typename Eigen::Matrix<T,R,C,P,MR,MC>::Index rows,cols;
  868. serialization::deserialize(rows,iter);
  869. serialization::deserialize(cols,iter);
  870. size_t size = sizeof(T)*rows*cols;
  871. obj.resize(rows,cols);
  872. auto ptr = reinterpret_cast<std::uint8_t*>(obj.data());
  873. std::copy(iter,iter+size,ptr);
  874. iter+=size;
  875. }
  876. template<typename T,int R,int C,int P,int MR,int MC>
  877. inline size_t getByteSize(const Eigen::Array<T,R,C,P,MR,MC>& obj)
  878. {
  879. // space for numbers of rows,cols and data
  880. return 2*sizeof(typename Eigen::Array<T,R,C,P,MR,MC>::Index)+sizeof(T)*obj.rows()*obj.cols();
  881. }
  882. template<typename T,int R,int C,int P,int MR,int MC>
  883. inline void serialize(const Eigen::Array<T,R,C,P,MR,MC>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
  884. {
  885. serialization::serialize(obj.rows(),buffer,iter);
  886. serialization::serialize(obj.cols(),buffer,iter);
  887. size_t size = sizeof(T)*obj.rows()*obj.cols();
  888. auto ptr = reinterpret_cast<const std::uint8_t*>(obj.data());
  889. iter = std::copy(ptr,ptr+size,iter);
  890. }
  891. template<typename T,int R,int C,int P,int MR,int MC>
  892. inline void deserialize(Eigen::Array<T,R,C,P,MR,MC>& obj,std::vector<char>::const_iterator& iter)
  893. {
  894. typename Eigen::Array<T,R,C,P,MR,MC>::Index rows,cols;
  895. serialization::deserialize(rows,iter);
  896. serialization::deserialize(cols,iter);
  897. size_t size = sizeof(T)*rows*cols;
  898. obj.resize(rows,cols);
  899. auto ptr = reinterpret_cast<std::uint8_t*>(obj.data());
  900. std::copy(iter,iter+size,ptr);
  901. iter+=size;
  902. }
  903. template<typename T,int P,typename I>
  904. inline size_t getByteSize(const Eigen::SparseMatrix<T,P,I>& obj)
  905. {
  906. // space for numbers of rows,cols,nonZeros and tripplets with data (rowIdx,colIdx,value)
  907. size_t size = sizeof(typename Eigen::SparseMatrix<T,P,I>::Index);
  908. return 3*size+(sizeof(T)+2*size)*obj.nonZeros();
  909. }
  910. template<typename T,int P,typename I>
  911. inline void serialize(const Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
  912. {
  913. serialization::serialize(obj.rows(),buffer,iter);
  914. serialization::serialize(obj.cols(),buffer,iter);
  915. serialization::serialize(obj.nonZeros(),buffer,iter);
  916. for(int k=0;k<obj.outerSize();++k)
  917. {
  918. for(typename Eigen::SparseMatrix<T,P,I>::InnerIterator it(obj,k);it;++it)
  919. {
  920. serialization::serialize(it.row(),buffer,iter);
  921. serialization::serialize(it.col(),buffer,iter);
  922. serialization::serialize(it.value(),buffer,iter);
  923. }
  924. }
  925. }
  926. template<typename T,int P,typename I>
  927. inline void deserialize(Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>::const_iterator& iter)
  928. {
  929. typename Eigen::SparseMatrix<T,P,I>::Index rows,cols,nonZeros;
  930. serialization::deserialize(rows,iter);
  931. serialization::deserialize(cols,iter);
  932. serialization::deserialize(nonZeros,iter);
  933. obj.resize(rows,cols);
  934. obj.setZero();
  935. std::vector<Eigen::Triplet<T,I> > triplets;
  936. for(int i=0;i<nonZeros;i++)
  937. {
  938. typename Eigen::SparseMatrix<T,P,I>::Index rowId,colId;
  939. serialization::deserialize(rowId,iter);
  940. serialization::deserialize(colId,iter);
  941. T value;
  942. serialization::deserialize(value,iter);
  943. triplets.push_back(Eigen::Triplet<T,I>(rowId,colId,value));
  944. }
  945. obj.setFromTriplets(triplets.begin(),triplets.end());
  946. }
  947. template<typename T,int P>
  948. inline size_t getByteSize(const Eigen::Quaternion<T,P>& /*obj*/)
  949. {
  950. return sizeof(T)*4;
  951. }
  952. template<typename T,int P>
  953. inline void serialize(const Eigen::Quaternion<T,P>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
  954. {
  955. serialization::serialize(obj.w(),buffer,iter);
  956. serialization::serialize(obj.x(),buffer,iter);
  957. serialization::serialize(obj.y(),buffer,iter);
  958. serialization::serialize(obj.z(),buffer,iter);
  959. }
  960. template<typename T,int P>
  961. inline void deserialize(Eigen::Quaternion<T,P>& obj,std::vector<char>::const_iterator& iter)
  962. {
  963. serialization::deserialize(obj.w(),iter);
  964. serialization::deserialize(obj.x(),iter);
  965. serialization::deserialize(obj.y(),iter);
  966. serialization::deserialize(obj.z(),iter);
  967. }
  968. // pointers
  969. template <typename T>
  970. inline typename std::enable_if<std::is_pointer<T>::value,size_t>::type getByteSize(const T& obj)
  971. {
  972. size_t size = sizeof(bool);
  973. if(obj)
  974. size += getByteSize(*obj);
  975. return size;
  976. }
  977. template <typename T>
  978. inline typename std::enable_if<std::is_pointer<T>::value>::type serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
  979. {
  980. serialization::serialize(obj == nullptr,buffer,iter);
  981. if(obj)
  982. serialization::serialize(*obj,buffer,iter);
  983. }
  984. template <typename T>
  985. inline typename std::enable_if<std::is_pointer<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter)
  986. {
  987. bool isNullPtr;
  988. serialization::deserialize(isNullPtr,iter);
  989. if(isNullPtr)
  990. {
  991. if(obj)
  992. {
  993. std::cout << "serialization: possible memory leak in serialization for '" << typeid(obj).name() << "'" << std::endl;
  994. obj = nullptr;
  995. }
  996. }
  997. else
  998. {
  999. if(obj)
  1000. {
  1001. std::cout << "serialization: possible memory corruption in deserialization for '" << typeid(obj).name() << "'" << std::endl;
  1002. }
  1003. else
  1004. {
  1005. obj = new typename std::remove_pointer<T>::type();
  1006. }
  1007. serialization::deserialize(*obj,iter);
  1008. }
  1009. }
  1010. // std::shared_ptr and std::unique_ptr
  1011. template <typename T>
  1012. inline typename std::enable_if<serialization::is_smart_ptr<T>::value,size_t>::type getByteSize(const T& obj)
  1013. {
  1014. return getByteSize(obj.get());
  1015. }
  1016. template <typename T>
  1017. inline typename std::enable_if<serialization::is_smart_ptr<T>::value>::type serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
  1018. {
  1019. serialize(obj.get(),buffer,iter);
  1020. }
  1021. template <template<typename> class T0,typename T1>
  1022. inline typename std::enable_if<serialization::is_smart_ptr<T0<T1> >::value>::type deserialize(T0<T1>& obj,std::vector<char>::const_iterator& iter)
  1023. {
  1024. bool isNullPtr;
  1025. serialization::deserialize(isNullPtr,iter);
  1026. if(isNullPtr)
  1027. {
  1028. obj.reset();
  1029. }
  1030. else
  1031. {
  1032. obj = T0<T1>(new T1());
  1033. serialization::deserialize(*obj,iter);
  1034. }
  1035. }
  1036. // std::weak_ptr
  1037. template <typename T>
  1038. inline size_t getByteSize(const std::weak_ptr<T>& /*obj*/)
  1039. {
  1040. return sizeof(size_t);
  1041. }
  1042. template <typename T>
  1043. inline void serialize(const std::weak_ptr<T>& /*obj*/,std::vector<char>& /*buffer*/,std::vector<char>::iterator& /*iter*/)
  1044. {
  1045. }
  1046. template <typename T>
  1047. inline void deserialize(std::weak_ptr<T>& /*obj*/,std::vector<char>::const_iterator& /*iter*/)
  1048. {
  1049. }
  1050. // functions to overload for non-intrusive serialization
  1051. template <typename T>
  1052. inline void serialize(const T& obj,std::vector<char>& /*buffer*/)
  1053. {
  1054. std::cerr << typeid(obj).name() << " is not serializable: derive from igl::Serializable or specialize the template function igl::serialization::serialize(const T& obj,std::vector<char>& buffer)" << std::endl;
  1055. }
  1056. template <typename T>
  1057. inline void deserialize(T& obj,const std::vector<char>& /*buffer*/)
  1058. {
  1059. std::cerr << typeid(obj).name() << " is not deserializable: derive from igl::Serializable or specialize the template function igl::serialization::deserialize(T& obj, const std::vector<char>& buffer)" << std::endl;
  1060. }
  1061. // helper functions
  1062. template <typename T>
  1063. inline void updateMemoryMap(T& obj,size_t size,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  1064. {
  1065. // check if object is already serialized
  1066. auto startPtr = new IndexedPointer<T>();
  1067. startPtr->Object = &obj;
  1068. auto startBasePtr = static_cast<IndexedPointerBase*>(startPtr);
  1069. startBasePtr->Type = IndexedPointerBase::BEGIN;
  1070. auto startAddress = reinterpret_cast<std::uintptr_t>(&obj);
  1071. auto p = std::pair<std::uintptr_t,IndexedPointerBase*>(startAddress,startBasePtr);
  1072. auto el = memoryMap.insert(p);
  1073. auto iter = ++el.first; // next elememt
  1074. if(el.second && (iter == memoryMap.end() || iter->second->Type != IndexedPointerBase::END))
  1075. {
  1076. // not yet serialized
  1077. auto endPtr = new IndexedPointer<T>();
  1078. auto endBasePtr = static_cast<IndexedPointerBase*>(endPtr);
  1079. endBasePtr->Type = IndexedPointerBase::END;
  1080. auto endAddress = reinterpret_cast<std::uintptr_t>(&obj) + size - 1;
  1081. auto p = std::pair<std::uintptr_t,IndexedPointerBase*>(endAddress,endBasePtr);
  1082. // insert end address
  1083. memoryMap.insert(el.first,p);
  1084. }
  1085. else
  1086. {
  1087. // already serialized
  1088. // remove inserted address
  1089. memoryMap.erase(el.first);
  1090. }
  1091. }
  1092. }
  1093. }
  1094. #endif