-
Notifications
You must be signed in to change notification settings - Fork 43
/
create.hpp
357 lines (318 loc) · 9.29 KB
/
create.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
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
351
352
353
354
355
356
357
// 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_CREATE_HPP
#define FLUSSPFERD_CREATE_HPP
#ifndef PREPROC_DEBUG
#include "object.hpp"
#include "function.hpp"
#include "array.hpp"
#include "native_function.hpp"
#include "local_root_scope.hpp"
#include <boost/type_traits/is_function.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/mpl/bool.hpp>
#endif
#include "detail/limit.hpp"
#include <boost/preprocessor.hpp>
namespace flusspferd {
class native_object_base;
class function;
class native_function_base;
#ifndef IN_DOXYGEN
namespace detail {
object create_native_object(object const &proto);
object create_native_enumerable_object(object const &proto);
}
#endif
/**
* @name Creating values
* @addtogroup create
*/
//@{
/**
* Create a simple object.
*
* Creates a new object with prototype @p proto. If proto is @c null,
* @c Object.prototype will be used.
*
* @param proto The object to use as prototype.
* @return The new object.
*/
object create_object(object const &proto = object());
/**
* Create an array.
*
* @param length The initial length of the new array.
* @return The new array.
*/
array create_array(unsigned int length = 0);
#ifndef IN_DOXYGEN
#define FLUSSPFERD_FN_CREATE_NATIVE_OBJECT(z, n_args, d) \
template< \
typename T \
BOOST_PP_ENUM_TRAILING_PARAMS(n_args, typename T) \
> \
T &create_native_object( \
object proto \
BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(n_args, T, const & param), \
typename boost::enable_if< boost::mpl::not_<\
typename T::class_info::custom_enumerate > \
>::type * = 0 \
) { \
if (proto.is_null()) \
proto = current_context().prototype<T>(); \
local_root_scope scope; \
object obj = detail::create_native_object(proto); \
return *(new T(obj BOOST_PP_ENUM_TRAILING_PARAMS(n_args, param))); \
} \
\
template< \
typename T \
BOOST_PP_ENUM_TRAILING_PARAMS(n_args, typename T) \
> \
T &create_native_object( \
object proto \
BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(n_args, T, const & param), \
typename boost::enable_if< \
typename T::class_info::custom_enumerate >::type * = 0 \
) { \
if (proto.is_null()) \
proto = current_context().prototype<T>(); \
local_root_scope scope; \
object obj = detail::create_native_enumerable_object(proto); \
return *(new T(obj BOOST_PP_ENUM_TRAILING_PARAMS(n_args, param))); \
} \
/* */
BOOST_PP_REPEAT(
BOOST_PP_INC(FLUSSPFERD_PARAM_LIMIT),
FLUSSPFERD_FN_CREATE_NATIVE_OBJECT,
~
)
#else
/**
* Create a new native object of type @p T.
*
* @param T The type of the object's class.
* @param proto The prototype to be used. If @p null, the class' default
* prototype will be used.
* @param ... The parameters to the constructor of @p T.
* @return The new object.
*/
template<typename T>
T &create_native_object(object const &proto, ...);
#endif
//@}
/**
* @name Creating functions
* @addtogroup create_function
*/
//@{
function create_function(
std::string const &name,
unsigned n_args,
std::vector<std::string> argnames,
flusspferd::string const &body,
std::string const &file,
unsigned line);
/**
* Create a new native function.
*
* @p ptr will be <code>delete</code>d by Flusspferd.
*
* @param ptr The native function object.
* @return The new function.
*/
function create_native_function(native_function_base *ptr);
/**
* Create a new native function as method of an object.
*
* The new method of object @p o will have the name @c ptr->name().
*
* @param o The object to add the method to.
* @param ptr The native function object.
* @return The new method.
*/
function create_native_function(object const &o, native_function_base *ptr);
#ifndef IN_DOXYGEN
#define FLUSSPFERD_FN_CREATE_NATIVE_FUNCTION(z, n_args, d) \
template< \
typename T \
BOOST_PP_ENUM_TRAILING_PARAMS(n_args, typename T) \
> \
object create_native_functor_function( \
object const &o \
BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(n_args, T, const & param), \
typename boost::enable_if_c<!boost::is_function<T>::value>::type * = 0 \
) { \
return create_native_function(o, new T(BOOST_PP_ENUM_PARAMS(n_args, param))); \
} \
/* */
BOOST_PP_REPEAT(
BOOST_PP_INC(FLUSSPFERD_PARAM_LIMIT),
FLUSSPFERD_FN_CREATE_NATIVE_FUNCTION,
~
)
#else
/**
* Create a new native function of type @p F as method of an object.
*
* @p F must inherit from #native_function_base.
*
* The new method of object @p o will have the name @c ptr->name().
*
* @param F The functor type.
* @param o The object to add the method to.
* @param ... The parameters to pass to the constructor of @p F.
* @return The new method.
*/
template<typename F>
object create_native_functor_function(object const &o, ...);
#endif
/**
* Create a new native method of an object.
*
* @param o The object to add the method to.
* @param name The method name.
* @param fn The functor to call.
* @param arity The function arity.
* @return The new function.
*/
inline function create_native_function(
object const &o,
std::string const &name,
boost::function<void (call_context &)> const &fn,
unsigned arity = 0)
{
return create_native_functor_function<native_function<void> >(
o, fn, arity, name);
}
/**
* Create a new native method of an object.
*
* @param T The function signature to use.
* @param o The object to add the method to.
* @param name The function name.
* @param fn The functor to call.
* @return The new function.
*/
template<typename T>
function create_native_function(
object const &o,
std::string const &name,
boost::function<T> const &fn)
{
return create_native_functor_function<native_function<T,false> >(o, fn, name);
}
/**
* Create a new native method of an object.
*
* The first parameter passed will be 'this'.
*
* @param T The function signature to use.
* @param o The object to add the method to.
* @param name The function name.
* @param fn The functor to call.
* @return The new function.
*/
template<typename T>
function create_native_method(
object const &o,
std::string const &name,
boost::function<T> const &fn)
{
return create_native_functor_function<native_function<T,true> >(o, fn, name);
}
/**
* Create a new native method of an object.
*
* @param o The object to add the method to.
* @param name The method name.
* @param fnptr The function to call (also determines the function signature).
* @return The new method.
*/
template<typename T>
function create_native_function(
object const &o,
std::string const &name,
T *fnptr,
typename boost::enable_if_c<boost::is_function<T>::value>::type* =0)
{
return create_native_function<T>(o, name, boost::function<T>(fnptr));
}
/**
* Create a new native method of an object.
*
* The first parameter passed will be 'this'.
*
* @param o The object to add the method to.
* @param name The method name.
* @param fnptr The function to call (also determines the function signature).
* @return The new method.
*/
template<typename T>
function create_native_method(
object const &o,
std::string const &name,
T *fnptr,
typename boost::enable_if_c<boost::is_function<T>::value>::type* =0)
{
return create_native_method<T>(o, name, boost::function<T>(fnptr));
}
/**
* Create a new native method of an object.
*
* @param o The object to add the method to.
* @param name The method name.
* @param memfnptr The member function to call.
* @return The new function.
*/
template<typename T>
function create_native_method(
object const &o,
std::string const &name,
void (T::*memfnptr)(call_context &),
unsigned arity = 0)
{
return create_native_functor_function<native_member_function<void, T> >(
o, memfnptr, arity, name);
}
/**
* Create a new native method of an object.
*
* @param o The object to add the method to.
* @param name The method name.
* @param memfnptr The member function to call (also determines the function
* signature).
* @return The new function.
*/
template<typename R, typename T>
function create_native_method(
object const &o,
std::string const &name,
R T::*memfnptr)
{
return create_native_functor_function<native_member_function<R, T> >(
o, memfnptr, name);
}
//@}
}
#endif