Remove instance_holder dependency on class_rep.
[luabind.git] / luabind / detail / constructor.hpp
blobbeff17f7ea53187085ecde925c9b28fc1db337a9
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/get_main_thread.hpp>
11 # include <luabind/object.hpp>
12 # include <luabind/wrapper_base.hpp>
13 # include <luabind/detail/inheritance.hpp>
15 # include <boost/preprocessor/iteration/iterate.hpp>
16 # include <boost/preprocessor/iteration/local.hpp>
17 # include <boost/preprocessor/repetition/enum_params.hpp>
18 # include <boost/preprocessor/repetition/enum_binary_params.hpp>
20 namespace luabind { namespace detail {
22 inline void inject_backref(lua_State*, void*, void*)
25 template <class T>
26 void inject_backref(lua_State* L, T* p, wrap_base*)
28 weak_ref(get_main_thread(L), 1).swap(wrap_access::ref(*p));
31 template <std::size_t Arity, class T, class Pointer, class Signature>
32 struct construct_aux;
34 template <class T, class Pointer, class Signature>
35 struct construct
36 : construct_aux<mpl::size<Signature>::value - 2, T, Pointer, Signature>
37 {};
39 template <class T, class Pointer, class Signature>
40 struct construct_aux<0, T, Pointer, Signature>
42 typedef pointer_holder<Pointer, T> holder_type;
44 void operator()(argument const& self_) const
46 object_rep* self = touserdata<object_rep>(self_);
48 std::auto_ptr<T> instance(new T);
49 inject_backref(self_.interpreter(), instance.get(), instance.get());
51 void* naked_ptr = instance.get();
52 Pointer ptr(instance.release());
54 void* storage = self->allocate(sizeof(holder_type));
56 self->set_instance(new (storage) holder_type(
57 ptr, registered_class<T>::id, naked_ptr));
61 # define BOOST_PP_ITERATION_PARAMS_1 \
62 (3, (1, LUABIND_MAX_ARITY, <luabind/detail/constructor.hpp>))
63 # include BOOST_PP_ITERATE()
65 }} // namespace luabind::detail
67 # endif // LUABIND_DETAIL_CONSTRUCTOR_081018_HPP
69 #else // !BOOST_PP_IS_ITERATING
71 # define N BOOST_PP_ITERATION()
73 template <class T, class Pointer, class Signature>
74 struct construct_aux<N, T, Pointer, Signature>
76 typedef typename mpl::begin<Signature>::type first;
77 typedef typename mpl::next<first>::type iter0;
79 # define BOOST_PP_LOCAL_MACRO(n) \
80 typedef typename mpl::next< \
81 BOOST_PP_CAT(iter,BOOST_PP_DEC(n))>::type BOOST_PP_CAT(iter,n); \
82 typedef typename BOOST_PP_CAT(iter,n)::type BOOST_PP_CAT(a,BOOST_PP_DEC(n));
84 # define BOOST_PP_LOCAL_LIMITS (1,N)
85 # include BOOST_PP_LOCAL_ITERATE()
87 typedef pointer_holder<Pointer, T> holder_type;
89 void operator()(argument const& self_, BOOST_PP_ENUM_BINARY_PARAMS(N,a,_)) const
91 object_rep* self = touserdata<object_rep>(self_);
93 std::auto_ptr<T> instance(new T(BOOST_PP_ENUM_PARAMS(N,_)));
94 inject_backref(self_.interpreter(), instance.get(), instance.get());
96 void* naked_ptr = instance.get();
97 Pointer ptr(instance.release());
99 void* storage = self->allocate(sizeof(holder_type));
101 self->set_instance(new (storage) holder_type(
102 ptr, registered_class<T>::id, naked_ptr));
106 #endif