libigl v2.5.0
Loading...
Searching...
No Matches
XMLSerializable.h
Go to the documentation of this file.
1#ifndef IGL_XML_XMLSERIALIZABLE_H
2#define IGL_XML_XMLSERIALIZABLE_H
3
4#include "serialize_xml.h"
5#include "../igl_inline.h"
6#include "../serialize.h"
7
8#include <tinyxml2.h>
9
10
11// Interface for xml-serializable class see serialize_xml.h
12
13// Pretty sure all of these IGL_INLINE should be inline
14
15namespace igl
16{
17 namespace xml
18 {
19 // interface for user defined types
21 {
22 virtual void Serialize(std::vector<char>& buffer) const = 0;
23 virtual void Deserialize(const std::vector<char>& buffer) = 0;
24 virtual void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const = 0;
25 virtual void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element) = 0;
26 };
27
28 // Convenient interface for user defined types
30 {
31 private:
32
33 template <typename T>
34 struct XMLSerializationObject: public XMLSerializableBase
35 {
36 bool Binary;
37 std::string Name;
38 T* Object;
39
40 void Serialize(std::vector<char>& buffer) const {
41 serialize(*Object,Name,buffer);
42 }
43
44 void Deserialize(const std::vector<char>& buffer) {
45 deserialize(*Object,Name,buffer);
46 }
47
48 void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const {
49 serialize_xml(*Object,Name,doc,element,Binary);
50 }
51
52 void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element) {
53 deserialize_xml(*Object,Name,doc,element);
54 }
55 };
56
57 mutable bool initialized;
58 mutable std::vector<XMLSerializableBase*> objects;
59
60 public:
61
62 // Override this function to add your member variables which should be serialized
63 IGL_INLINE virtual void InitSerialization() = 0;
64
65 // Following functions can be overridden to handle the specific events.
66 // Return false to prevent the de-/serialization of an object.
67 IGL_INLINE virtual bool PreSerialization() const;
68 IGL_INLINE virtual void PostSerialization() const;
69 IGL_INLINE virtual bool PreDeserialization();
70 IGL_INLINE virtual void PostDeserialization();
71
72 // Default implementation of XMLSerializableBase interface
73 IGL_INLINE void Serialize(std::vector<char>& buffer) const;
74 IGL_INLINE void Deserialize(const std::vector<char>& buffer);
75 IGL_INLINE void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const;
76 IGL_INLINE void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element);
77
78 // Default constructor, destructor, assignment and copy constructor
83
84 // Use this function to add your variables which should be serialized
85 template <typename T>
86 IGL_INLINE void Add(T& obj,std::string name,bool binary = false);
87 };
88
89 // IMPLEMENTATION
90
92 {
93 return true;
94 }
95
99
101 {
102 return true;
103 }
104
108
109 IGL_INLINE void XMLSerializable::Serialize(std::vector<char>& buffer) const
110 {
111 if(this->PreSerialization())
112 {
113 if(initialized == false)
114 {
115 objects.clear();
116 (const_cast<XMLSerializable*>(this))->InitSerialization();
117 initialized = true;
118 }
119
120 for(unsigned int i=0;i<objects.size();i++)
121 objects[i]->Serialize(buffer);
122
123 this->PostSerialization();
124 }
125 }
126
127 IGL_INLINE void XMLSerializable::Deserialize(const std::vector<char>& buffer)
128 {
129 if(this->PreDeserialization())
130 {
131 if(initialized == false)
132 {
133 objects.clear();
134 (const_cast<XMLSerializable*>(this))->InitSerialization();
135 initialized = true;
136 }
137
138 for(unsigned int i=0;i<objects.size();i++)
139 objects[i]->Deserialize(buffer);
140
141 this->PostDeserialization();
142 }
143 }
144
145 IGL_INLINE void XMLSerializable::Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const
146 {
147 if(this->PreSerialization())
148 {
149 if(initialized == false)
150 {
151 objects.clear();
152 (const_cast<XMLSerializable*>(this))->InitSerialization();
153 initialized = true;
154 }
155
156 for(unsigned int i=0;i<objects.size();i++)
157 objects[i]->Serialize(doc,element);
158
159 this->PostSerialization();
160 }
161 }
162
163 IGL_INLINE void XMLSerializable::Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element)
164 {
165 if(this->PreDeserialization())
166 {
167 if(initialized == false)
168 {
169 objects.clear();
170 (const_cast<XMLSerializable*>(this))->InitSerialization();
171 initialized = true;
172 }
173
174 for(unsigned int i=0;i<objects.size();i++)
175 objects[i]->Deserialize(doc,element);
176
177 this->PostDeserialization();
178 }
179 }
180
182 {
183 initialized = false;
184 }
185
187 {
188 initialized = false;
189 objects.clear();
190 }
191
193 {
194 initialized = false;
195 objects.clear();
196 }
197
198
200 {
201 if(this != &obj)
202 {
203 if(initialized)
204 {
205 initialized = false;
206 objects.clear();
207 }
208 }
209 return *this;
210 }
211
212 template <typename T>
213 IGL_INLINE void XMLSerializable::Add(T& obj,std::string name,bool binary)
214 {
215 XMLSerializationObject<T>* object = new XMLSerializationObject<T>();
216 object->Binary = binary;
217 object->Name = name;
218 object->Object = &obj;
219
220 objects.push_back(object);
221 }
222
223 }
224}
225#endif
Definition XMLSerializable.h:30
virtual bool PreDeserialization()
Definition XMLSerializable.h:100
XMLSerializable & operator=(const XMLSerializable &obj)
Definition XMLSerializable.h:199
virtual void PostSerialization() const
Definition XMLSerializable.h:96
void Serialize(std::vector< char > &buffer) const
Definition XMLSerializable.h:109
~XMLSerializable()
Definition XMLSerializable.h:192
void Add(T &obj, std::string name, bool binary=false)
Definition XMLSerializable.h:213
virtual void InitSerialization()=0
XMLSerializable()
Definition XMLSerializable.h:181
virtual void PostDeserialization()
Definition XMLSerializable.h:105
virtual bool PreSerialization() const
Definition XMLSerializable.h:91
void Deserialize(const std::vector< char > &buffer)
Definition XMLSerializable.h:127
#define IGL_INLINE
Definition igl_inline.h:15
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
bool serialize(const T &obj, const std::string &filename)
Definition serialize.h:424
bool deserialize(T &obj, const std::string &filename)
Definition serialize.h:494
Definition serialize.h:159
Definition XMLSerializable.h:21
virtual void Serialize(tinyxml2::XMLDocument *doc, tinyxml2::XMLElement *element) const =0
virtual void Deserialize(const tinyxml2::XMLDocument *doc, const tinyxml2::XMLElement *element)=0
virtual void Serialize(std::vector< char > &buffer) const =0
virtual void Deserialize(const std::vector< char > &buffer)=0