Branched for 0.7.1 release.
[luabind.git] / luabind / detail / constructor.hpp
blob410a949712b2fa7d746281ac268fb1bc3fc33edf
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
48 template<int N>
49 struct constructor_helper;
51 namespace mpl = boost::mpl;
53 template<int N>
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(
67 , ref
68 , (T*)0
69 , (ConstructorSig*)0
70 , (Policies*)0
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(
83 , ref
84 , (T*)0
85 , (W*)0
86 , (ConstructorSig*)0
87 , (Policies*)0
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)
110 template<>
111 struct constructor_helper<BOOST_PP_ITERATION()>
113 template<class T, class Policies, BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY, class A)>
114 static T* execute(
115 lua_State* L
116 , weak_ref const&
117 , T*
118 , constructor<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY,A)>*
119 , Policies*)
121 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL, _)
122 return new T(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAM, _));
125 template<class T>
126 struct apply
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
132 L = L;
133 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL, _)
134 return new T(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAM, _));
136 };*/
139 template<>
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)>
143 static T* execute(
144 lua_State* L
145 , weak_ref const& ref
146 , T*
147 , W*
148 , constructor<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY,A)>*
149 , Policies*)
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;
154 return result;
157 template<class T>
158 struct apply
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;
166 return o;
168 };*/
172 #undef LUABIND_PARAM
173 #undef LUABIND_DECL
175 #endif // LUABIND_CONSTRUCTOR_HPP_INCLUDED