40#include <Eigen/Sparse>
46#define SERIALIZE_TYPE(Type,Params) \
47namespace igl { namespace serialization { \
48 void _serialization(bool s,Type& obj,std::vector<char>& buffer) {Params} \
49 template<> inline void serialize(const Type& obj,std::vector<char>& buffer) { \
50 _serialization(true,const_cast<Type&>(obj),buffer); \
52 template<> inline void deserialize(Type& obj,const std::vector<char>& buffer) { \
53 _serialization(false,obj,const_cast<std::vector<char>&>(buffer)); \
57#define SERIALIZE_TYPE_SOURCE(Type,Params) \
58namespace igl { namespace serialization { \
59 void _serialization(bool s,Type& obj,std::vector<char>& buffer) {Params} \
60 void _serialize(const Type& obj,std::vector<char>& buffer) { \
61 _serialization(true,const_cast<Type&>(obj),buffer); \
63 void _deserialize(Type& obj,const std::vector<char>& buffer) { \
64 _serialization(false,obj,const_cast<std::vector<char>&>(buffer)); \
68#define SERIALIZE_MEMBER(Object) igl::serializer(s,obj.Object,std::string(#Object),buffer);
69#define SERIALIZE_MEMBER_NAME(Object,Name) igl::serializer(s,obj.Object,std::string(Name),buffer);
74 struct IndexedPointerBase;
88 inline bool serialize(
const T& obj,
const std::string& filename);
90 inline bool serialize(
const T& obj,
const std::string& objectName,
const std::string& filename,
bool overwrite =
false);
92 inline bool serialize(
const T& obj,
const std::string& objectName,std::vector<char>& buffer);
94 inline bool serialize(
const T& obj,
const std::string& objectName,std::vector<char>& buffer);
107 template <
typename T>
108 inline bool deserialize(T& obj,
const std::string& filename);
109 template <
typename T>
110 inline bool deserialize(T& obj,
const std::string& objectName,
const std::string& filename);
111 template <
typename T>
112 inline bool deserialize(T& obj,
const std::string& objectName,
const std::vector<char>& buffer);
116 template <
typename T>
118 template <
typename T>
119 inline bool serializer(
bool serialize,T& obj,
const std::string& objectName,
const std::string& filename,
bool overwrite =
false);
120 template <
typename T>
121 inline bool serializer(
bool serialize,T& obj,
const std::string& objectName,std::vector<char>& buffer);
161 virtual void Serialize(std::vector<char>& buffer)
const = 0;
170 template <
typename T>
175 std::unique_ptr<T> Object;
177 void Serialize(std::vector<char>& buffer)
const override {
181 void Deserialize(
const std::vector<char>& buffer)
override {
186 mutable bool initialized;
187 mutable std::vector<SerializableBase*> objects;
205 inline void Serialize(std::vector<char>& buffer)
const override final;
206 inline void Deserialize(
const std::vector<char>& buffer)
override final;
215 template <
typename T>
216 inline void Add(T& obj,std::string name,
bool binary =
false);
232 namespace serialization
235 template <
typename T>
237 template <
typename T1,
typename T2>
239 template <
typename T1,
typename T2>
241 template <
typename T>
243 template <
typename T1,
typename T2>
245 template <
typename T>
248 template <
typename T>
250 template <
typename T,
int R,
int C,
int P,
int MR,
int MC>
252 template <
typename T,
int R,
int C,
int P,
int MR,
int MC>
254 template <
typename T,
int P,
typename I>
257 template <
typename T>
259 template <
typename T>
261 template <
typename T>
263 template <
typename T>
266 template <
typename T>
268 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
273 template <
typename T>
274 inline typename std::enable_if<!is_serializable<T>::value,
size_t>::type
getByteSize(
const T& obj);
275 template <
typename T>
276 inline typename std::enable_if<!is_serializable<T>::value>::type
serialize(
const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
277 template <
typename T>
278 inline typename std::enable_if<!is_serializable<T>::value>::type
deserialize(T& obj,std::vector<char>::const_iterator& iter);
281 template <
typename T>
282 inline typename std::enable_if<std::is_fundamental<T>::value,
size_t>::type
getByteSize(
const T& obj);
283 template <
typename T>
284 inline typename std::enable_if<std::is_fundamental<T>::value>::type
serialize(
const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
285 template <
typename T>
286 inline typename std::enable_if<std::is_fundamental<T>::value>::type
deserialize(T& obj,std::vector<char>::const_iterator& iter);
290 inline void serialize(
const std::string& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
291 inline void deserialize(std::string& obj,std::vector<char>::const_iterator& iter);
294 template <
typename T>
295 inline typename std::enable_if<std::is_enum<T>::value,
size_t>::type
getByteSize(
const T& obj);
296 template <
typename T>
297 inline typename std::enable_if<std::is_enum<T>::value>::type
serialize(
const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
298 template <
typename T>
299 inline typename std::enable_if<std::is_enum<T>::value>::type
deserialize(T& obj,std::vector<char>::const_iterator& iter);
302 template <
typename T>
303 inline typename std::enable_if<std::is_base_of<SerializableBase,T>::value,
size_t>::type
getByteSize(
const T& obj);
304 template <
typename T>
305 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);
306 template <
typename T>
307 inline typename std::enable_if<std::is_base_of<SerializableBase,T>::value>::type
deserialize(T& obj,std::vector<char>::const_iterator& iter);
311 template <
typename T1,
typename T2>
312 inline size_t getByteSize(
const std::pair<T1,T2>& obj);
313 template <
typename T1,
typename T2>
314 inline void serialize(
const std::pair<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
315 template <
typename T1,
typename T2>
316 inline void deserialize(std::pair<T1,T2>& obj,std::vector<char>::const_iterator& iter);
319 template <
typename T1,
typename T2>
320 inline size_t getByteSize(
const std::vector<T1,T2>& obj);
321 template <
typename T1,
typename T2>
322 inline void serialize(
const std::vector<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
323 template <
typename T1,
typename T2>
324 inline void deserialize(std::vector<T1,T2>& obj,std::vector<char>::const_iterator& iter);
325 template <
typename T2>
326 inline void deserialize(std::vector<bool,T2>& obj,std::vector<char>::const_iterator& iter);
329 template <
typename T>
331 template <
typename T>
332 inline void serialize(
const std::set<T>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
333 template <
typename T>
334 inline void deserialize(std::set<T>& obj,std::vector<char>::const_iterator& iter);
337 template <
typename T1,
typename T2>
338 inline size_t getByteSize(
const std::map<T1,T2>& obj);
339 template <
typename T1,
typename T2>
340 inline void serialize(
const std::map<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
341 template <
typename T1,
typename T2>
342 inline void deserialize(std::map<T1,T2>& obj,std::vector<char>::const_iterator& iter);
345 template <
typename T>
346 inline size_t getByteSize(
const std::list<T>& obj);
347 template <
typename T>
348 inline void serialize(
const std::list<T>& obj, std::vector<char>& buffer, std::vector<char>::iterator& iter);
349 template <
typename T>
350 inline void deserialize(std::list<T>& obj, std::vector<char>::const_iterator& iter);
353 template<
typename T,
int R,
int C,
int P,
int MR,
int MC>
354 inline size_t getByteSize(
const Eigen::Matrix<T,R,C,P,MR,MC>& obj);
355 template<
typename T,
int R,
int C,
int P,
int MR,
int MC>
356 inline void serialize(
const Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
357 template<
typename T,
int R,
int C,
int P,
int MR,
int MC>
358 inline void deserialize(Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>::const_iterator& iter);
360 template<
typename T,
int R,
int C,
int P,
int MR,
int MC>
361 inline size_t getByteSize(
const Eigen::Array<T,R,C,P,MR,MC>& obj);
362 template<
typename T,
int R,
int C,
int P,
int MR,
int MC>
363 inline void serialize(
const Eigen::Array<T,R,C,P,MR,MC>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
364 template<
typename T,
int R,
int C,
int P,
int MR,
int MC>
365 inline void deserialize(Eigen::Array<T,R,C,P,MR,MC>& obj,std::vector<char>::const_iterator& iter);
367 template<
typename T,
int P,
typename I>
368 inline size_t getByteSize(
const Eigen::SparseMatrix<T,P,I>& obj);
369 template<
typename T,
int P,
typename I>
370 inline void serialize(
const Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
371 template<
typename T,
int P,
typename I>
372 inline void deserialize(Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>::const_iterator& iter);
374 template<
typename T,
int P>
375 inline size_t getByteSize(
const Eigen::Quaternion<T,P>& obj);
376 template<
typename T,
int P>
377 inline void serialize(
const Eigen::Quaternion<T,P>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
378 template<
typename T,
int P>
379 inline void deserialize(Eigen::Quaternion<T,P>& obj,std::vector<char>::const_iterator& iter);
382 template <
typename T>
383 inline typename std::enable_if<std::is_pointer<T>::value,
size_t>::type
getByteSize(
const T& obj);
384 template <
typename T>
385 inline typename std::enable_if<std::is_pointer<T>::value>::type
serialize(
const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
386 template <
typename T>
387 inline typename std::enable_if<std::is_pointer<T>::value>::type
deserialize(T& obj,std::vector<char>::const_iterator& iter);
390 template <
typename T>
391 inline typename std::enable_if<serialization::is_smart_ptr<T>::value,
size_t>::type
getByteSize(
const T& obj);
392 template <
typename T>
393 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);
394 template <
template<
typename>
class T0,
typename T1>
395 inline typename std::enable_if<serialization::is_smart_ptr<T0<T1> >::value>::type
deserialize(T0<T1>& obj,std::vector<char>::const_iterator& iter);
398 template <
typename T>
399 inline size_t getByteSize(
const std::weak_ptr<T>& obj);
400 template <
typename T>
401 inline void serialize(
const std::weak_ptr<T>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter);
402 template <
typename T>
403 inline void deserialize(std::weak_ptr<T>& obj,std::vector<char>::const_iterator& iter);
406 template <
typename T>
407 inline void serialize(
const T& obj,std::vector<char>& buffer);
408 template <
typename T>
409 inline void deserialize(T& obj,
const std::vector<char>& buffer);
412 template <
typename T>
423 template <
typename T>
424 inline bool serialize(
const T& obj,
const std::string& filename)
426 return serialize(obj,
"obj",filename,
true);
429 template <
typename T>
430 inline bool serialize(
const T& obj,
const std::string& objectName,
const std::string& filename,
bool overwrite)
432 bool success =
false;
434 std::vector<char> buffer;
436 std::ios_base::openmode
mode = std::ios::out | std::ios::binary;
439 mode |= std::ios::trunc;
441 mode |= std::ios::app;
443 std::ofstream file(filename.c_str(),
mode);
449 file.write(&buffer[0],buffer.size());
457 std::cerr <<
"serialization: file " << filename <<
" not found!" << std::endl;
463 template <
typename T>
464 inline bool serialize(
const T& obj,
const std::string& objectName,std::vector<char>& buffer)
468 std::vector<char> tmp(size);
469 auto it = tmp.begin();
472 std::string objectType(
typeid(obj).name());
473 size_t newObjectSize = tmp.size();
475 size_t curSize = buffer.size();
476 size_t newSize = curSize + newHeaderSize + newObjectSize;
478 buffer.resize(newSize);
480 std::vector<char>::iterator iter = buffer.begin()+curSize;
488 iter = std::copy(tmp.begin(),tmp.end(),iter);
493 template <
typename T>
499 template <
typename T>
500 inline bool deserialize(T& obj,
const std::string& objectName,
const std::string& filename)
502 bool success =
false;
504 std::ifstream file(filename.c_str(),std::ios::binary);
508 file.seekg(0,std::ios::end);
509 std::streamoff size = file.tellg();
510 file.seekg(0,std::ios::beg);
512 std::vector<char> buffer(size);
513 file.read(&buffer[0],size);
520 std::cerr <<
"serialization: file " << filename <<
" not found!" << std::endl;
526 template <
typename T>
527 inline bool deserialize(T& obj,
const std::string& objectName,
const std::vector<char>& buffer)
529 bool success =
false;
532 auto objectIter = buffer.cend();
533 auto iter = buffer.cbegin();
534 while(iter != buffer.end())
543 if(name == objectName && type ==
typeid(obj).name())
552 if(objectIter != buffer.end())
566 template <
typename T>
567 inline bool serializer(
bool s,T& obj,
const std::string& filename)
572 template <
typename T>
573 inline bool serializer(
bool s,T& obj,
const std::string& objectName,
const std::string& filename,
bool overwrite)
578 template <
typename T>
579 inline bool serializer(
bool s,T& obj,
const std::string& objectName,std::vector<char>& buffer)
606 if(initialized ==
false)
613 for(
const auto& v : objects)
615 v->Serialize(buffer);
626 if(initialized ==
false)
633 for(
auto& v : objects)
635 v->Deserialize(buffer);
672 template <
typename T>
675 auto object =
new SerializationObject<T>();
676 object->Binary = binary;
678 object->Object = std::unique_ptr<T>(&obj);
680 objects.push_back(
object);
683 namespace serialization
685 template <
typename T>
686 inline typename std::enable_if<!is_serializable<T>::value,
size_t>::type
getByteSize(
const T& )
688 return sizeof(std::vector<char>::size_type);
691 template <
typename T>
692 inline typename std::enable_if<!is_serializable<T>::value>::type
serialize(
const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
695 std::vector<char> tmp;
696 serialize<>(obj,tmp);
699 size_t size = buffer.size();
701 size_t cur = iter - buffer.begin();
703 buffer.resize(size+tmp.size());
704 iter = buffer.begin()+cur;
705 iter = std::copy(tmp.begin(),tmp.end(),iter);
708 template <
typename T>
709 inline typename std::enable_if<!is_serializable<T>::value>::type
deserialize(T& obj,std::vector<char>::const_iterator& iter)
711 std::vector<char>::size_type size;
712 serialization::deserialize<>(size,iter);
714 std::vector<char> tmp;
716 std::copy(iter,iter+size,tmp.begin());
718 deserialize<>(obj,tmp);
724 template <
typename T>
725 inline typename std::enable_if<std::is_fundamental<T>::value,
size_t>::type
getByteSize(
const T& )
730 template <
typename T>
731 inline typename std::enable_if<std::is_fundamental<T>::value>::type
serialize(
const T& obj,std::vector<char>& ,std::vector<char>::iterator& iter)
734 const uint8_t* ptr =
reinterpret_cast<const uint8_t*
>(&obj);
735 iter = std::copy(ptr,ptr+
sizeof(T),iter);
738 template <
typename T>
739 inline typename std::enable_if<std::is_fundamental<T>::value>::type
deserialize(T& obj,std::vector<char>::const_iterator& iter)
741 uint8_t* ptr =
reinterpret_cast<uint8_t*
>(&obj);
742 std::copy(iter,iter+
sizeof(T),ptr);
750 return getByteSize(obj.length())+obj.length()*
sizeof(uint8_t);
753 inline void serialize(
const std::string& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
756 for(
const auto& cur : obj)
762 inline void deserialize(std::string& obj,std::vector<char>::const_iterator& iter)
767 std::string str(size,
'\0');
768 for(
size_t i=0; i<size; ++i)
778 template <
typename T>
779 inline typename std::enable_if<std::is_enum<T>::value,
size_t>::type
getByteSize(
const T& )
784 template <
typename T>
785 inline typename std::enable_if<std::is_enum<T>::value>::type
serialize(
const T& obj,std::vector<char>& ,std::vector<char>::iterator& iter)
787 const uint8_t* ptr =
reinterpret_cast<const uint8_t*
>(&obj);
788 iter = std::copy(ptr,ptr+
sizeof(T),iter);
791 template <
typename T>
792 inline typename std::enable_if<std::is_enum<T>::value>::type
deserialize(T& obj,std::vector<char>::const_iterator& iter)
794 uint8_t* ptr =
reinterpret_cast<uint8_t*
>(&obj);
795 std::copy(iter,iter+
sizeof(T),ptr);
801 template <
typename T>
802 inline typename std::enable_if<std::is_base_of<SerializableBase,T>::value,
size_t>::type
getByteSize(
const T& )
804 return sizeof(std::vector<char>::size_type);
807 template <
typename T>
808 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)
811 std::vector<char> tmp;
815 size_t size = buffer.size();
817 size_t cur = iter - buffer.begin();
819 buffer.resize(size+tmp.size());
820 iter = buffer.begin()+cur;
821 iter = std::copy(tmp.begin(),tmp.end(),iter);
824 template <
typename T>
825 inline typename std::enable_if<std::is_base_of<SerializableBase,T>::value>::type
deserialize(T& obj,std::vector<char>::const_iterator& iter)
827 std::vector<char>::size_type size;
830 std::vector<char> tmp;
832 std::copy(iter,iter+size,tmp.begin());
834 obj.Deserialize(tmp);
842 template <
typename T1,
typename T2>
848 template <
typename T1,
typename T2>
849 inline void serialize(
const std::pair<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
855 template <
typename T1,
typename T2>
856 inline void deserialize(std::pair<T1,T2>& obj,std::vector<char>::const_iterator& iter)
864 template <
typename T1,
typename T2>
867 return std::accumulate(obj.begin(),obj.end(),
sizeof(
size_t),[](
const size_t& acc,
const T1& cur) { return acc+getByteSize(cur); });
870 template <
typename T1,
typename T2>
871 inline void serialize(
const std::vector<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
873 size_t size = obj.size();
875 for(
const T1& cur : obj)
881 template <
typename T1,
typename T2>
882 inline void deserialize(std::vector<T1,T2>& obj,std::vector<char>::const_iterator& iter)
894 template <
typename T2>
895 inline void deserialize(std::vector<bool,T2>& obj,std::vector<char>::const_iterator& iter)
901 for(
int i=0;i<obj.size();i++)
911 template <
typename T>
914 return std::accumulate(obj.begin(),obj.end(),
getByteSize(obj.size()),[](
const size_t& acc,
const T& cur) { return acc+getByteSize(cur); });
917 template <
typename T>
918 inline void serialize(
const std::set<T>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
921 for(
const T& cur : obj)
927 template <
typename T>
928 inline void deserialize(std::set<T>& obj,std::vector<char>::const_iterator& iter)
934 for(
size_t i=0; i<size; ++i)
944 template <
typename T1,
typename T2>
947 return std::accumulate(obj.begin(),obj.end(),
sizeof(
size_t),[](
const size_t& acc,
const std::pair<T1,T2>& cur) { return acc+getByteSize(cur); });
950 template <
typename T1,
typename T2>
951 inline void serialize(
const std::map<T1,T2>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
954 for(
const auto& cur : obj)
960 template <
typename T1,
typename T2>
961 inline void deserialize(std::map<T1,T2>& obj,std::vector<char>::const_iterator& iter)
967 for(
size_t i=0; i<size; ++i)
969 std::pair<T1,T2> pair;
977 template <
typename T>
980 return std::accumulate(obj.begin(), obj.end(),
getByteSize(obj.size()), [](
const size_t& acc,
const T& cur) { return acc + getByteSize(cur); });
983 template <
typename T>
984 inline void serialize(
const std::list<T>& obj, std::vector<char>& buffer, std::vector<char>::iterator& iter)
987 for (
const T& cur : obj)
993 template <
typename T>
994 inline void deserialize(std::list<T>& obj, std::vector<char>::const_iterator& iter)
1000 for (
size_t i = 0; i < size; ++i)
1004 obj.emplace_back(val);
1010 template<
typename T,
int R,
int C,
int P,
int MR,
int MC>
1014 return 2*
sizeof(
typename Eigen::Matrix<T,R,C,P,MR,MC>::Index)+
sizeof(T)*obj.rows()*obj.cols();
1017 template<
typename T,
int R,
int C,
int P,
int MR,
int MC>
1018 inline void serialize(
const Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
1022 size_t size =
sizeof(T)*obj.rows()*obj.cols();
1023 auto ptr =
reinterpret_cast<const uint8_t*
>(obj.data());
1024 iter = std::copy(ptr,ptr+size,iter);
1027 template<
typename T,
int R,
int C,
int P,
int MR,
int MC>
1028 inline void deserialize(Eigen::Matrix<T,R,C,P,MR,MC>& obj,std::vector<char>::const_iterator& iter)
1030 typename Eigen::Matrix<T,R,C,P,MR,MC>::Index rows,cols;
1033 size_t size =
sizeof(T)*rows*cols;
1034 obj.resize(rows,cols);
1035 auto ptr =
reinterpret_cast<uint8_t*
>(obj.data());
1036 std::copy(iter,iter+size,ptr);
1040 template<
typename T,
int R,
int C,
int P,
int MR,
int MC>
1044 return 2*
sizeof(
typename Eigen::Array<T,R,C,P,MR,MC>::Index)+
sizeof(T)*obj.rows()*obj.cols();
1047 template<
typename T,
int R,
int C,
int P,
int MR,
int MC>
1048 inline void serialize(
const Eigen::Array<T,R,C,P,MR,MC>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
1052 size_t size =
sizeof(T)*obj.rows()*obj.cols();
1053 auto ptr =
reinterpret_cast<const uint8_t*
>(obj.data());
1054 iter = std::copy(ptr,ptr+size,iter);
1057 template<
typename T,
int R,
int C,
int P,
int MR,
int MC>
1058 inline void deserialize(Eigen::Array<T,R,C,P,MR,MC>& obj,std::vector<char>::const_iterator& iter)
1060 typename Eigen::Array<T,R,C,P,MR,MC>::Index rows,cols;
1063 size_t size =
sizeof(T)*rows*cols;
1064 obj.resize(rows,cols);
1065 auto ptr =
reinterpret_cast<uint8_t*
>(obj.data());
1066 std::copy(iter,iter+size,ptr);
1070 template<
typename T,
int P,
typename I>
1074 size_t size =
sizeof(
typename Eigen::SparseMatrix<T,P,I>::Index);
1075 return 3*size+(
sizeof(T)+2*size)*obj.nonZeros();
1078 template<
typename T,
int P,
typename I>
1079 inline void serialize(
const Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
1085 for(
int k=0;k<obj.outerSize();++k)
1087 for(
typename Eigen::SparseMatrix<T,P,I>::InnerIterator it(obj,k);it;++it)
1096 template<
typename T,
int P,
typename I>
1097 inline void deserialize(Eigen::SparseMatrix<T,P,I>& obj,std::vector<char>::const_iterator& iter)
1099 typename Eigen::SparseMatrix<T,P,I>::Index rows,cols,nonZeros;
1104 obj.resize(rows,cols);
1107 std::vector<Eigen::Triplet<T,I> > triplets;
1108 for(
int i=0;i<nonZeros;i++)
1110 typename Eigen::SparseMatrix<T,P,I>::Index rowId,colId;
1115 triplets.push_back(Eigen::Triplet<T,I>(rowId,colId,value));
1117 obj.setFromTriplets(triplets.begin(),triplets.end());
1120 template<
typename T,
int P>
1126 template<
typename T,
int P>
1127 inline void serialize(
const Eigen::Quaternion<T,P>& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
1135 template<
typename T,
int P>
1136 inline void deserialize(Eigen::Quaternion<T,P>& obj,std::vector<char>::const_iterator& iter)
1146 template <
typename T>
1147 inline typename std::enable_if<std::is_pointer<T>::value,
size_t>::type
getByteSize(
const T& obj)
1149 size_t size =
sizeof(bool);
1157 template <
typename T>
1158 inline typename std::enable_if<std::is_pointer<T>::value>::type
serialize(
const T& obj,std::vector<char>& buffer,std::vector<char>::iterator& iter)
1166 template <
typename T>
1167 inline typename std::enable_if<std::is_pointer<T>::value>::type
deserialize(T& obj,std::vector<char>::const_iterator& iter)
1176 std::cout <<
"serialization: possible memory leak in serialization for '" <<
typeid(obj).name() <<
"'" << std::endl;
1184 std::cout <<
"serialization: possible memory corruption in deserialization for '" <<
typeid(obj).name() <<
"'" << std::endl;
1188 obj =
new typename std::remove_pointer<T>::type();
1196 template <
typename T>
1197 inline typename std::enable_if<serialization::is_smart_ptr<T>::value,
size_t>::type
getByteSize(
const T& obj)
1202 template <
typename T>
1203 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)
1208 template <
template<
typename>
class T0,
typename T1>
1209 inline typename std::enable_if<serialization::is_smart_ptr<T0<T1> >::value>::type
deserialize(T0<T1>& obj,std::vector<char>::const_iterator& iter)
1220 obj = T0<T1>(
new T1());
1227 template <
typename T>
1230 return sizeof(size_t);
1233 template <
typename T>
1234 inline void serialize(
const std::weak_ptr<T>& ,std::vector<char>& ,std::vector<char>::iterator& )
1239 template <
typename T>
1240 inline void deserialize(std::weak_ptr<T>& ,std::vector<char>::const_iterator& )
1246 template <
typename T>
1249 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;
1252 template <
typename T>
1255 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;
1260 template <
typename T>
1261 inline void updateMemoryMap(T& obj,
size_t size,std::map<std::uintptr_t,IndexedPointerBase*>& memoryMap)
1265 startPtr->Object = &obj;
1268 auto startAddress =
reinterpret_cast<std::uintptr_t
>(&obj);
1269 auto p = std::pair<std::uintptr_t,IndexedPointerBase*>(startAddress,startBasePtr);
1271 auto el = memoryMap.insert(p);
1272 auto iter = ++el.first;
1279 auto endAddress =
reinterpret_cast<std::uintptr_t
>(&obj) + size - 1;
1280 auto p = std::pair<std::uintptr_t,IndexedPointerBase*>(endAddress,endBasePtr);
1283 memoryMap.insert(el.first,p);
1290 memoryMap.erase(el.first);
Definition serialize.h:167
void Add(T &obj, std::string name, bool binary=false)
Definition serialize.h:673
virtual void PostSerialization() const
Definition serialize.h:589
Serializable()
Definition serialize.h:642
void Deserialize(const std::vector< char > &buffer) override final
Definition serialize.h:622
virtual bool PreDeserialization()
Definition serialize.h:593
void Serialize(std::vector< char > &buffer) const override final
Definition serialize.h:602
virtual void PostDeserialization()
Definition serialize.h:598
virtual bool PreSerialization() const
Definition serialize.h:584
Serializable & operator=(const Serializable &obj)
Definition serialize.h:659
virtual ~Serializable()
Definition serialize.h:653
virtual void InitSerialization()=0
std::enable_if<!is_serializable< T >::value, size_t >::type getByteSize(const T &obj)
Definition serialize.h:686
void deserialize(igl::opengl::ViewerCore &obj, const std::vector< char > &buffer)
Definition ViewerCore.h:286
void updateMemoryMap(T &obj, size_t size)
void serialize(const igl::opengl::ViewerCore &obj, std::vector< char > &buffer)
Definition ViewerCore.h:280
void mode(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &X, const int d, Eigen::Matrix< T, Eigen::Dynamic, 1 > &M)
Takes mode of coefficients in a matrix along a given dimension.
bool serialize(const T &obj, const std::string &filename)
Definition serialize.h:424
bool deserialize(T &obj, const std::string &filename)
Definition serialize.h:494
bool serializer(bool serialize, T &obj, const std::string &filename)
Definition serialize.h:567
Definition serialize.h:221
enum igl::IndexedPointerBase::@2 Type
@ END
Definition serialize.h:222
@ BEGIN
Definition serialize.h:222
size_t Index
Definition serialize.h:223
Definition serialize.h:227
const T * Object
Definition serialize.h:228
Definition serialize.h:159
virtual void Serialize(std::vector< char > &buffer) const =0
virtual void Deserialize(const std::vector< char > &buffer)=0
virtual ~SerializableBase()=default
Definition serialize.h:249
static const bool value
Definition serialize.h:249
Definition serialize.h:267
static const bool value
Definition serialize.h:268
Definition serialize.h:258
static const bool value
Definition serialize.h:258
Definition serialize.h:236
static const bool value
Definition serialize.h:236