My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
class XmlIO;

template <class T>
void XmlAttrLoad(T& var, const String& text)
{
var.XmlAttrLoad(text);
}

template <class T>
String XmlAttrStore(const T& var)
{
return var.XmlAttrStore();
}

class XmlIO {
XmlNode& node;
bool loading;
Value userdata;

public:
bool IsLoading() const { return loading; }
bool IsStoring() const { return !loading; }

XmlNode& Node() { return node; }
const XmlNode& Node() const { return node; }

XmlNode *operator->() { return &node; }

String GetAttr(const char *id) { return node.Attr(id); }
void SetAttr(const char *id, const String& val) { node.SetAttr(id, val); }

template <class T> XmlIO operator()(const char *tag, T& var);
template <class T> XmlIO operator()(const char *tag, const char *itemtag, T& var);

template <class T> XmlIO Attr(const char *id, T& var) {
if(IsLoading())
XmlAttrLoad(var, node.Attr(id));
else
node.SetAttr(id, XmlAttrStore(var));
return *this;
}

template <class T> XmlIO Attr(const char *id, T& var, T def) {
if(IsLoading())
if(IsNull(node.Attr(id)))
var = def;
else
XmlAttrLoad(var, node.Attr(id));
else
if(var != def)
node.SetAttr(id, XmlAttrStore(var));
return *this;
}

XmlIO At(int i) { XmlIO m(node.At(i), IsLoading(), userdata); return m; }
XmlIO Add() { XmlIO m(node.Add(), IsLoading(), userdata); return m; }
XmlIO Add(const char *id) { XmlIO m(node.Add(id), IsLoading(), userdata); return m; }
XmlIO GetAdd(const char *id) { XmlIO m(node.GetAdd(id), IsLoading(), userdata); return m; }

void SetUserData(const Value& v) { userdata = v; }
Value GetUserData() const { return userdata; }

XmlIO(XmlNode& xml, bool loading, const Value& userdata) : node(xml), loading(loading), userdata(userdata) {}
XmlIO(XmlNode& xml, bool loading) : node(xml), loading(loading) {}
XmlIO(XmlIO xml, const char *tag) : node(xml.node.GetAdd(tag)), loading(xml.loading), userdata(xml.userdata) {}
};

template <class T>
void Xmlize(XmlIO& xml, T& var)
{
var.Xmlize(xml);
}

template <class T>
void Xmlize(XmlIO& xml, const char* itemtag, T& var)
{
var.Xmlize(xml);
}

template <class T> XmlIO XmlIO::operator()(const char *tag, T& var) {
XmlIO n(*this, tag);
Xmlize(n, var);
return *this;
}

template <class T> XmlIO XmlIO::operator()(const char *tag, const char *itemtag, T& var) {
XmlIO n(*this, tag);
Xmlize(n, itemtag, var);
return *this;
}

template<> inline void XmlAttrLoad(String& var, const String& text) { var = text; }
template<> inline String XmlAttrStore(const String& var) { return var; }

template<> void XmlAttrLoad(WString& var, const String& text);
template<> String XmlAttrStore(const WString& var);
template<> void XmlAttrLoad(int& var, const String& text);
template<> String XmlAttrStore(const int& var);
template<> void XmlAttrLoad(dword& var, const String& text);
template<> String XmlAttrStore(const dword& var);
template<> void XmlAttrLoad(double& var, const String& text);
template<> String XmlAttrStore(const double& var);
template<> void XmlAttrLoad(bool& var, const String& text);
template<> String XmlAttrStore(const bool& var);
template <> void XmlAttrLoad(int16& var, const String& text);
template <> String XmlAttrStore(const int16& var);
template <> void XmlAttrLoad(int64& var, const String& text);
template <> String XmlAttrStore(const int64& var);
template <> void XmlAttrLoad(byte& var, const String& text);
template <> String XmlAttrStore(const byte& var);
template <> void XmlAttrLoad(Date& var, const String& text);
template <> String XmlAttrStore(const Date& var);
template <> void XmlAttrLoad(Time& var, const String& text);
template <> String XmlAttrStore(const Time& var);

template<> void Xmlize(XmlIO& xml, String& var);
template<> void Xmlize(XmlIO& xml, WString& var);
template<> void Xmlize(XmlIO& xml, int& var);
template<> void Xmlize(XmlIO& xml, dword& var);
template<> void Xmlize(XmlIO& xml, double& var);
template<> void Xmlize(XmlIO& xml, bool& var);
template<> void Xmlize(XmlIO& xml, Date& var);
template<> void Xmlize(XmlIO& xml, Time& var);
template<> void Xmlize(XmlIO& xml, int16& var);
template<> void Xmlize(XmlIO& xml, int64& var);
template<> void Xmlize(XmlIO& xml, byte& var);

#ifndef SVO_VALUE
template<> void Xmlize(XmlIO& xml, Color& c);
template<> void Xmlize(XmlIO& xml, Value& v);

template<> void Xmlize(XmlIO& xml, ValueArray& v);
template<> void Xmlize(XmlIO& xml, ValueMap& v);
#endif

void XmlizeLangAttr(XmlIO& xml, int& lang, const char *id = "lang");
void XmlizeLang(XmlIO& xml, const char *tag, int& lang, const char *id = "id");

template<class T>
void XmlizeContainer(XmlIO& xml, const char *tag, T& data)
{
if(xml.IsStoring())
for(int i = 0; i < data.GetCount(); i++) {
XmlIO io = xml.Add(tag);
Xmlize(io, data[i]);
}
else {
data.Clear();
for(int i = 0; i < xml->GetCount(); i++)
if(xml->Node(i).IsTag(tag)) {
XmlIO io = xml.At(i);
Xmlize(io, data.Add());
}
}
}

template<class T>
void Xmlize(XmlIO& xml, Vector<T>& data)
{
XmlizeContainer(xml, "item", data);
}

template<class T>
void Xmlize(XmlIO& xml, const char* itemtag, Vector<T>& data)
{
XmlizeContainer(xml, itemtag, data);
}

template<class T>
void Xmlize(XmlIO& xml, Array<T>& data)
{
XmlizeContainer(xml, "item", data);
}

template<class T>
void Xmlize(XmlIO& xml, const char* itemtag, Array<T>& data)
{
XmlizeContainer(xml, itemtag, data);
}

template<class T>
void XmlizeStore(XmlIO& xml, const T& data)
{
ASSERT(xml.IsStoring());
Xmlize(xml, const_cast<T&>(data));
}

template<class K, class V, class T>
void XmlizeMap(XmlIO& xml, const char *keytag, const char *valuetag, T& data)
{
if(xml.IsStoring()) {
for(int i = 0; i < data.GetCount(); i++)
if(!data.IsUnlinked(i)) {
XmlIO k = xml.Add(keytag);
XmlizeStore(k, data.GetKey(i));
XmlIO v = xml.Add(valuetag);
XmlizeStore(v, data[i]);
}
}
else {
data.Clear();
int i = 0;
while(i < xml->GetCount() - 1 && xml->Node(i).IsTag(keytag) && xml->Node(i + 1).IsTag(valuetag)) {
K key;
XmlIO k = xml.At(i++);
Xmlize(k, key);
XmlIO v = xml.At(i++);
Xmlize(v, data.Add(key));
}
}
}

template<class K, class V, class H>
void Xmlize(XmlIO& xml, VectorMap<K, V, H>& data)
{
XmlizeMap<K, V>(xml, "key", "value", data);
}

template<class K, class V, class H>
void Xmlize(XmlIO& xml, ArrayMap<K, V, H>& data)
{
XmlizeMap<K, V>(xml, "key", "value", data);
}

template<class K, class T>
void XmlizeIndex(XmlIO& xml, const char *keytag, T& data)
{
if(xml.IsStoring()) {
for(int i = 0; i < data.GetCount(); i++)
if(!data.IsUnlinked(i)) {
//XmlizeStore(xml.Add(keytag), data.GetKey(i)); //FIXME xmlize with hashfn awareness
XmlIO io = xml.Add(keytag);
XmlizeStore(io, data[i]);
}
}
else {
data.Clear();
int i = 0;
//while(i < xml->GetCount() - 1 && xml->Node(i).IsTag(keytag) && xml->Node(i + 1).IsTag(valuetag)) {
while(i < xml->GetCount() && xml->Node(i).IsTag(keytag)) {
//K key;
//Xmlize(xml.At(i++), key); //FIXME dexmlize with hashfn awareness
K k;
XmlIO io = xml.At(i++);
Xmlize(io, k);
data.Add(k);
}
}
}

template<class K, class H>
void Xmlize(XmlIO& xml, Index<K, H>& data)
{
XmlizeIndex<K>(xml, "key", data);
}

template<class K, class H>
void Xmlize(XmlIO& xml, ArrayIndex<K, H>& data)
{
XmlizeIndex<K>(xml, "key", data);
}

void RegisterValueXmlize(dword type, void (*xmlize)(XmlIO& xml, Value& v), const char *name);

template <class T>
void ValueXmlize(XmlIO& xml, Value& v)
{
T x;
if(xml.IsStoring())
x = v;
Xmlize(xml, x);
if(xml.IsLoading())
v = x;
}

#define REGISTER_VALUE_XMLIZE(T) \
INITBLOCK { RegisterValueXmlize(GetValueTypeNo<T>(), &ValueXmlize<T>, #T); }

template <class T>
struct ParamHelper__ {
T& data;
void Invoke(XmlIO xml) {
Xmlize(xml, data);
}

ParamHelper__(T& data) : data(data) {}
};

String StoreAsXML(Callback1<XmlIO> xmlize, const char *name);
bool LoadFromXML(Callback1<XmlIO> xmlize, const String& xml);

template <class T>
String StoreAsXML(const T& data, const char *name)
{
ParamHelper__<T> p(const_cast<T &>(data));
return StoreAsXML(callback(&p, &ParamHelper__<T>::Invoke), name);
}

template <class T>
bool LoadFromXML(T& data, const String& xml)
{
ParamHelper__<T> p(data);
return LoadFromXML(callback(&p, &ParamHelper__<T>::Invoke), xml);
}

bool StoreAsXMLFile(Callback1<XmlIO> xmlize, const char *name = NULL, const char *file = NULL);
bool LoadFromXMLFile(Callback1<XmlIO> xmlize, const char *file = NULL);

template <class T>
bool StoreAsXMLFile(T& data, const char *name = NULL, const char *file = NULL)
{
ParamHelper__<T> p(data);
return StoreAsXMLFile(callback(&p, &ParamHelper__<T>::Invoke), name, file);
}

template <class T>
bool LoadFromXMLFile(T& data, const char *file = NULL)
{
ParamHelper__<T> p(data);
return LoadFromXMLFile(callback(&p, &ParamHelper__<T>::Invoke), file);
}

template <class T>
void XmlizeBySerialize(XmlIO& xio, T& x)
{
String h;
if(xio.IsStoring())
h = HexString(StoreAsString(x));
xio.Attr("data", h);
if(xio.IsLoading())
try {
LoadFromString(x, ScanHexString(h));
}
catch(LoadingError) {
throw XmlError("xmlize by serialize error");
}
}


void StoreJsonValue(XmlIO& xio, const Value& v);
Value LoadJsonValue(const XmlNode& n);

template <class T>
void XmlizeByJsonize(XmlIO& xio, T& x)
{
if(xio.IsStoring())
StoreJsonValue(xio, StoreAsJsonValue(x));
else
LoadFromJsonValue(x, LoadJsonValue(xio.Node()));
}

Change log

r4975 by cxl on May 20, 2012   Diff
Core: XmlizeByJsonize
Go to: 
Project members, sign in to write a code review

Older revisions

r4956 by cxl on May 16, 2012   Diff
Core: svo_value Xmlize moved as method
for ValueArray, ValueMap
r4952 by cxl on May 14, 2012   Diff
*Core: XmlizeBySerialize fixed for GCC
r4950 by cxl on May 14, 2012   Diff
Draw: Font, Drawing, Painting, Image
now support Xmlize and Jsonize
All revisions of this file

File info

Size: 9666 bytes, 350 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting