OSDN Git Service

build moflib1.0 on cmake-base system
[moflib/moflib.git] / extlib / luabind-0.8 / luabind / detail / constructor.hpp
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)
4
5 #if !BOOST_PP_IS_ITERATING
6
7 # ifndef LUABIND_DETAIL_CONSTRUCTOR_081018_HPP
8 #  define LUABIND_DETAIL_CONSTRUCTOR_081018_HPP
9
10 #  include <luabind/object.hpp>
11 #  include <luabind/wrapper_base.hpp>
12
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>
17
18 namespace luabind { namespace detail {
19
20 inline void inject_backref(lua_State* L, void*, void*)
21 {}
22
23 template <class T>
24 void inject_backref(lua_State* L, T* p, wrap_base*)
25 {
26     weak_ref(L, 1).swap(wrap_access::ref(*p));
27 }
28
29 template <std::size_t Arity, class T, class Signature>
30 struct construct_aux;
31
32 template <class T, class Signature>
33 struct construct
34   : construct_aux<mpl::size<Signature>::value - 2, T, Signature>
35 {};
36
37 template <class T, class Signature>
38 struct construct_aux<0, T, Signature>
39 {
40     void operator()(argument const& self_) const
41     {
42         object_rep* self = touserdata<object_rep>(self_);
43         class_rep* cls = self->crep();
44
45         std::auto_ptr<T> instance(new T);
46         inject_backref(self_.interpreter(), instance.get(), instance.get());
47
48         if (cls->has_holder())
49         {
50             cls->construct_holder()(self->ptr(), instance.get());
51         }
52         else
53         {
54             self->set_object(instance.get());
55         }
56
57         self->set_destructor(cls->destructor());
58         instance.release();
59     }
60 };
61
62 #  define BOOST_PP_ITERATION_PARAMS_1 \
63     (3, (1, LUABIND_MAX_ARITY, <luabind/detail/constructor.hpp>))
64 #  include BOOST_PP_ITERATE()
65
66 }} // namespace luabind::detail
67
68 # endif // LUABIND_DETAIL_CONSTRUCTOR_081018_HPP
69
70 #else // !BOOST_PP_IS_ITERATING
71
72 # define N BOOST_PP_ITERATION()
73
74 template <class T, class Signature>
75 struct construct_aux<N, T, Signature>
76 {
77     typedef typename mpl::begin<Signature>::type first;
78     typedef typename mpl::next<first>::type iter0;
79
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));
84
85 # define BOOST_PP_LOCAL_LIMITS (1,N)
86 # include BOOST_PP_LOCAL_ITERATE()
87
88     void operator()(argument const& self_, BOOST_PP_ENUM_BINARY_PARAMS(N,a,_)) const
89     {
90         object_rep* self = touserdata<object_rep>(self_);
91         class_rep* cls = self->crep();
92
93         std::auto_ptr<T> instance(new T(BOOST_PP_ENUM_PARAMS(N,_)));
94         inject_backref(self_.interpreter(), instance.get(), instance.get());
95
96         if (cls->has_holder())
97         {
98             cls->construct_holder()(self->ptr(), instance.get());
99         }
100         else
101         {
102             self->set_object(instance.get());
103         }
104
105         self->set_destructor(cls->destructor());
106         instance.release();
107     }
108 };
109
110 #endif
111