Test object identity with shared_ptr_converter.
[luabind.git] / luabind / detail / constructor.hpp
bloba4da7f0d22e0732132f97aa168be9e32e2a096e6
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>
12 # include <luabind/detail/inheritance.hpp>
14 # include <boost/preprocessor/iteration/iterate.hpp>
15 # include <boost/preprocessor/iteration/local.hpp>
16 # include <boost/preprocessor/repetition/enum_params.hpp>
17 # include <boost/preprocessor/repetition/enum_binary_params.hpp>
19 namespace luabind { namespace detail {
21 inline void inject_backref(lua_State* L, void*, void*)
24 template <class T>
25 void inject_backref(lua_State* L, T* p, wrap_base*)
27 weak_ref(L, 1).swap(wrap_access::ref(*p));
30 template <std::size_t Arity, class T, class Pointer, class Signature>
31 struct construct_aux;
33 template <class T, class Pointer, class Signature>
34 struct construct
35 : construct_aux<mpl::size<Signature>::value - 2, T, Pointer, Signature>
36 {};
38 template <class T, class Pointer, class Signature>
39 struct construct_aux<0, T, Pointer, Signature>
41 void operator()(argument const& self_) const
43 object_rep* self = touserdata<object_rep>(self_);
44 class_rep* cls = self->crep();
46 std::auto_ptr<T> instance(new T);
47 inject_backref(self_.interpreter(), instance.get(), instance.get());
49 void* naked_ptr = instance.get();
50 Pointer ptr(instance.release());
52 self->set_instance(new pointer_holder<Pointer, T>(
53 ptr, registered_class<T>::id, naked_ptr, cls));
57 # define BOOST_PP_ITERATION_PARAMS_1 \
58 (3, (1, LUABIND_MAX_ARITY, <luabind/detail/constructor.hpp>))
59 # include BOOST_PP_ITERATE()
61 }} // namespace luabind::detail
63 # endif // LUABIND_DETAIL_CONSTRUCTOR_081018_HPP
65 #else // !BOOST_PP_IS_ITERATING
67 # define N BOOST_PP_ITERATION()
69 template <class T, class Pointer, class Signature>
70 struct construct_aux<N, T, Pointer, Signature>
72 typedef typename mpl::begin<Signature>::type first;
73 typedef typename mpl::next<first>::type iter0;
75 # define BOOST_PP_LOCAL_MACRO(n) \
76 typedef typename mpl::next< \
77 BOOST_PP_CAT(iter,BOOST_PP_DEC(n))>::type BOOST_PP_CAT(iter,n); \
78 typedef typename BOOST_PP_CAT(iter,n)::type BOOST_PP_CAT(a,BOOST_PP_DEC(n));
80 # define BOOST_PP_LOCAL_LIMITS (1,N)
81 # include BOOST_PP_LOCAL_ITERATE()
83 void operator()(argument const& self_, BOOST_PP_ENUM_BINARY_PARAMS(N,a,_)) const
85 object_rep* self = touserdata<object_rep>(self_);
86 class_rep* cls = self->crep();
88 std::auto_ptr<T> instance(new T(BOOST_PP_ENUM_PARAMS(N,_)));
89 inject_backref(self_.interpreter(), instance.get(), instance.get());
91 void* naked_ptr = instance.get();
92 Pointer ptr(instance.release());
94 self->set_instance(new pointer_holder<Pointer, T>(
95 ptr, registered_class<T>::id, naked_ptr, cls));
99 #endif