-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxml_base64.h
334 lines (273 loc) · 9.8 KB
/
xml_base64.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#pragma once
#include <fstream>
#include <string>
#include <utility>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <typeinfo>
#include <sstream>
#include <filesystem>
#include "base64.h"
#include "tinyxml2.h"
using namespace std;
using namespace tinyxml2;
namespace xml_base64 {
// arithmetic
template<typename T> void serialize_process(T& object, XMLDocument& doc, XMLElement* root, const char* labelName, typename enable_if<is_arithmetic<T>::value>::type* = 0) {
XMLElement* newNode = doc.NewElement(labelName);
ostringstream oss;
oss << object;
string str = encode_base64(oss.str());
newNode->SetAttribute("val", str.c_str());
root->InsertEndChild(newNode);
}
template<typename T> void deserialize_process(T& object, bool isClass, XMLElement*& root, typename enable_if<is_arithmetic<T>::value>::type* = 0) {
if (!root) {
cout << "ERROR: Data type mismatch." << endl;
}
// get the value
const char* v = root->Attribute("val");
if (v) {
bool b = true;
string str(v);
string value = decode_base64(str);
// if object is char
if (typeid(object).name() == typeid('c').name()) {
object = value.at(0);
}
// if object is bool
else if (typeid(object).name() == typeid(b).name()) {
if (value.at(0) == '1')
object = true;
else
object = false;
}
else {
istringstream iss(value);
iss >> object;
}
if (isClass) {
root = root->NextSiblingElement();
}
}
else {
cout << "ERROR: Data type mismatch." << endl;
}
}
// string
void serialize_process(string& object, XMLDocument& doc, XMLElement* root, const char* labelName) {
XMLElement* newNode = doc.NewElement(labelName);
string str = encode_base64(object);
newNode->SetAttribute("val", str.c_str());
root->InsertEndChild(newNode);
}
void deserialize_process(string& object, bool isClass, XMLElement*& root) {
if (!root) {
cout << "ERROR: Data type mismatch." << endl;
}
// get the value
const char* v = root->Attribute("val");
if (v) {
string str(v);
string value = decode_base64(str);
object = value;
if (isClass) {
root = root->NextSiblingElement();
}
}
else {
cout << "ERROR: Data type mismatch." << endl;
}
}
// pair
template <typename T1, typename T2> void serialize_process(pair<T1, T2>& object, XMLDocument& doc, XMLElement* root, const char* labelName) {
XMLElement* newNode = doc.NewElement(labelName);
root->InsertEndChild(newNode);
xml_base64::serialize_process(object.first, doc, newNode, "first");
xml_base64::serialize_process(object.second, doc, newNode, "second");
}
template <typename T1, typename T2> void deserialize_process(pair<T1, T2>& object, bool isClass, XMLElement*& root) {
if (!root) {
cout << "ERROR: Data type mismatch." << endl;
}
XMLElement* first = root->FirstChildElement("first");
XMLElement* second = root->FirstChildElement("second");
xml_base64::deserialize_process(object.first, false, first);
xml_base64::deserialize_process(object.second, false, second);
if (isClass) {
root = root->NextSiblingElement();
}
}
// vector
template <typename T> void serialize_process(vector<T>& object, XMLDocument& doc, XMLElement* root, const char* labelName) {
int size = object.size();
XMLElement* newNode = doc.NewElement(labelName);
newNode->SetAttribute("size", size);
root->InsertEndChild(newNode);
for (typename vector<T>::iterator iter = object.begin(); iter != object.end(); iter++) {
xml_base64::serialize_process(*iter, doc, newNode, "vector_item");
}
}
template <typename T> void deserialize_process(vector<T>& object, bool isClass, XMLElement*& root) {
if (!root) {
cout << "ERROR: Data type mismatch." << endl;
}
XMLElement* child = root->FirstChildElement();
T item;
while (child != NULL) {
xml_base64::deserialize_process(item, false, child);
object.push_back(item);
child = child->NextSiblingElement();
}
if (isClass) {
root = root->NextSiblingElement();
}
}
// list
template <typename T> void serialize_process(list<T>& object, XMLDocument& doc, XMLElement* root, const char* labelName) {
int size = object.size();
XMLElement* newNode = doc.NewElement(labelName);
newNode->SetAttribute("size", size);
root->InsertEndChild(newNode);
for (typename list<T>::iterator iter = object.begin(); iter != object.end(); iter++) {
xml_base64::serialize_process(*iter, doc, newNode, "list_item");
}
}
template <typename T> void deserialize_process(list<T>& object, bool isClass, XMLElement*& root) {
if (!root) {
cout << "ERROR: Data type mismatch." << endl;
}
XMLElement* child = root->FirstChildElement();
T item;
while (child != NULL) {
xml_base64::deserialize_process(item, false, child);
object.push_back(item);
child = child->NextSiblingElement();
}
if (isClass) {
root = root->NextSiblingElement();
}
}
// set
template <typename T> void serialize_process(set<T>& object, XMLDocument& doc, XMLElement* root, const char* labelName) {
int size = object.size();
XMLElement* newNode = doc.NewElement(labelName);
newNode->SetAttribute("size", size);
root->InsertEndChild(newNode);
for (typename set<T>::iterator iter = object.begin(); iter != object.end(); iter++) {
xml_base64::serialize_process(*iter, doc, newNode, "set_item");
}
}
template <typename T> void deserialize_process(set<T>& object, bool isClass, XMLElement*& root) {
if (!root) {
cout << "ERROR: Data type mismatch." << endl;
}
XMLElement* child = root->FirstChildElement();
T item;
while (child != NULL) {
xml_base64::deserialize_process(item, false, child);
object.insert(item);
child = child->NextSiblingElement();
}
if (isClass) {
root = root->NextSiblingElement();
}
}
// map
template <typename K, typename V> void serialize_process(map<K, V>& object, XMLDocument& doc, XMLElement* root, const char* labelName) {
int size = object.size();
XMLElement* newNode = doc.NewElement(labelName);
newNode->SetAttribute("size", size);
root->InsertEndChild(newNode);
for (typename map<K, V>::iterator iter = object.begin(); iter != object.end(); iter++) {
XMLElement* pairNode = doc.NewElement("map_item");
newNode->InsertEndChild(pairNode);
xml_base64::serialize_process(iter->first, doc, newNode->LastChildElement(), "first");
xml_base64::serialize_process(iter->second, doc, newNode->LastChildElement(), "second");
}
}
template <typename K, typename V> void deserialize_process(map<K, V>& object, bool isClass, XMLElement*& root) {
if (!root) {
cout << "ERROR: Data type mismatch." << endl;
}
XMLElement* child = root->FirstChildElement();
K key;
V value;
while (child != NULL) {
XMLElement* first = child->FirstChildElement("first");
XMLElement* second = child->FirstChildElement("second");
xml_base64::deserialize_process(key, false, first);
xml_base64::deserialize_process(value, false, second);
object.insert(map<K, V>::value_type(key, value));
child = child->NextSiblingElement();
}
if (isClass) {
root = root->NextSiblingElement();
}
}
// user-dedoced types
template<typename T> void serialize_process(T& object, XMLDocument& doc, XMLElement* root, const char* labelName, typename enable_if<is_class<T>::value>::type* = 0) {
XMLElement* newNode = doc.NewElement(labelName);
root->InsertEndChild(newNode);
object.serialize_xml_base64(doc, newNode, "class_member");
}
template<typename T> void deserialize_process(T& object, bool isClass, XMLElement* root, typename enable_if<is_class<T>::value>::type* = 0) {
XMLElement* newNode = root->FirstChildElement();
object.deserialize_xml_base64(true, newNode);
}
template <typename... T> void enable_xml_base64_serialize(XMLDocument& doc, XMLElement* root, const char* labelName, T... object) {
(xml_base64::serialize_process(object, doc, root, labelName), ...);
}
template <typename... T> void enable_xml_base64_deserialize(bool isClass, XMLElement*& root, T&... object) {
(xml_base64::deserialize_process(object, isClass, root), ...);
}
// smart pointer
template <typename T> void serialize_process(unique_ptr<T>& object, XMLDocument& doc, XMLElement* root, const char* labelName) {
serialize_process(*(object.get()), doc, root, labelName);
}
template <typename T> void deserialize_process(unique_ptr<T>& object, bool isClass, XMLElement*& root) {
object.reset(new T);
deserialize_process(*(object.get()), false, root);
}
template <typename T> void serialize_process(shared_ptr<T>& object, XMLDocument& doc, XMLElement* root, const char* labelName) {
serialize_process(*(object.get()), doc, root, labelName);
}
template <typename T> void deserialize_process(shared_ptr<T>& object, bool isClass, XMLElement*& root) {
object.reset(new T);
deserialize_process(*(object.get()), false, root);
}
// interface
template <typename T> int serialize_xml_base64(T& object, const char * labelName, const char* xmlPath) {
XMLDocument doc;
if (doc.LoadFile(xmlPath) != 3) {
filesystem::remove(xmlPath);
doc.LoadFile(xmlPath);
}
// add declaration
XMLDeclaration* declaration = doc.NewDeclaration();
doc.InsertFirstChild(declaration);
XMLElement* root = doc.NewElement("serialization");
doc.InsertEndChild(root);
xml_base64::serialize_process(object, doc, root, labelName);
return doc.SaveFile(xmlPath);
}
template <typename T> int deserialize_xml_base64(T& object, const char * label_name, const char* file_path) {
XMLDocument doc;
if (doc.LoadFile(file_path) != 0) {
cout << "ERROR: Failed to load file " << file_path << endl;
return 0;
}
XMLElement* root = doc.RootElement();
XMLElement* node = root->FirstChildElement(label_name);
if (node != NULL) {
xml_base64::deserialize_process(object, false, node);
return 1;
}
else {
cout << "ERROR: The file '" << file_path << "' do not have element named '" << label_name << "'." << endl;
return 0;
}
}
};