serialize.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. #include "serialize.h"
  8. namespace igl
  9. {
  10. template <typename T>
  11. IGL_INLINE bool serialize(const T& obj,const std::string& filename)
  12. {
  13. return serialize(obj,"obj",filename,true);
  14. }
  15. template <typename T>
  16. IGL_INLINE bool serialize(const T& obj,const std::string& objectName,const std::string& filename,bool overwrite)
  17. {
  18. bool success = false;
  19. std::vector<char> buffer;
  20. std::ios_base::openmode mode = std::ios::out | std::ios::binary;
  21. if(overwrite)
  22. mode |= std::ios::trunc;
  23. else
  24. mode |= std::ios::app;
  25. std::ofstream file(filename.c_str(),mode);
  26. if(file.is_open())
  27. {
  28. serialize(obj,objectName,buffer);
  29. file.write(&buffer[0],buffer.size());
  30. file.close();
  31. success = true;
  32. }
  33. else
  34. {
  35. std::cerr << "serialization: file " << filename << " not found!" << std::endl;
  36. }
  37. return success;
  38. }
  39. template <typename T>
  40. IGL_INLINE bool serialize(const T& obj,const std::string& objectName,std::vector<char>& buffer)
  41. {
  42. std::map<std::uintptr_t,IndexedPointerBase*> handler;
  43. return serialize(obj,objectName,buffer,0,handler);
  44. }
  45. template <typename T>
  46. IGL_INLINE bool serialize(const T& obj,const std::string& objectName,std::vector<char>& buffer,size_t offset,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  47. {
  48. // serialize object data
  49. size_t size = serialization::getByteSize(obj);
  50. std::vector<char> tmp(size);
  51. auto it = tmp.begin();
  52. serialization::serialize(obj,tmp,it,memoryMap);
  53. std::string objectType(typeid(obj).name());
  54. size_t newObjectSize = tmp.size();
  55. size_t newHeaderSize = serialization::getByteSize(objectName) + serialization::getByteSize(objectType) + sizeof(size_t);
  56. size_t curSize = buffer.size();
  57. size_t newSize = curSize + newHeaderSize + newObjectSize;
  58. buffer.resize(newSize);
  59. std::vector<char>::iterator iter = buffer.begin()+curSize;
  60. // serialize object header (name/type/size)
  61. serialization::serialize(objectName,buffer,iter,memoryMap);
  62. serialization::serialize(objectType,buffer,iter,memoryMap);
  63. serialization::serialize(newObjectSize,buffer,iter,memoryMap);
  64. // copy serialized data to buffer
  65. iter = std::copy(tmp.begin(),tmp.end(),iter);
  66. return true;
  67. }
  68. template <typename T>
  69. IGL_INLINE bool deserialize(T& obj,const std::string& filename)
  70. {
  71. return deserialize(obj,"obj",filename);
  72. }
  73. template <typename T>
  74. IGL_INLINE bool deserialize(T& obj,const std::string& objectName,const std::string& filename)
  75. {
  76. bool success = false;
  77. std::ifstream file(filename.c_str(),std::ios::binary);
  78. if(file.is_open())
  79. {
  80. file.seekg(0,std::ios::end);
  81. std::streamoff size = file.tellg();
  82. file.seekg(0,std::ios::beg);
  83. std::vector<char> buffer(size);
  84. file.read(&buffer[0],size);
  85. deserialize(obj,objectName,buffer);
  86. file.close();
  87. success = true;
  88. }
  89. else
  90. {
  91. std::cerr << "serialization: file " << filename << " not found!" << std::endl;
  92. }
  93. return success;
  94. }
  95. template <typename T>
  96. IGL_INLINE bool deserialize(T& obj,const std::string& objectName,const std::vector<char>& buffer)
  97. {
  98. bool success = false;
  99. // find suitable object header
  100. auto objectIter = buffer.cend();
  101. auto iter = buffer.cbegin();
  102. while(iter != buffer.end())
  103. {
  104. std::string name;
  105. std::string type;
  106. size_t size;
  107. serialization::deserialize(name,iter);
  108. serialization::deserialize(type,iter);
  109. serialization::deserialize(size,iter);
  110. if(name == objectName && type == typeid(obj).name())
  111. {
  112. objectIter = iter;
  113. //break; // find first suitable object header
  114. }
  115. iter+=size;
  116. }
  117. if(objectIter != buffer.end())
  118. {
  119. serialization::deserialize(obj,objectIter);
  120. success = true;
  121. }
  122. else
  123. {
  124. obj = T();
  125. }
  126. return success;
  127. }
  128. // Wrapper function which combines both, de- and serialization
  129. template <typename T>
  130. IGL_INLINE bool serializer(bool s,T& obj,std::string& filename)
  131. {
  132. return s ? serialize(obj,filename) : deserialize(obj,filename);
  133. }
  134. template <typename T>
  135. IGL_INLINE bool serializer(bool s,T& obj,std::string& objectName,const std::string& filename,bool overwrite)
  136. {
  137. return s ? serialize(obj,objectName,filename,overwrite) : deserialize(obj,objectName,filename);
  138. }
  139. template <typename T>
  140. IGL_INLINE bool serializer(bool s,T& obj,std::string& objectName,std::vector<char>& buffer)
  141. {
  142. return s ? serialize(obj,objectName,buffer) : deserialize(obj,objectName,buffer);
  143. }
  144. IGL_INLINE bool Serializable::PreSerialization() const
  145. {
  146. return true;
  147. }
  148. IGL_INLINE void Serializable::PostSerialization() const
  149. {
  150. }
  151. IGL_INLINE bool Serializable::PreDeserialization()
  152. {
  153. return true;
  154. }
  155. IGL_INLINE void Serializable::PostDeserialization()
  156. {
  157. }
  158. IGL_INLINE void Serializable::Serialize(std::vector<char>& buffer) const
  159. {
  160. if(this->PreSerialization())
  161. {
  162. if(initialized == false)
  163. {
  164. objects.clear();
  165. (const_cast<Serializable*>(this))->InitSerialization();
  166. initialized = true;
  167. }
  168. for(const auto& v : objects)
  169. {
  170. v->Serialize(buffer);
  171. }
  172. this->PostSerialization();
  173. }
  174. }
  175. IGL_INLINE void Serializable::Deserialize(const std::vector<char>& buffer)
  176. {
  177. if(this->PreDeserialization())
  178. {
  179. if(initialized == false)
  180. {
  181. objects.clear();
  182. (const_cast<Serializable*>(this))->InitSerialization();
  183. initialized = true;
  184. }
  185. for(auto& v : objects)
  186. {
  187. v->Deserialize(buffer);
  188. }
  189. this->PostDeserialization();
  190. }
  191. }
  192. IGL_INLINE Serializable::Serializable()
  193. {
  194. initialized = false;
  195. }
  196. IGL_INLINE Serializable::Serializable(const Serializable& obj)
  197. {
  198. initialized = false;
  199. objects.clear();
  200. }
  201. IGL_INLINE Serializable::~Serializable()
  202. {
  203. initialized = false;
  204. objects.clear();
  205. }
  206. IGL_INLINE Serializable& Serializable::operator=(const Serializable& obj)
  207. {
  208. if(this != &obj)
  209. {
  210. if(initialized)
  211. {
  212. initialized = false;
  213. objects.clear();
  214. }
  215. }
  216. return *this;
  217. }
  218. template <typename T>
  219. IGL_INLINE void Serializable::Add(T& obj,std::string name,bool binary)
  220. {
  221. auto object = new SerializationObject<T>();
  222. object->Binary = binary;
  223. object->Name = name;
  224. object->Object = std::unique_ptr<T>(&obj);
  225. objects.push_back(object);
  226. }
  227. namespace serialization
  228. {
  229. // not serializable
  230. template <typename T>
  231. IGL_INLINE typename std::enable_if<!is_serializable<T>::value,size_t>::type getByteSize(const T& obj)
  232. {
  233. return sizeof(std::vector<char>::size_type);
  234. }
  235. template <typename T>
  236. IGL_INLINE typename std::enable_if<!is_serializable<T>::value>::type serialize(const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  237. {
  238. // data
  239. std::vector<char> tmp;
  240. serialize(obj,tmp);
  241. // size
  242. size_t size = buffer.size();
  243. serialization::serialize(tmp.size(),buffer,iter,memoryMap);
  244. size_t cur = iter - buffer.begin();
  245. buffer.resize(size+tmp.size());
  246. iter = buffer.begin()+cur;
  247. iter = std::copy(tmp.begin(),tmp.end(),iter);
  248. }
  249. template <typename T>
  250. IGL_INLINE typename std::enable_if<!is_serializable<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter)
  251. {
  252. std::vector<char>::size_type size;
  253. serialization::deserialize(size,iter);
  254. std::vector<char> tmp;
  255. tmp.resize(size);
  256. std::copy(iter,iter+size,tmp.begin());
  257. deserialize(obj,tmp);
  258. iter += size;
  259. }
  260. // fundamental types
  261. template <typename T>
  262. IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value,size_t>::type getByteSize(const T& obj)
  263. {
  264. return sizeof(T);
  265. }
  266. template <typename T>
  267. 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,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  268. {
  269. //serialization::updateMemoryMap(obj,sizeof(T),memoryMap);
  270. const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&obj);
  271. iter = std::copy(ptr,ptr+sizeof(T),iter);
  272. }
  273. template <typename T>
  274. IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter)
  275. {
  276. uint8_t* ptr = reinterpret_cast<uint8_t*>(&obj);
  277. std::copy(iter,iter+sizeof(T),ptr);
  278. iter += sizeof(T);
  279. }
  280. // std::string
  281. IGL_INLINE size_t getByteSize(const std::string& obj)
  282. {
  283. return getByteSize(obj.length())+obj.length()*sizeof(uint8_t);
  284. }
  285. IGL_INLINE void serialize(const std::string& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  286. {
  287. serialization::serialize(obj.length(),buffer,iter,memoryMap);
  288. for(const auto& cur : obj)
  289. {
  290. serialization::serialize(cur,buffer,iter,memoryMap);
  291. }
  292. }
  293. IGL_INLINE void deserialize(std::string& obj,std::vector<char>::const_iterator& iter)
  294. {
  295. size_t size;
  296. serialization::deserialize(size,iter);
  297. std::string str(size,'\0');
  298. for(size_t i=0; i<size; ++i)
  299. {
  300. serialization::deserialize(str.at(i),iter);
  301. }
  302. obj = str;
  303. }
  304. // SerializableBase
  305. template <typename T>
  306. IGL_INLINE typename std::enable_if<std::is_base_of<SerializableBase,T>::value,size_t>::type getByteSize(const T& obj)
  307. {
  308. return sizeof(std::vector<char>::size_type);
  309. }
  310. template <typename T>
  311. 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,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  312. {
  313. // data
  314. std::vector<char> tmp;
  315. obj.Serialize(tmp);
  316. // size
  317. size_t size = buffer.size();
  318. serialization::serialize(tmp.size(),buffer,iter,memoryMap);
  319. size_t cur = iter - buffer.begin();
  320. buffer.resize(size+tmp.size());
  321. iter = buffer.begin()+cur;
  322. iter = std::copy(tmp.begin(),tmp.end(),iter);
  323. }
  324. template <typename T>
  325. IGL_INLINE typename std::enable_if<std::is_base_of<SerializableBase,T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter)
  326. {
  327. std::vector<char>::size_type size;
  328. serialization::deserialize(size,iter);
  329. std::vector<char> tmp;
  330. tmp.resize(size);
  331. std::copy(iter,iter+size,tmp.begin());
  332. obj.Deserialize(tmp);
  333. iter += size;
  334. }
  335. // STL containers
  336. // std::pair
  337. template <typename T1,typename T2>
  338. IGL_INLINE size_t getByteSize(const std::pair<T1,T2>& obj)
  339. {
  340. return getByteSize(obj.first)+getByteSize(obj.second);
  341. }
  342. template <typename T1,typename T2>
  343. IGL_INLINE void serialize(const std::pair<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  344. {
  345. serialization::serialize(obj.first,buffer,iter,memoryMap);
  346. serialization::serialize(obj.second,buffer,iter,memoryMap);
  347. }
  348. template <typename T1,typename T2>
  349. IGL_INLINE void deserialize(std::pair<T1,T2>& obj,std::vector<char>::const_iterator& iter)
  350. {
  351. serialization::deserialize(obj.first,iter);
  352. serialization::deserialize(obj.second,iter);
  353. }
  354. // std::vector
  355. template <typename T1,typename T2>
  356. IGL_INLINE size_t getByteSize(const std::vector<T1,T2>& obj)
  357. {
  358. return std::accumulate(obj.begin(),obj.end(),sizeof(size_t),[](const size_t& acc,const T1& cur) { return acc+getByteSize(cur); });
  359. }
  360. template <typename T1,typename T2>
  361. IGL_INLINE void serialize(const std::vector<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  362. {
  363. size_t size = obj.size();
  364. serialization::serialize(size,buffer,iter,memoryMap);
  365. for(const auto& cur : obj)
  366. {
  367. serialization::serialize(cur,buffer,iter,memoryMap);
  368. }
  369. }
  370. template <typename T1,typename T2>
  371. IGL_INLINE void deserialize(std::vector<T1,T2>& obj,std::vector<char>::const_iterator& iter)
  372. {
  373. size_t size;
  374. serialization::deserialize(size,iter);
  375. obj.resize(size);
  376. for(auto& v : obj)
  377. {
  378. serialization::deserialize(v,iter);
  379. }
  380. }
  381. //std::set
  382. template <typename T>
  383. IGL_INLINE size_t getByteSize(const std::set<T>& obj)
  384. {
  385. return std::accumulate(obj.begin(),obj.end(),getByteSize(obj.size()),[](const size_t& acc,const T& cur) { return acc+getByteSize(cur); });
  386. }
  387. template <typename T>
  388. IGL_INLINE void serialize(const std::set<T>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  389. {
  390. serialization::serialize(obj.size(),buffer,iter,memoryMap);
  391. for(const auto& cur : obj)
  392. {
  393. serialization::serialize(cur,buffer,iter,memoryMap);
  394. }
  395. }
  396. template <typename T>
  397. IGL_INLINE void deserialize(std::set<T>& obj,std::vector<char>::const_iterator& iter)
  398. {
  399. size_t size;
  400. serialization::deserialize(size,iter);
  401. obj.clear();
  402. for(size_t i=0; i<size; ++i)
  403. {
  404. T val;
  405. serialization::deserialize(val,iter);
  406. obj.insert(val);
  407. }
  408. }
  409. // std::map
  410. template <typename T1,typename T2>
  411. IGL_INLINE size_t getByteSize(const std::map<T1,T2>& obj)
  412. {
  413. return std::accumulate(obj.begin(),obj.end(),sizeof(size_t),[](const size_t& acc,const std::pair<T1,T2>& cur) { return acc+getByteSize(cur); });
  414. }
  415. template <typename T1,typename T2>
  416. IGL_INLINE void serialize(const std::map<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  417. {
  418. serialization::serialize(obj.size(),buffer,iter,memoryMap);
  419. for(const auto& cur : obj)
  420. {
  421. serialization::serialize(cur,buffer,iter,memoryMap);
  422. }
  423. }
  424. template <typename T1,typename T2>
  425. IGL_INLINE void deserialize(std::map<T1,T2>& obj,std::vector<char>::const_iterator& iter)
  426. {
  427. size_t size;
  428. serialization::deserialize(size,iter);
  429. obj.clear();
  430. for(size_t i=0; i<size; ++i)
  431. {
  432. std::pair<T1,T2> pair;
  433. serialization::deserialize(pair,iter);
  434. obj.insert(pair);
  435. }
  436. }
  437. // Eigen types
  438. template<typename T,int R,int C,int P,int MR,int MC>
  439. IGL_INLINE size_t getByteSize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj)
  440. {
  441. // space for numbers of rows,cols and data
  442. return 2*sizeof(typename Eigen::Matrix<T,R,C,P,MR,MC>::Index)+sizeof(T)*obj.rows()*obj.cols();
  443. }
  444. template<typename T,int R,int C,int P,int MR,int MC>
  445. IGL_INLINE void serialize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  446. {
  447. serialization::serialize(obj.rows(),buffer,iter,memoryMap);
  448. serialization::serialize(obj.cols(),buffer,iter,memoryMap);
  449. size_t size = sizeof(T)*obj.rows()*obj.cols();
  450. auto ptr = reinterpret_cast<const uint8_t*>(obj.data());
  451. iter = std::copy(ptr,ptr+size,iter);
  452. }
  453. template<typename T,int R,int C,int P,int MR,int MC>
  454. IGL_INLINE void deserialize(Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>::const_iterator& iter)
  455. {
  456. typename Eigen::Matrix<T,R,C,P,MR,MC>::Index rows,cols;
  457. serialization::deserialize(rows,iter);
  458. serialization::deserialize(cols,iter);
  459. size_t size = sizeof(T)*rows*cols;
  460. obj.resize(rows,cols);
  461. auto ptr = reinterpret_cast<uint8_t*>(obj.data());
  462. std::copy(iter,iter+size,ptr);
  463. iter+=size;
  464. }
  465. template<typename T,int P,typename I>
  466. IGL_INLINE size_t getByteSize(const Eigen::SparseMatrix<T,P,I>& obj)
  467. {
  468. // space for numbers of rows,cols,nonZeros and tripplets with data (rowIdx,colIdx,value)
  469. size_t size = sizeof(Eigen::SparseMatrix<T,P,I>::Index);
  470. return 3*size+(sizeof(T)+2*size)*obj.nonZeros();
  471. }
  472. template<typename T,int P,typename I>
  473. IGL_INLINE void serialize(const Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  474. {
  475. serialization::serialize(obj.rows(),buffer,iter,memoryMap);
  476. serialization::serialize(obj.cols(),buffer,iter,memoryMap);
  477. serialization::serialize(obj.nonZeros(),buffer,iter,memoryMap);
  478. for(int k=0;k<obj.outerSize();++k)
  479. {
  480. for(typename Eigen::SparseMatrix<T,P,I>::InnerIterator it(obj,k);it;++it)
  481. {
  482. serialization::serialize(it.row(),buffer,iter,memoryMap);
  483. serialization::serialize(it.col(),buffer,iter,memoryMap);
  484. serialization::serialize(it.value(),buffer,iter,memoryMap);
  485. }
  486. }
  487. }
  488. template<typename T,int P,typename I>
  489. IGL_INLINE void deserialize(Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>::const_iterator& iter)
  490. {
  491. typename Eigen::SparseMatrix<T,P,I>::Index rows,cols,nonZeros;
  492. serialization::deserialize(rows,iter);
  493. serialization::deserialize(cols,iter);
  494. serialization::deserialize(nonZeros,iter);
  495. obj.resize(rows,cols);
  496. obj.setZero();
  497. std::vector<Eigen::Triplet<T,I> > triplets;
  498. for(int i=0;i<nonZeros;i++)
  499. {
  500. typename Eigen::SparseMatrix<T,P,I>::Index rowId,colId;
  501. serialization::deserialize(rowId,iter);
  502. serialization::deserialize(colId,iter);
  503. T value;
  504. serialization::deserialize(value,iter);
  505. triplets.push_back(Eigen::Triplet<T,I>(rowId,colId,value));
  506. }
  507. obj.setFromTriplets(triplets.begin(),triplets.end());
  508. }
  509. // pointers
  510. template <typename T>
  511. IGL_INLINE typename std::enable_if<std::is_pointer<T>::value,size_t>::type getByteSize(const T& obj)
  512. {
  513. size_t size = sizeof(bool);
  514. if(obj)
  515. size += getByteSize(*obj);
  516. return size;
  517. }
  518. template <typename T>
  519. 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,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  520. {
  521. serialization::serialize(obj == nullptr,buffer,iter,memoryMap);
  522. if(obj)
  523. serialization::serialize(*obj,buffer,iter,memoryMap);
  524. }
  525. template <typename T>
  526. IGL_INLINE typename std::enable_if<std::is_pointer<T>::value>::type deserialize(T& obj,std::vector<char>::const_iterator& iter)
  527. {
  528. bool isNullPtr;
  529. serialization::deserialize(isNullPtr,iter);
  530. if(isNullPtr)
  531. {
  532. if(obj)
  533. {
  534. std::cout << "serialization: possible memory leak in serialization for '" << typeid(obj).name() << "'" << std::endl;
  535. obj = nullptr;
  536. }
  537. }
  538. else
  539. {
  540. if(obj)
  541. {
  542. std::cout << "serialization: possible memory corruption in deserialization for '" << typeid(obj).name() << "'" << std::endl;
  543. }
  544. else
  545. {
  546. obj = new typename std::remove_pointer<T>::type();
  547. }
  548. serialization::deserialize(*obj,iter);
  549. }
  550. }
  551. // std::shared_ptr and std::unique_ptr
  552. template <typename T>
  553. IGL_INLINE typename std::enable_if<serialization::is_smart_ptr<T>::value,size_t>::type getByteSize(const T& obj)
  554. {
  555. return getByteSize(obj.get());
  556. }
  557. template <typename T>
  558. IGL_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,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  559. {
  560. serialize(obj.get(),buffer,iter,memoryMap);
  561. }
  562. template <template<typename> class T0,typename T1>
  563. IGL_INLINE typename std::enable_if<serialization::is_smart_ptr<T0<T1> >::value>::type deserialize(T0<T1>& obj,std::vector<char>::const_iterator& iter)
  564. {
  565. bool isNullPtr;
  566. serialization::deserialize(isNullPtr,iter);
  567. if(isNullPtr)
  568. {
  569. obj.reset();
  570. }
  571. else
  572. {
  573. obj = T0<T1>(new T1());
  574. serialization::deserialize(*obj,iter);
  575. }
  576. }
  577. // std::weak_ptr
  578. template <typename T>
  579. IGL_INLINE size_t getByteSize(const std::weak_ptr<T>& obj)
  580. {
  581. return sizeof(size_t);
  582. }
  583. template <typename T>
  584. IGL_INLINE void serialize(const std::weak_ptr<T>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  585. {
  586. }
  587. template <typename T>
  588. IGL_INLINE void deserialize(std::weak_ptr<T>& obj,std::vector<char>::const_iterator& iter)
  589. {
  590. }
  591. // functions to overload for non-intrusive serialization
  592. template <typename T>
  593. IGL_INLINE void serialize(const T& obj,std::vector<char>& buffer)
  594. {
  595. std::cerr << typeid(obj).name() << " is not serializable: derive from igl::Serializable or overload the function igl::serialization::serialize(const T& obj,std::vector<char>& buffer)" << std::endl;
  596. }
  597. template <typename T>
  598. IGL_INLINE void deserialize(T& obj,const std::vector<char>& buffer)
  599. {
  600. std::cerr << typeid(obj).name() << " is not deserializable: derive from igl::Serializable or overload the function igl::serialization::deserialize(T& obj, const std::vector<char>& buffer)" << std::endl;
  601. }
  602. // helper functions
  603. template <typename T>
  604. IGL_INLINE void updateMemoryMap(T& obj,size_t size,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
  605. {
  606. // check if object is already serialized
  607. auto startPtr = new IndexedPointer<T>();
  608. startPtr->Object = &obj;
  609. auto startBasePtr = static_cast<IndexedPointerBase*>(startPtr);
  610. startBasePtr->Type = IndexedPointerBase::BEGIN;
  611. auto startAddress = reinterpret_cast<std::uintptr_t>(&obj);
  612. auto p = std::pair<std::uintptr_t,IndexedPointerBase*>(startAddress,startBasePtr);
  613. auto el = memoryMap.insert(p);
  614. auto iter = ++el.first; // next elememt
  615. if(el.second && (iter == memoryMap.end() || iter->second->Type != IndexedPointerBase::END))
  616. {
  617. // not yet serialized
  618. auto endPtr = new IndexedPointer<T>();
  619. auto endBasePtr = static_cast<IndexedPointerBase*>(endPtr);
  620. endBasePtr->Type = IndexedPointerBase::END;
  621. auto endAddress = reinterpret_cast<std::uintptr_t>(&obj) + size - 1;
  622. auto p = std::pair<std::uintptr_t,IndexedPointerBase*>(endAddress,endBasePtr);
  623. // insert end address
  624. memoryMap.insert(el.first,p);
  625. }
  626. else
  627. {
  628. // already serialized
  629. // remove inserted address
  630. memoryMap.erase(el.first);
  631. }
  632. }
  633. }
  634. }