// // 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/. #ifndef IGL_XML_SERIALIZABLE_XML_H #define IGL_XML_SERIALIZABLE_XML_H // ----------------------------------------------------------------------------- // 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 or XMLSerializableBase. // // See also: serialize.h // ----------------------------------------------------------------------------- #include "../igl_inline.h" #include #include #include #include #include #include #include #include #include #include //#define SERIALIZE_XML(x) igl::xml::serialize_xml(x,#x,doc,element); //#define DESERIALIZE_XML(x) igl::xml::deserialize_xml(x,#x,,doc,element); namespace igl { namespace xml { struct XMLSerializableBase; // 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 overwrite 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 IGL_INLINE void serialize_xml(const T& obj,const std::string& filename); template IGL_INLINE void serialize_xml( const T& obj, const std::string& objectName, const std::string& filename, bool binary = false, bool overwrite = false); template IGL_INLINE 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 overwrite 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 IGL_INLINE void deserialize_xml(T& obj,const std::string& filename); template IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const std::string& filename); template IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element); // internal functions namespace serialization_xml { // fundamental types template IGL_INLINE typename std::enable_if::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template IGL_INLINE typename std::enable_if::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); // std::string IGL_INLINE void serialize(const std::string& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); IGL_INLINE void deserialize(std::string& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); // XMLSerializableBase template IGL_INLINE typename std::enable_if::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template IGL_INLINE typename std::enable_if::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); // STL containers template IGL_INLINE void serialize(const std::pair& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template IGL_INLINE void deserialize(std::pair& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); template IGL_INLINE void serialize(const std::vector& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template IGL_INLINE void deserialize(std::vector& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); template IGL_INLINE void serialize(const std::set& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template IGL_INLINE void deserialize(std::set& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); template IGL_INLINE void serialize(const std::map& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template IGL_INLINE void deserialize(std::map& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); // Eigen types // Serialize a Dense Eigen Matrix to xml (in the matrix= attribute, // awkward...) // // Inputs: // obj MR by MC matrix of T types // name name of matrix // to_string function converting T to string // Outputs: // doc pointer to xml document // element pointer to xml element // template IGL_INLINE void serialize( const Eigen::Matrix& obj, const std::string& name, const std::function& to_string, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element); // De-Serialize a Dense Eigen Matrix from xml (in the matrix= attribute, // awkward...) // // Inputs: // doc pointer to xml document // element pointer to xml element // name name of matrix // from_string function string to T // Outputs: // obj MR by MC matrix of T types template IGL_INLINE void deserialize( const tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name, const std::function & from_string, Eigen::Matrix& obj); // Legacy APIs template IGL_INLINE void serialize( const Eigen::Matrix& obj, tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* element, const std::string& name); template IGL_INLINE void deserialize( Eigen::Matrix& obj, const tinyxml2::XMLDocument* doc, const tinyxml2::XMLElement* element, const std::string& name); template IGL_INLINE void serialize(const Eigen::SparseMatrix& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template IGL_INLINE void deserialize(Eigen::SparseMatrix& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name); // raw pointers template IGL_INLINE typename std::enable_if::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name); template IGL_INLINE typename std::enable_if::value>::type 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); IGL_INLINE void getAttribute(const char* src,bool& dest); IGL_INLINE void getAttribute(const char* scr,char& dest); IGL_INLINE void getAttribute(const char* src,std::string& dest); IGL_INLINE void getAttribute(const char* src,float& dest); IGL_INLINE void getAttribute(const char* src,double& dest); template IGL_INLINE typename std::enable_if::value && std::is_unsigned::value>::type getAttribute(const char* src,T& dest); template IGL_INLINE typename std::enable_if::value && !std::is_unsigned::value>::type getAttribute(const char* src,T& dest); IGL_INLINE void replaceSubString(std::string& str,const std::string& search,const std::string& replace); IGL_INLINE void encodeXMLElementName(std::string& name); IGL_INLINE void decodeXMLElementName(std::string& name); IGL_INLINE std::string base64_encode(unsigned char const* bytes_to_encode,unsigned int in_len); IGL_INLINE 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; }; } } } #ifndef IGL_STATIC_LIBRARY #include "serialize_xml.cpp" #endif #endif