forked from Flusspferd/flusspferd
/
convert.hpp
320 lines (257 loc) · 6.93 KB
/
convert.hpp
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
// vim:ts=2:sw=2:expandtab:autoindent:filetype=cpp:
/*
The MIT License
Copyright (c) 2008, 2009 Flusspferd contributors (see "CONTRIBUTORS" or
http://flusspferd.org/contributors.txt)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef FLUSSPFERD_CONVERT_HPP
#define FLUSSPFERD_CONVERT_HPP
#include "value.hpp"
#include "root.hpp"
#include "exception.hpp"
//#include "spidermonkey/string.hpp"
#include <boost/noncopyable.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_float.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/type_traits/remove_cv.hpp>
#include <boost/optional.hpp>
#include <limits>
#include <vector>
#include <list>
namespace flusspferd {
class string;
class object;
class function;
class array;
class native_object_base;
class native_function_base;
#ifndef IN_DOXYGEN
namespace detail {
template<typename T, typename Condition = void>
struct convert;
template<
typename T,
typename O = typename boost::remove_cv<T>::type,
typename Condition = void
>
struct convert_ptr{
struct to_value {
typename convert<O>::to_value base;
value perform(T *p) {
return base.perform(*p);
}
};
struct from_value {
typename convert<O>::from_value base;
boost::optional<O> holder;
T *perform(value const &v) {
holder = base.perform(v);
return &holder.get();
}
};
};
template<typename T, typename C = T>
struct to_value_helper {
value perform(T const &x) {
return value(C(x));
}
};
template<typename T>
struct to_value_helper<T, T> {
value perform(T const &x) {
return value(x);
}
};
template<>
struct convert<value> {
struct to_value {
value const &perform(value const &v) {
return v;
}
};
typedef to_value from_value;
};
template<>
struct convert<bool> {
typedef to_value_helper<bool> to_value;
struct from_value {
bool perform(value const &v) {
return v.to_boolean();
}
};
};
template<typename T>
struct convert<T *> : convert_ptr<T> {};
template<typename T>
struct convert<T &> {
struct to_value {
typename convert_ptr<T>::to_value base;
value perform(T &x) {
return base.perform(&x);
}
};
struct from_value {
typename convert_ptr<T>::from_value base;
T &perform(value const &x) {
return *base.perform(x);
}
};
};
template<typename T>
struct convert<
T,
typename boost::enable_if<boost::is_integral<T> >::type
>
{
typedef to_value_helper<T> to_value;
struct from_value {
typedef std::numeric_limits<T> limits;
T perform(value const &v) {
double num = v.to_number();
if (num < double(limits::min()) || num > double(limits::max()))
throw exception("Not inside integer range", "RangeError");
return T(num);
}
};
};
template<typename T>
struct convert<
T,
typename boost::enable_if<boost::is_float<T> >::type
>
{
typedef to_value_helper<T> to_value;
struct from_value {
T perform(value const &v) {
return v.to_number();
}
};
};
template<typename T>
struct convert< boost::optional<T> > {
struct to_value {
typename convert<T>::to_value base;
value perform(boost::optional<T> const &x) {
if (!x)
return value();
return base.perform(x.get());
}
};
struct from_value {
typename convert<T>::from_value base;
boost::optional<T> perform(value const &v) {
if (v.is_undefined() || v.is_null())
return boost::optional<T>();
return base.perform(v);
}
};
};
template<typename T>
struct convert< detail::root<T> > {
struct to_value {
typename convert<T>::to_value base;
value perform(detail::root<T> const &x) {
return base.perform(x);
}
};
typedef typename convert<T>::from_value from_value;
};
template<typename T, std::size_t N>
struct convert<T [N]> {
typedef typename convert<T const *>::to_value to_value;
typedef void from_value;
};
template<>
struct convert<object>;
template<>
struct convert<function>;
template<>
struct convert<array>;
template<>
struct convert<string>;
template<>
struct convert<char const *>;
template<>
struct convert<std::string>;
template<>
struct convert<std::basic_string<jschar> >;
template<typename T>
struct convert_ptr<T, native_object_base>;
template<typename T>
struct convert_ptr<T, native_function_base>;
struct convert_container_base {
struct to_value {
value start();
void add(value obj, value x);
};
struct from_value {
std::size_t length(value obj);
value element(value obj, std::size_t i);
};
};
template<typename Container>
struct convert_container {
struct to_value {
convert_container_base::to_value base;
root_value root;
typename convert<typename Container::value_type>::to_value item_converter;
value perform(Container const &cont) {
root = base.start();
for (typename Container::const_iterator it = cont.begin();
it != cont.end();
++it)
{
base.add(root, item_converter.perform(*it));
}
return root;
}
};
struct from_value {
convert_container_base::from_value base;
typename convert<typename Container::value_type>::from_value
item_converter;
Container perform(value val) {
Container result;
std::size_t length = base.length(val);
result.reserve(length);
for (std::size_t i = 0; i < length; ++i)
result.push_back(item_converter.perform(base.element(val, i)));
return result;
}
};
};
template<typename T, typename A>
struct convert< std::vector<T, A> >
: convert_container< std::vector<T, A> > {};
template<typename T, typename A>
struct convert< std::list<T, A> >
: convert_container< std::list<T, A> > {};
}
#endif
template<typename T>
class convert : private detail::convert<T>, private boost::noncopyable {
private:
typedef detail::convert<T> base;
public:
typedef typename base::to_value to_value;
typedef typename base::from_value from_value;
};
}
#endif