// // Copyright (C) 2014 Christian Sch�ller // // This Source Code Form is subject to the terms of the Mozilla Public License // v. 2.0. If a copy of the MPL was not distributed with this file, You can // obtain one at http://mozilla.org/MPL/2.0/. // ----------------------------------------------------------------------------- // Functions to save and load a serialization of fundamental c++ data types to // and from a binary file. STL containers, Eigen matrix types and nested data // structures are also supported. To serialize a user defined class implement // the interface Serializable. // // See also: xml/serialize_xml.h // ----------------------------------------------------------------------------- // TODOs: // * loops of pointers and from multiple objects // * cross-platform compatibility (big-, little-endian) // ----------------------------------------------------------------------------- #ifndef IGL_SERIALIZE_H #define IGL_SERIALIZE_H #include "igl_inline.h" #include #include #include #include #include #include #include #include #include //#define SERIALIZE(x) igl::serialize(x,#x,buffer); //#define DESERIALIZE(x) igl::deserialize(x,#x,buffer); namespace igl { // serializes the given object either to a file or to a provided buffer // Templates: // T type of the object to serialize // Inputs: // obj object to serialize // objectName unique object name,used for the identification // filename name of the file containing the serialization // Outputs: // buffer binary serialization // template IGL_INLINE void serialize(const T& obj,const std::string& filename); template IGL_INLINE void serialize(const T& obj,const std::string& objectName,std::vector& buffer); // deserializes the given data from a file or buffer back to the provided object // // Templates: // T type of the object to serialize // Inputs: // buffer binary serialization // objectName unique object name, used for the identification // filename name of the file containing the serialization // Outputs: // obj object to load back serialization to // template IGL_INLINE void deserialize(T& obj,const std::string& filename); template IGL_INLINE void deserialize(T& obj,const std::string& objectName,const std::vector& buffer); // interface for user defined types struct Serializable { virtual void Serialize(std::vector& buffer) const = 0; virtual void Deserialize(const std::vector& buffer) = 0; }; // example: // // class Test : public igl::Serializable { // // int var; // // void Serialize(std::vector& buffer) { // serialize(var,"var1",buffer); // } // void Deserialize(const std::vector& buffer) { // deserialize(var,"var1",buffer); // } // } // internal functions namespace detail { // fundamental types template IGL_INLINE typename std::enable_if::value,size_t>::type getByteSize(const T& obj); template IGL_INLINE typename std::enable_if::value>::type serialize(const T& obj,std::vector& buffer,std::vector::iterator& iter); template IGL_INLINE typename std::enable_if::value>::type deserialize(T& obj,std::vector::const_iterator& iter); // std::string IGL_INLINE size_t getByteSize(const std::string& obj); IGL_INLINE void serialize(const std::string& obj,std::vector& buffer,std::vector::iterator& iter); IGL_INLINE void deserialize(std::string& obj,std::vector::const_iterator& iter); // Serializable template IGL_INLINE typename std::enable_if::value,size_t>::type getByteSize(const T& obj); template IGL_INLINE typename std::enable_if::value>::type serialize(const T& obj,std::vector& buffer,std::vector::iterator& iter); template IGL_INLINE typename std::enable_if::value>::type deserialize(T& obj,std::vector::const_iterator& iter); // stl containers // std::pair template IGL_INLINE size_t getByteSize(const std::pair& obj); template IGL_INLINE void serialize(const std::pair& obj,std::vector& buffer,std::vector::iterator& iter); template IGL_INLINE void deserialize(std::pair& obj,std::vector::const_iterator& iter); // std::vector template IGL_INLINE size_t getByteSize(const std::vector& obj); template IGL_INLINE void serialize(const std::vector& obj,std::vector& buffer,std::vector::iterator& iter); template IGL_INLINE void deserialize(std::vector& obj,std::vector::const_iterator& iter); // std::set template IGL_INLINE size_t getByteSize(const std::set& obj); template IGL_INLINE void serialize(const std::set& obj,std::vector& buffer,std::vector::iterator& iter); template IGL_INLINE void deserialize(std::set& obj,std::vector::const_iterator& iter); // std::map template IGL_INLINE size_t getByteSize(const std::map& obj); template IGL_INLINE void serialize(const std::map& obj,std::vector& buffer,std::vector::iterator& iter); template IGL_INLINE void deserialize(std::map& obj,std::vector::const_iterator& iter); // Eigen types template IGL_INLINE size_t getByteSize(const Eigen::Matrix& obj); template IGL_INLINE void serialize(const Eigen::Matrix& obj,std::vector& buffer,std::vector::iterator& iter); template IGL_INLINE void deserialize(Eigen::Matrix& obj,std::vector::const_iterator& iter); template IGL_INLINE size_t getByteSize(const Eigen::SparseMatrix& obj); template IGL_INLINE void serialize(const Eigen::SparseMatrix& obj,std::vector& buffer,std::vector::iterator& iter); template IGL_INLINE void deserialize(Eigen::SparseMatrix& obj,std::vector::const_iterator& iter); // pointers template IGL_INLINE typename std::enable_if::value,size_t>::type getByteSize(const T& obj); template IGL_INLINE typename std::enable_if::value>::type serialize(const T& obj,std::vector& buffer,std::vector::iterator& iter); template IGL_INLINE typename std::enable_if::value>::type deserialize(T& obj,std::vector::const_iterator& iter); // compile time type serializable check template struct is_stl_container { static const bool value = false; }; template struct is_stl_container > { static const bool value = true; }; template struct is_stl_container > { static const bool value = true; }; template struct is_stl_container > { static const bool value = true; }; template struct is_stl_container > { static const bool value = true; }; template struct is_eigen_type { static const bool value = false; }; template struct is_eigen_type > { static const bool value = true; }; template struct is_eigen_type > { static const bool value = true; }; template struct is_serializable { using T0 = typename std::remove_pointer::type; static const bool value = std::is_fundamental::value || std::is_same::value || std::is_base_of::value || is_stl_container::value || is_eigen_type::value; }; } } #ifndef IGL_STATIC_LIBRARY #include "serialize.cpp" #endif #endif