// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2013 Alec Jacobson // // 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/. // --------------------------------------------------------------------------- // serialize.h // author: Christian Schüller // ----------------------------------------------------------------------------- // 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 #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 void serialize(const T& obj,const std::string& filename); template 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 void deserialize(T& obj,const std::string& filename); template 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 std::enable_if_t::value,size_t> getByteSize(const T& obj); template std::enable_if_t::value> serialize(const T& obj,std::vector& buffer,std::vector::iterator& iter); template std::enable_if_t::value> deserialize(T& obj,std::vector::const_iterator& iter); // std::string size_t getByteSize(const std::string& obj); void serialize(const std::string& obj,std::vector& buffer,std::vector::iterator& iter); void deserialize(std::string& obj,std::vector::iterator& iter); // Serializable template std::enable_if_t::value,size_t> getByteSize(const T& obj); template std::enable_if_t::value> serialize(const T& obj,std::vector& buffer,std::vector::iterator& iter); template std::enable_if_t::value> deserialize(T& obj,std::vector::const_iterator& iter); // stl containers // std::pair template size_t getByteSize(const std::pair& obj); template void serialize(const std::pair& obj,std::vector& buffer,std::vector::iterator& iter); template void deserialize(std::pair& obj,std::vector::const_iterator& iter); // std::vector template size_t getByteSize(const std::vector& obj); template void serialize(const std::vector& obj,std::vector& buffer,std::vector::iterator& iter); template void deserialize(std::vector& obj,std::vector::const_iterator& iter); // std::set template size_t getByteSize(const std::set& obj); template void serialize(const std::set& obj,std::vector& buffer,std::vector::iterator& iter); template void deserialize(std::set& obj,std::vector::const_iterator& iter); // std::map template size_t getByteSize(const std::map& obj); template void serialize(const std::map& obj,std::vector& buffer,std::vector::iterator& iter); template void deserialize(std::map& obj,std::vector::const_iterator& iter); // Eigen types template size_t getByteSize(const Eigen::Matrix& obj); template void serialize(const Eigen::Matrix& obj,std::vector& buffer,std::vector::iterator& iter); template void deserialize(Eigen::Matrix& obj,std::vector::const_iterator& iter); template size_t getByteSize(const Eigen::SparseMatrix& obj); template void serialize(const Eigen::SparseMatrix& obj,std::vector& buffer,std::vector::iterator& iter); template void deserialize(Eigen::SparseMatrix& obj,std::vector::const_iterator& iter); // pointers template std::enable_if_t::value,size_t> getByteSize(const T& obj); template std::enable_if_t::value> serialize(const T& obj,std::vector& buffer,std::vector::iterator& iter); template std::enable_if_t::value> 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; }; } } #include "serialize.cpp" #endif