libigl v2.5.0
Loading...
Searching...
No Matches
serialize_xml.h
Go to the documentation of this file.
1//
2// Copyright (C) 2014 Christian Sch�ller <schuellchr@gmail.com>
3//
4// This Source Code Form is subject to the terms of the Mozilla Public License
5// v. 2.0. If a copy of the MPL was not distributed with this file, You can
6// obtain one at http://mozilla.org/MPL/2.0/.
7#ifndef IGL_XML_SERIALIZABLE_XML_H
8#define IGL_XML_SERIALIZABLE_XML_H
9// -----------------------------------------------------------------------------
10// Functions to save and load a serialization of fundamental c++ data types to
11// and from a xml file. STL containers, Eigen matrix types and nested data
12// structures are also supported. To serialize a user defined class implement
13// the interface XMLSerializable or XMLSerializableBase.
14//
15// See also: serialize.h
16// -----------------------------------------------------------------------------
17
18#include "../igl_inline.h"
19
20
21#include <Eigen/Dense>
22#include <Eigen/Sparse>
23#include <tinyxml2.h>
24
25#include <type_traits>
26#include <functional>
27#include <iostream>
28#include <vector>
29#include <set>
30#include <map>
31#include <memory>
32
33//#define SERIALIZE_XML(x) igl::xml::serialize_xml(x,#x,doc,element);
34//#define DESERIALIZE_XML(x) igl::xml::deserialize_xml(x,#x,,doc,element);
35
36namespace igl
37{
38 namespace xml
39 {
53 template <typename T>
55 const T& obj,
56 const std::string& objectName,
57 const std::string& filename,
58 bool binary = false,
59 bool overwrite = false);
61 template <typename T>
62 IGL_INLINE void serialize_xml(const T& obj,const std::string& filename);
67 template <typename T>
69 const T& obj,
70 const std::string& objectName,
71 tinyxml2::XMLDocument* doc,
72 tinyxml2::XMLElement* element,
73 bool binary = false);
74
85 template <typename T>
86 IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const std::string& filename);
90 template <typename T>
91 IGL_INLINE void deserialize_xml(T& obj,const std::string& filename);
98 template <typename T>
99 IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element);
100
101 // internal functions
103 namespace serialization_xml
104 {
105 // fundamental types
106 template <typename T>
107 IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
108 template <typename T>
109 IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
110
111 // std::string
112 IGL_INLINE void serialize(const std::string& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
113 IGL_INLINE void deserialize(std::string& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
114
115 // XMLSerializableBase
116 template <typename T>
117 IGL_INLINE typename std::enable_if<std::is_base_of<XMLSerializableBase,T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
118 template <typename T>
119 IGL_INLINE typename std::enable_if<std::is_base_of<XMLSerializableBase,T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
120
121 // STL containers
122 template <typename T1, typename T2>
123 IGL_INLINE void serialize(const std::pair<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
124 template <typename T1,typename T2>
125 IGL_INLINE void deserialize(std::pair<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
126
127 template <typename T1,typename T2>
128 IGL_INLINE void serialize(const std::vector<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
129 template <typename T1,typename T2>
130 IGL_INLINE void deserialize(std::vector<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
131
132 template <typename T>
133 IGL_INLINE void serialize(const std::set<T>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
134 template <typename T>
135 IGL_INLINE void deserialize(std::set<T>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
136
137 template <typename T1,typename T2>
138 IGL_INLINE void serialize(const std::map<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
139 template <typename T1,typename T2>
140 IGL_INLINE void deserialize(std::map<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
141
142 // Eigen types
143
144 // Serialize a Dense Eigen Matrix to xml (in the matrix= attribute,
145 // awkward...)
146 //
147 // Inputs:
148 // obj MR by MC matrix of T types
149 // name name of matrix
150 // to_string function converting T to string
151 // Outputs:
152 // doc pointer to xml document
153 // element pointer to xml element
154 //
155 template<typename T,int R,int C,int P,int MR,int MC>
157 const Eigen::Matrix<T,R,C,P,MR,MC>& obj,
158 const std::string& name,
159 const std::function<std::string(const T &) >& to_string,
160 tinyxml2::XMLDocument* doc,
161 tinyxml2::XMLElement* element);
162 // De-Serialize a Dense Eigen Matrix from xml (in the matrix= attribute,
163 // awkward...)
164 //
165 // Inputs:
166 // doc pointer to xml document
167 // element pointer to xml element
168 // name name of matrix
169 // from_string function string to T
170 // Outputs:
171 // obj MR by MC matrix of T types
172 template<typename T,int R,int C,int P,int MR,int MC>
174 const tinyxml2::XMLDocument* doc,
175 const tinyxml2::XMLElement* element,
176 const std::string& name,
177 const std::function<void(const std::string &,T &)> & from_string,
178 Eigen::Matrix<T,R,C,P,MR,MC>& obj);
179
180 // Legacy APIs
181 template<typename T,int R,int C,int P,int MR,int MC>
183 const Eigen::Matrix<T,R,C,P,MR,MC>& obj,
184 tinyxml2::XMLDocument* doc,
185 tinyxml2::XMLElement* element,
186 const std::string& name);
187 template<typename T,int R,int C,int P,int MR,int MC>
189 Eigen::Matrix<T,R,C,P,MR,MC>& obj,
190 const tinyxml2::XMLDocument* doc,
191 const tinyxml2::XMLElement* element,
192 const std::string& name);
193
194 template<typename T,int P,typename I>
195 IGL_INLINE void serialize(const Eigen::SparseMatrix<T,P,I>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
196 template<typename T,int P,typename I>
197 IGL_INLINE void deserialize(Eigen::SparseMatrix<T,P,I>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
198
199 // raw pointers
200 template <typename T>
201 IGL_INLINE typename std::enable_if<std::is_pointer<T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
202 template <typename T>
203 IGL_INLINE typename std::enable_if<std::is_pointer<T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name);
204
205 // helper functions
206 tinyxml2::XMLElement* getElement(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name);
207 IGL_INLINE void getAttribute(const char* src,bool& dest);
208 IGL_INLINE void getAttribute(const char* scr,char& dest);
209 IGL_INLINE void getAttribute(const char* src,std::string& dest);
210 IGL_INLINE void getAttribute(const char* src,float& dest);
211 IGL_INLINE void getAttribute(const char* src,double& dest);
212 template<typename T>
213 IGL_INLINE typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest);
214 template<typename T>
215 IGL_INLINE typename std::enable_if<std::is_integral<T>::value && !std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest);
216 IGL_INLINE void replaceSubString(std::string& str,const std::string& search,const std::string& replace);
217 IGL_INLINE void encodeXMLElementName(std::string& name);
218 IGL_INLINE void decodeXMLElementName(std::string& name);
219 IGL_INLINE std::string base64_encode(unsigned char const* bytes_to_encode,unsigned int in_len);
220 IGL_INLINE std::string base64_decode(std::string const& encoded_string);
221
222 // compile time type serializable check
223 template <typename T>
224 struct is_stl_container { static const bool value = false; };
225 template <typename T1,typename T2>
226 struct is_stl_container<std::pair<T1,T2> > { static const bool value = true; };
227 template <typename T1,typename T2>
228 struct is_stl_container<std::vector<T1,T2> > { static const bool value = true; };
229 template <typename T>
230 struct is_stl_container<std::set<T> > { static const bool value = true; };
231 template <typename T1,typename T2>
232 struct is_stl_container<std::map<T1,T2> > { static const bool value = true; };
233
234 template <typename T>
235 struct is_eigen_type { static const bool value = false; };
236 template <typename T,int R,int C,int P,int MR,int MC>
237 struct is_eigen_type<Eigen::Matrix<T,R,C,P,MR,MC> > { static const bool value = true; };
238 template <typename T,int P,typename I>
239 struct is_eigen_type<Eigen::SparseMatrix<T,P,I> > { static const bool value = true; };
240
241 template <typename T>
243 using T0 = typename std::remove_pointer<T>::type;
244 static const bool value = std::is_fundamental<T0>::value || std::is_same<std::string,T0>::value || std::is_base_of<XMLSerializableBase,T0>::value
246 };
247 }
248 }
249}
250
251#ifndef IGL_STATIC_LIBRARY
252 #include "serialize_xml.cpp"
253#endif
254
255#endif
#define IGL_INLINE
Definition igl_inline.h:15
std::enable_if< std::is_fundamental< T >::value >::type serialize(const T &obj, tinyxml2::XMLDocument *doc, tinyxml2::XMLElement *element, const std::string &name)
std::enable_if< std::is_fundamental< T >::value >::type deserialize(T &obj, const tinyxml2::XMLDocument *doc, const tinyxml2::XMLElement *element, const std::string &name)
void encodeXMLElementName(std::string &name)
std::string base64_decode(std::string const &encoded_string)
void getAttribute(const char *src, bool &dest)
tinyxml2::XMLElement * getElement(tinyxml2::XMLDocument *doc, tinyxml2::XMLElement *element, const std::string &name)
std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
void decodeXMLElementName(std::string &name)
void replaceSubString(std::string &str, const std::string &search, const std::string &replace)
void deserialize_xml(T &obj, const std::string &objectName, const std::string &filename)
deserialize object to file
void serialize_xml(const T &obj, const std::string &objectName, const std::string &filename, bool binary=false, bool overwrite=false)
Serialize object to file.
Definition AABB.h:17
Definition XMLSerializable.h:21
Definition serialize_xml.h:235
static const bool value
Definition serialize_xml.h:235
Definition serialize_xml.h:242
typename std::remove_pointer< T >::type T0
Definition serialize_xml.h:243
static const bool value
Definition serialize_xml.h:244
Definition serialize_xml.h:224
static const bool value
Definition serialize_xml.h:224