-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdynamic.hpp
More file actions
340 lines (269 loc) · 7.42 KB
/
Copy pathdynamic.hpp
File metadata and controls
340 lines (269 loc) · 7.42 KB
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
#ifndef COCAINE_DYNAMIC_HPP
#define COCAINE_DYNAMIC_HPP
#include <boost/variant.hpp>
#include <string>
#include <vector>
#include <map>
#include <type_traits>
namespace cocaine {
class dynamic_t;
namespace detail { namespace dynamic {
template<class ConstVisitor, class Result>
struct const_visitor_applier :
public boost::static_visitor<Result>
{
const_visitor_applier(ConstVisitor v) :
m_const_visitor(v)
{
// pass
}
template<class T>
Result
operator()(T& v) const {
return m_const_visitor(static_cast<const T&>(v));
}
private:
ConstVisitor m_const_visitor;
};
template<class T>
struct my_decay {
typedef typename std::remove_reference<T>::type unref;
typedef typename std::remove_cv<unref>::type type;
};
class object_t :
public std::map<std::string, cocaine::dynamic_t>
{
typedef std::map<std::string, cocaine::dynamic_t>
base_type;
public:
object_t() {
// pass
}
template<class InputIt>
object_t(InputIt first, InputIt last) :
base_type(first, last)
{
// pass
}
object_t(const object_t& other) :
base_type(other)
{
// pass
}
object_t(object_t&& other) :
base_type(std::move(other))
{
// pass
}
object_t(std::initializer_list<value_type> init) :
base_type(init)
{
// pass
}
object_t(const base_type& other) :
base_type(other)
{
// pass
}
object_t(base_type&& other) :
base_type(std::move(other))
{
// pass
}
object_t&
operator=(const object_t& other) {
base_type::operator=(other);
return *this;
}
object_t&
operator=(object_t&& other) {
base_type::operator=(std::move(other));
return *this;
}
using base_type::at;
cocaine::dynamic_t&
at(const std::string& key, cocaine::dynamic_t& def);
const cocaine::dynamic_t&
at(const std::string& key, const cocaine::dynamic_t& def) const;
using base_type::operator[];
const cocaine::dynamic_t&
operator[](const std::string& key) const;
};
}} // namespace detail::dynamic
template<class From, class = void>
struct dynamic_constructor {
static const bool enable = false;
};
template<class To, class = void>
struct dynamic_converter {
// pass
};
class dynamic_t {
public:
struct null_t {
bool
operator==(const null_t&) const {
return true;
}
};
typedef bool
bool_t;
typedef int64_t
int_t;
typedef double
double_t;
typedef std::string
string_t;
typedef std::vector<dynamic_t>
array_t;
typedef detail::dynamic::object_t
object_t;
typedef boost::variant<null_t,
bool_t,
int_t,
double_t,
string_t,
array_t,
object_t>
value_t;
public:
dynamic_t();
dynamic_t(const dynamic_t& other);
dynamic_t(dynamic_t&& other);
template<class T>
dynamic_t(
T&& from,
typename std::enable_if<dynamic_constructor<typename detail::dynamic::my_decay<T>::type>::enable>::type* = 0
);
dynamic_t&
operator=(const dynamic_t& other);
dynamic_t&
operator=(dynamic_t&& other);
template<class T>
typename std::enable_if<dynamic_constructor<typename detail::dynamic::my_decay<T>::type>::enable, dynamic_t&>::type
operator=(T&& from);
bool
operator==(const dynamic_t& other) const;
bool
operator!=(const dynamic_t& other) const;
template<class Visitor>
typename Visitor::result_type
apply(Visitor& visitor) {
return boost::apply_visitor(visitor, m_value);
}
template<class Visitor>
typename Visitor::result_type
apply(const Visitor& visitor) {
return boost::apply_visitor(visitor, m_value);
}
template<class Visitor>
typename Visitor::result_type
apply(const Visitor& visitor) const {
return boost::apply_visitor(
detail::dynamic::const_visitor_applier<const Visitor&, typename Visitor::result_type>(visitor),
m_value
);
}
template<class Visitor>
typename Visitor::result_type
apply(Visitor& visitor) const {
return boost::apply_visitor(
detail::dynamic::const_visitor_applier<Visitor&, typename Visitor::result_type>(visitor),
m_value
);
}
bool
is_null() const;
bool
is_bool() const;
bool
is_int() const;
bool
is_double() const;
bool
is_string() const;
bool
is_array() const;
bool
is_object() const;
bool_t
as_bool() const;
int_t
as_int() const;
double_t
as_double() const;
const string_t&
as_string() const;
const array_t&
as_array() const;
const object_t&
as_object() const;
string_t&
as_string();
array_t&
as_array();
object_t&
as_object();
// it returns bool, but is enabled only for T for which dynamic_converter::result_type is defined
template<class T>
typename std::conditional<
true,
bool,
typename dynamic_converter<typename detail::dynamic::my_decay<T>::type>::result_type
>::type
convertible_to() const;
template<class T>
typename dynamic_converter<typename detail::dynamic::my_decay<T>::type>::result_type
to() const;
private:
template<class T>
T&
get() {
return boost::get<T>(m_value);
}
template<class T>
const T&
get() const {
return boost::get<T>(m_value);
}
template<class T>
bool
is() const {
return static_cast<bool>(boost::get<T>(&m_value));
}
private:
// boost::apply_visitor takes non-constant reference to variable object
mutable value_t m_value;
};
template<class T>
dynamic_t::dynamic_t(
T&& from,
typename std::enable_if<dynamic_constructor<typename detail::dynamic::my_decay<T>::type>::enable>::type*
) : m_value(null_t())
{
dynamic_constructor<typename detail::dynamic::my_decay<T>::type>::convert(std::forward<T>(from), m_value);
}
template<class T>
typename std::enable_if<dynamic_constructor<typename detail::dynamic::my_decay<T>::type>::enable, dynamic_t&>::type
dynamic_t::operator=(T&& from) {
dynamic_constructor<typename detail::dynamic::my_decay<T>::type>::convert(std::forward<T>(from), m_value);
return *this;
}
template<class T>
typename std::conditional<
true,
bool,
typename dynamic_converter<typename detail::dynamic::my_decay<T>::type>::result_type
>::type
dynamic_t::convertible_to() const {
return dynamic_converter<typename detail::dynamic::my_decay<T>::type>::convertible(*this);
}
template<class T>
typename dynamic_converter<typename detail::dynamic::my_decay<T>::type>::result_type
dynamic_t::to() const {
return dynamic_converter<typename detail::dynamic::my_decay<T>::type>::convert(*this);
}
} // namespace cocaine
#include "constructors.hpp"
#include "converters.hpp"
#endif // COCAINE_DYNAMIC_HPP