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
|
// **************************************************************************
// * This file is part of the zenXML project. It is distributed under the *
// * Boost Software License: http://www.boost.org/LICENSE_1_0.txt *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
// **************************************************************************
#ifndef ZEN_XML_CONVERT_STRUC_HEADER_018727409908342709743
#define ZEN_XML_CONVERT_STRUC_HEADER_018727409908342709743
#include "dom.h"
namespace zen
{
/**
\file
\brief Handle conversion of arbitrary types to and from XML elements.
See comments in cvrt_text.h
*/
///Convert XML element to structured user data
/**
\param input The input XML element.
\param value Conversion target value.
\return "true" if value was read successfully.
*/
template <class T> bool readStruc(const XmlElement& input, T& value);
///Convert structured user data into an XML element
/**
\param value The value to be converted.
\param output The output XML element.
*/
template <class T> void writeStruc(const T& value, XmlElement& output);
//------------------------------ implementation -------------------------------------
namespace impl_2384343
{
ZEN_INIT_DETECT_MEMBER_TYPE(value_type);
ZEN_INIT_DETECT_MEMBER_TYPE(iterator);
ZEN_INIT_DETECT_MEMBER_TYPE(const_iterator);
ZEN_INIT_DETECT_MEMBER(begin) //
ZEN_INIT_DETECT_MEMBER(end) //we don't know the exact declaration of the member attribute: may be in a base class!
ZEN_INIT_DETECT_MEMBER(insert) //
}
template <typename T>
struct IsStlContainer :
StaticBool<
impl_2384343::HasMemberType_value_type <T>::value &&
impl_2384343::HasMemberType_iterator <T>::value &&
impl_2384343::HasMemberType_const_iterator<T>::value &&
impl_2384343::HasMember_begin <T>::value &&
impl_2384343::HasMember_end <T>::value &&
impl_2384343::HasMember_insert <T>::value> {};
namespace impl_2384343
{
ZEN_INIT_DETECT_MEMBER_TYPE(first_type);
ZEN_INIT_DETECT_MEMBER_TYPE(second_type);
ZEN_INIT_DETECT_MEMBER(first) //we don't know the exact declaration of the member attribute: may be in a base class!
ZEN_INIT_DETECT_MEMBER(second) //
}
template <typename T>
struct IsStlPair :
StaticBool<
impl_2384343::HasMemberType_first_type <T>::value &&
impl_2384343::HasMemberType_second_type<T>::value &&
impl_2384343::HasMember_first <T>::value &&
impl_2384343::HasMember_second <T>::value> {};
//######################################################################################
//Conversion from arbitrary types to an XML element
enum ValueType
{
VALUE_TYPE_STL_CONTAINER,
VALUE_TYPE_STL_PAIR,
VALUE_TYPE_OTHER,
};
template <class T>
struct GetValueType : StaticEnum<ValueType,
GetTextType<T>::value != TEXT_TYPE_OTHER ? VALUE_TYPE_OTHER : //some string classes are also STL containers, so check this first
IsStlContainer<T>::value ? VALUE_TYPE_STL_CONTAINER :
IsStlPair<T>::value ? VALUE_TYPE_STL_PAIR :
VALUE_TYPE_OTHER> {};
template <class T, ValueType type>
struct ConvertElement;
/* -> expected interface
{
void writeStruc(const T& value, XmlElement& output) const;
bool readStruc(const XmlElement& input, T& value) const;
};
*/
//partial specialization: handle conversion for all STL-container types!
template <class T>
struct ConvertElement<T, VALUE_TYPE_STL_CONTAINER>
{
void writeStruc(const T& value, XmlElement& output) const
{
std::for_each(value.begin(), value.end(),
[&](const typename T::value_type & childVal)
{
XmlElement& newChild = output.addChild("Item");
zen::writeStruc(childVal, newChild);
});
}
bool readStruc(const XmlElement& input, T& value) const
{
bool success = true;
value.clear();
auto iterPair = input.getChildren("Item");
for (auto iter = iterPair.first; iter != iterPair.second; ++iter)
{
typename T::value_type childVal; //MSVC 2010 bug: cannot put this into a lambda body
if (zen::readStruc(*iter, childVal))
value.insert(value.end(), childVal);
else
success = false;
}
return success;
}
};
//partial specialization: handle conversion for std::pair
template <class T>
struct ConvertElement<T, VALUE_TYPE_STL_PAIR>
{
void writeStruc(const T& value, XmlElement& output) const
{
XmlElement& child1 = output.addChild("one"); //don't use "1st/2nd", this will confuse a few pedantic XML parsers
zen::writeStruc(value.first, child1);
XmlElement& child2 = output.addChild("two");
zen::writeStruc(value.second, child2);
}
bool readStruc(const XmlElement& input, T& value) const
{
bool success = true;
const XmlElement* child1 = input.getChild("one");
if (!child1 || !zen::readStruc(*child1, value.first))
success = false;
const XmlElement* child2 = input.getChild("two");
if (!child2 || !zen::readStruc(*child2, value.second))
success = false;
return success;
}
};
//partial specialization: not a pure structured type, try text conversion (thereby respect user specializations of writeText()/readText())
template <class T>
struct ConvertElement<T, VALUE_TYPE_OTHER>
{
void writeStruc(const T& value, XmlElement& output) const
{
std::string tmp;
writeText(value, tmp);
output.setValue(tmp);
}
bool readStruc(const XmlElement& input, T& value) const
{
std::string rawStr;
input.getValue(rawStr);
return readText(rawStr, value);
}
};
template <class T> inline
void writeStruc(const T& value, XmlElement& output)
{
ConvertElement<T, GetValueType<T>::value>().writeStruc(value, output);
}
template <class T> inline
bool readStruc(const XmlElement& input, T& value)
{
return ConvertElement<T, GetValueType<T>::value>().readStruc(input, value);
}
}
#endif //ZEN_XML_CONVERT_STRUC_HEADER_018727409908342709743
|