// // 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 xml file. STL containers, Eigen matrix types and nested data // structures are also supported. To serialize a user defined class implement // the interface XMLSerializable. // // See also: serialize.h // ----------------------------------------------------------------------------- #ifndef IGL_SERIALIZABLE_XML_H #define IGL_SERIALIZABLE_XML_H #include #include #include #include #include #include #include #include #include "tinyxml2.h" //#define SERIALIZE_XML(x) igl::serialize_xml(x,#x,doc,element); //#define DESERIALIZE_XML(x) igl::deserialize_xml(x,#x,,doc,element); namespace igl { // serializes the given object either to a xml file or to the provided doc data // // 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 // binary set to true to serialize the object in binary format (faster for big data) // overwrite set to true to update the serialization in an existing xml file // element tinyxml2 virtual representation of the current xml node // Outputs: // doc contains current tinyxml2 virtual representation of the xml data // template void serialize_xml(const T& obj,const std::string& filename); template void serialize_xml(const T& obj,const std::string& objectName,const std::string& filename, bool binary = false,bool overwrite = false); template void serialize_xml(const T& obj,const std::string& objectName,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,bool binary = false); // deserializes the given data from a xml file or doc data back to the provided object // // Templates: // T type of the object to serialize // Inputs: // // objectName unique object name,used for the identification // filename name of the file containing the serialization // binary set to true to serialize the object in binary format (faster for big data) // overwrite set to true to update the serialization in an existing xml file // doc contains current tinyxml2 virtual representation of the xml data // element tinyxml2 virtual representation of the current xml node // Outputs: // obj object to load back serialization to // template void deserialize_xml(T& obj,const std::string& filename); template void deserialize_xml(T& obj,const std::string& objectName,const std::string& filename); template void deserialize_xml(T& obj,const std::string& objectName,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element); // interface for user defined types struct XMLSerializable : public Serializable { virtual void Serialize(std::vector& buffer) const = 0; virtual void Deserialize(const std::vector& buffer) = 0; virtual void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const = 0; virtual void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element) = 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); // } // void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const { // serialize_xml(var,"var1",doc,element); // } // void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element) { // deserialize_xml(var,"var1",doc,element); // } // } // internal functions namespace detail_xml { // fundamental types template std::enable_if_t::value> serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template std::enable_if_t::value> deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); // std::string void serialize(const std::string& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); void deserialize(std::string& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); // Serializable template std::enable_if_t::value> serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template std::enable_if_t::value> deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); // STL containers template void serialize(const std::pair& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template void deserialize(std::pair& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); template void serialize(const std::vector& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template void deserialize(std::vector& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); template void serialize(const std::set& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template void deserialize(std::set& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); template void serialize(const std::map& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template void deserialize(std::map& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); // Eigen types template void serialize(const Eigen::Matrix& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template void deserialize(Eigen::Matrix& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); template void serialize(const Eigen::SparseMatrix& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template void deserialize(Eigen::SparseMatrix& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); // pointers template std::enable_if_t::value> serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template std::enable_if_t::value> deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); // helper functions tinyxml2::XMLElement* getElement(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); void getAttribute(const char* src,bool& dest); void getAttribute(const char* scr,char& dest); void getAttribute(const char* src,std::string& dest); void getAttribute(const char* src,float& dest); void getAttribute(const char* src,double& dest); template std::enable_if_t::value && std::is_unsigned::value> getAttribute(const char* src,T& dest); template std::enable_if_t::value && !std::is_unsigned::value> getAttribute(const char* src,T& dest); void replaceSubString(std::string& str,const std::string& search,const std::string& replace); void encodeXMLElementName(std::string& name); void decodeXMLElementName(std::string& name); std::string base64_encode(unsigned char const* bytes_to_encode,unsigned int in_len); std::string base64_decode(std::string const& encoded_string); // 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_xml.cpp"; #endif