1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
24 #if !BOOST_PP_IS_ITERATING
26 #ifndef LUABIND_CONSTRUCTOR_HPP_INCLUDED
27 #define LUABIND_CONSTRUCTOR_HPP_INCLUDED
29 #include <boost/config.hpp>
30 #include <boost/preprocessor/iterate.hpp>
31 #include <boost/preprocessor/repetition/enum.hpp>
32 #include <boost/preprocessor/repetition/enum_params.hpp>
33 #include <boost/preprocessor/repeat.hpp>
34 #include <boost/preprocessor/comma_if.hpp>
36 #include <boost/mpl/apply_wrap.hpp>
38 #include <luabind/config.hpp>
39 #include <luabind/wrapper_base.hpp>
40 #include <luabind/detail/policy.hpp>
41 #include <luabind/detail/signature_match.hpp>
42 #include <luabind/detail/call_member.hpp>
43 #include <luabind/wrapper_base.hpp>
44 #include <luabind/weak_ref.hpp>
46 namespace luabind
{ namespace detail
49 struct constructor_helper
;
51 namespace mpl
= boost::mpl
;
54 struct wrapped_constructor_helper
;
56 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/detail/constructor.hpp>, 1))
57 #include BOOST_PP_ITERATE()
59 template<class T
, class Policies
, class ConstructorSig
>
60 struct construct_class
62 inline static void* apply(lua_State
* L
, weak_ref
const& ref
)
64 typedef constructor_helper
<ConstructorSig::arity
> helper
;
65 return helper::execute(
75 template<class T
, class W
, class Policies
, class ConstructorSig
>
76 struct construct_wrapped_class
78 inline static void* apply(lua_State
* L
, weak_ref
const& ref
)
80 typedef wrapped_constructor_helper
<ConstructorSig::arity
> helper
;
81 return helper::execute(
94 #endif // LUABIND_CONSTRUCTOR_HPP_INCLUDED
97 #elif BOOST_PP_ITERATION_FLAGS() == 1
100 #define LUABIND_DECL(z, n, text) \
101 typedef typename find_conversion_policy<n+1,Policies>::type BOOST_PP_CAT(converter_policy,n); \
103 typename mpl::apply_wrap2< \
104 BOOST_PP_CAT(converter_policy,n), BOOST_PP_CAT(A,n), lua_to_cpp \
105 >::type BOOST_PP_CAT(c,n);
107 #define LUABIND_PARAM(z,n,text) \
108 BOOST_PP_CAT(c,n).apply(L, LUABIND_DECORATE_TYPE(BOOST_PP_CAT(A,n)), n + 2)
111 struct constructor_helper
<BOOST_PP_ITERATION()>
113 template<class T
, class Policies
, BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, class A
)>
118 , constructor
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
,A
)>*
121 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL
, _
)
122 return new T(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAM
, _
));
128 template<class Policies, BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY, class A)>
129 static T* call(lua_State* L, const constructor<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY,A)>*, const Policies*)
131 // L is used, but the metrowerks compiler warns about this before expanding the macros
133 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL, _)
134 return new T(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAM, _));
140 struct wrapped_constructor_helper
<BOOST_PP_ITERATION()>
142 template<class T
, class W
, class Policies
, BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, class A
)>
145 , weak_ref
const& ref
148 , constructor
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
,A
)>*
151 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL
, _
)
152 W
* result
= new W(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAM
, _
));
153 static_cast<weak_ref
&>(detail::wrap_access::ref(*result
)) = ref
;
160 template<class Policies, BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY, class A)>
161 static T* call(lua_State* L, weak_ref const& ref, const constructor<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY,A)>*, const Policies*)
163 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL, _)
164 T* o = new T(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAM, _));
165 static_cast<weak_ref&>(detail::wrap_access::ref(*o)) = ref;
175 #endif // LUABIND_CONSTRUCTOR_HPP_INCLUDED