Initial revision
[luabind.git] / luabind / detail / constructor.hpp
blob3a825ca8a70bb8f4a15efba2081457eae2446cc5
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 <luabind/config.hpp>
37 #include <luabind/detail/policy.hpp>
38 #include <luabind/detail/signature_match.hpp>
39 #include <luabind/detail/call_member.hpp>
41 namespace luabind { namespace detail
43 template<int N>
44 struct constructor_helper;
46 template<int N>
47 struct wrapped_constructor_helper;
49 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/detail/constructor.hpp>, 1))
50 #include BOOST_PP_ITERATE()
52 template<class T, class Policies, class ConstructorSig>
53 struct construct_class
55 inline static void* apply(lua_State* L)
57 typedef constructor_helper<ConstructorSig::arity> ConstrHelp;
58 typedef typename ConstrHelp::template apply<T> Caller;
59 return Caller::call(L, static_cast<const ConstructorSig*>(0), static_cast<const Policies*>(0));
63 template<class T, class Policies, class ConstructorSig>
64 struct construct_wrapped_class
66 inline static void* apply(lua_State* L, int ref)
68 typedef wrapped_constructor_helper<ConstructorSig::arity> ConstrHelp;
69 typedef typename ConstrHelp::apply<T> Caller;
70 return Caller::call(L, ref, static_cast<const ConstructorSig*>(0), static_cast<const Policies*>(0));
76 #endif // LUABIND_CONSTRUCTOR_HPP_INCLUDED
79 #elif BOOST_PP_ITERATION_FLAGS() == 1
82 #define LUABIND_DECL(z, n, text) typedef typename find_conversion_policy<n+1,Policies>::type BOOST_PP_CAT(converter_policy,n); \
83 typename BOOST_PP_CAT(converter_policy,n)::template generate_converter<A##n, lua_to_cpp>::type BOOST_PP_CAT(c,n);
84 #define LUABIND_PARAM(z,n,text) BOOST_PP_CAT(c,n).apply(L, LUABIND_DECORATE_TYPE(A##n), n + 2)
86 template<>
87 struct constructor_helper<BOOST_PP_ITERATION()>
89 template<class T>
90 struct apply
92 template<class Policies, BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY, class A)>
93 static T* call(lua_State* L, const constructor<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY,A)>*, const Policies*)
95 // L is used, but the metrowerks compiler warns about this before expanding the macros
96 L = L;
97 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL, _)
98 return new T(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAM, _));
103 template<>
104 struct wrapped_constructor_helper<BOOST_PP_ITERATION()>
106 template<class T>
107 struct apply
109 template<class Policies, BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY, class A)>
110 static T* call(lua_State* L, int ref, const constructor<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY,A)>*, const Policies*)
112 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL, _)
113 return new T(luabind::object(L, ref, true/*luabind::object::reference()*/) BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAM, _));
119 #undef LUABIND_PARAM
120 #undef LUABIND_DECL
122 #endif // LUABIND_CONSTRUCTOR_HPP_INCLUDED