Merge branch '0.8' to sync with v0.8 release.
[luabind.git] / luabind / wrapper_base.hpp
blobcc8026a668ac07b0bbfb16c0ee2bd456962ea3e9
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.
23 #if !BOOST_PP_IS_ITERATING
25 #ifndef LUABIND_WRAPPER_BASE_HPP_INCLUDED
26 #define LUABIND_WRAPPER_BASE_HPP_INCLUDED
28 #include <luabind/config.hpp>
29 #include <luabind/weak_ref.hpp>
30 #include <luabind/detail/ref.hpp>
31 #include <luabind/detail/call_member.hpp>
33 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
34 #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
36 namespace luabind
38 namespace detail
40 struct wrap_access;
42 // implements the selection between dynamic dispatch
43 // or default implementation calls from within a virtual
44 // function wrapper. The input is the self reference on
45 // the top of the stack. Output is the function to call
46 // on the top of the stack (the input self reference will
47 // be popped)
48 LUABIND_API void do_call_member_selection(lua_State* L, char const* name);
51 struct wrapped_self_t: weak_ref
53 detail::lua_reference m_strong_ref;
56 struct wrap_base
58 friend struct detail::wrap_access;
59 wrap_base() {}
61 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/wrapper_base.hpp>, 1))
62 #include BOOST_PP_ITERATE()
64 private:
65 wrapped_self_t m_self;
68 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/wrapper_base.hpp>, 2))
69 #include BOOST_PP_ITERATE()
71 namespace detail
73 struct wrap_access
75 static wrapped_self_t const& ref(wrap_base const& b)
77 return b.m_self;
80 static wrapped_self_t& ref(wrap_base& b)
82 return b.m_self;
88 #endif // LUABIND_WRAPPER_BASE_HPP_INCLUDED
90 #elif BOOST_PP_ITERATION_FLAGS() == 1
92 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
93 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
95 template<class R BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
96 typename boost::mpl::if_<boost::is_void<R>
97 , luabind::detail::proxy_member_void_caller<boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> >
98 , luabind::detail::proxy_member_caller<R, boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> > >::type
99 call(char const* name BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS, _), detail::type_<R>* = 0) const
101 typedef boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> tuple_t;
102 #if BOOST_PP_ITERATION() == 0
103 tuple_t args;
104 #else
105 tuple_t args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a));
106 #endif
108 typedef typename boost::mpl::if_<boost::is_void<R>
109 , luabind::detail::proxy_member_void_caller<boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> >
110 , luabind::detail::proxy_member_caller<R, boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> > >::type proxy_type;
112 // this will be cleaned up by the proxy object
113 // once the call has been made
115 // TODO: what happens if this virtual function is
116 // dispatched from a lua thread where the state
117 // pointer is different?
119 // get the function
120 lua_State* L = m_self.state();
121 m_self.get(L);
122 assert(!lua_isnil(L, -1));
123 detail::do_call_member_selection(L, name);
125 if (lua_isnil(L, -1))
127 lua_pop(L, 1);
128 throw std::runtime_error("Attempt to call nonexistent function");
131 // push the self reference as the first parameter
132 m_self.get(L);
134 // now the function and self objects
135 // are on the stack. These will both
136 // be popped by pcall
137 return proxy_type(L, args);
140 #undef LUABIND_CALL_MEMBER_NAME
141 #undef LUABIND_OPERATOR_PARAMS
142 #undef LUABIND_TUPLE_PARAMS
144 #else // free call_member forwardarding functions
146 #define N BOOST_PP_ITERATION()
148 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
149 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
151 template<
152 class R
153 BOOST_PP_ENUM_TRAILING_PARAMS(N, class A)
155 typename boost::mpl::if_<
156 boost::is_void<R>
157 , detail::proxy_member_void_caller<
158 boost::tuples::tuple<
159 BOOST_PP_ENUM(N, LUABIND_TUPLE_PARAMS, _)
162 , detail::proxy_member_caller<
164 , boost::tuples::tuple<
165 BOOST_PP_ENUM(N, LUABIND_TUPLE_PARAMS, _)
168 >::type
169 call_member(
170 wrap_base const* self
171 , char const* fn
172 BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, A, &a)
173 , detail::type_<R>* = 0
176 return self->call(
178 BOOST_PP_ENUM_TRAILING_PARAMS(N, a)
179 , (detail::type_<R>*)0
183 #undef LUABIND_OPERATOR_PARAMS
184 #undef LUABIND_TUPLE_PARAMS
186 #undef N
188 #endif