1 // Copyright Daniel Wallin 2008. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #if !BOOST_PP_IS_ITERATING
7 # ifndef LUABIND_DETAIL_CONSTRUCTOR_081018_HPP
8 # define LUABIND_DETAIL_CONSTRUCTOR_081018_HPP
10 # include <luabind/object.hpp>
11 # include <luabind/wrapper_base.hpp>
13 # include <boost/preprocessor/iteration/iterate.hpp>
14 # include <boost/preprocessor/iteration/local.hpp>
15 # include <boost/preprocessor/repetition/enum_params.hpp>
16 # include <boost/preprocessor/repetition/enum_binary_params.hpp>
18 namespace luabind
{ namespace detail
{
20 inline void inject_backref(lua_State
* L
, void*, void*)
24 void inject_backref(lua_State
* L
, T
* p
, wrap_base
*)
26 weak_ref(L
, -1).swap(wrap_access::ref(*p
));
29 template <std::size_t Arity
, class T
, class Signature
>
32 template <class T
, class Signature
>
34 : construct_aux
<mpl::size
<Signature
>::value
- 2, T
, Signature
>
37 template <class T
, class Signature
>
38 struct construct_aux
<0, T
, Signature
>
40 void operator()(argument
const& self_
) const
42 object_rep
* self
= touserdata
<object_rep
>(self_
);
43 class_rep
* cls
= self
->crep();
45 std::auto_ptr
<T
> instance(new T
);
46 inject_backref(self_
.interpreter(), instance
.get(), instance
.get());
48 if (cls
->has_holder())
50 cls
->construct_holder()(self
->ptr(), instance
.get());
54 self
->set_object(instance
.get());
57 self
->set_destructor(cls
->destructor());
62 # define BOOST_PP_ITERATION_PARAMS_1 \
63 (3, (1, LUABIND_MAX_ARITY, <luabind/detail/constructor.hpp>))
64 # include BOOST_PP_ITERATE()
66 }} // namespace luabind::detail
68 # endif // LUABIND_DETAIL_CONSTRUCTOR_081018_HPP
70 #else // !BOOST_PP_IS_ITERATING
72 # define N BOOST_PP_ITERATION()
74 template <class T
, class Signature
>
75 struct construct_aux
<N
, T
, Signature
>
77 typedef typename
mpl::begin
<Signature
>::type first
;
78 typedef typename
mpl::next
<first
>::type iter0
;
80 # define BOOST_PP_LOCAL_MACRO(n) \
81 typedef typename mpl::next< \
82 BOOST_PP_CAT(iter,BOOST_PP_DEC(n))>::type BOOST_PP_CAT(iter,n); \
83 typedef typename BOOST_PP_CAT(iter,n)::type BOOST_PP_CAT(a,BOOST_PP_DEC(n));
85 # define BOOST_PP_LOCAL_LIMITS (1,N)
86 # include BOOST_PP_LOCAL_ITERATE()
88 void operator()(argument
const& self_
, BOOST_PP_ENUM_BINARY_PARAMS(N
,a
,_
)) const
90 object_rep
* self
= touserdata
<object_rep
>(self_
);
91 class_rep
* cls
= self
->crep();
93 std::auto_ptr
<T
> instance(new T(BOOST_PP_ENUM_PARAMS(N
,_
)));
94 inject_backref(self_
.interpreter(), instance
.get(), instance
.get());
96 if (cls
->has_holder())
98 cls
->construct_holder()(self
->ptr(), instance
.get());
102 self
->set_object(instance
.get());
105 self
->set_destructor(cls
->destructor());