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.
24 #if !BOOST_PP_IS_ITERATING
26 #ifndef LUABIND_CALL_MEMBER_HPP_INCLUDED
27 #define LUABIND_CALL_MEMBER_HPP_INCLUDED
29 #include <luabind/config.hpp>
30 #include <luabind/detail/convert_to_lua.hpp>
31 #include <luabind/detail/pcall.hpp>
32 #include <luabind/error.hpp>
33 #include <luabind/detail/stack_utils.hpp>
34 #include <luabind/object.hpp> // TODO: REMOVE DEPENDENCY
36 #include <boost/tuple/tuple.hpp>
38 #include <boost/preprocessor/control/if.hpp>
39 #include <boost/preprocessor/facilities/expand.hpp>
40 #include <boost/preprocessor/repetition/enum.hpp>
42 #include <boost/mpl/apply_wrap.hpp>
49 namespace mpl
= boost::mpl
;
51 // if the proxy_member_caller returns non-void
52 template<class Ret
, class Tuple
>
53 class proxy_member_caller
55 // friend class luabind::object;
58 proxy_member_caller(lua_State
* L_
, const Tuple args
)
65 proxy_member_caller(const proxy_member_caller
& rhs
)
68 , m_called(rhs
.m_called
)
73 ~proxy_member_caller()
79 // don't count the function and self-reference
80 // since those will be popped by pcall
81 int top
= lua_gettop(L
) - 2;
83 // pcall will pop the function and self reference
84 // and all the parameters
86 push_args_from_tuple
<1>::apply(L
, m_args
);
87 if (pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 0))
89 assert(lua_gettop(L
) == top
+ 1);
90 #ifndef LUABIND_NO_EXCEPTIONS
91 throw luabind::error(L
);
93 error_callback_fun e
= get_error_callback();
96 assert(0 && "the lua function threw an error and exceptions are disabled."
97 "If you want to handle this error use luabind::set_error_callback()");
101 // pops the return values from the function
102 stack_pop
pop(L
, lua_gettop(L
) - top
);
107 typename
mpl::apply_wrap2
<default_policy
,Ret
,lua_to_cpp
>::type converter
;
111 // don't count the function and self-reference
112 // since those will be popped by pcall
113 int top
= lua_gettop(L
) - 2;
115 // pcall will pop the function and self reference
116 // and all the parameters
117 push_args_from_tuple
<1>::apply(L
, m_args
);
118 if (pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 1))
120 assert(lua_gettop(L
) == top
+ 1);
121 #ifndef LUABIND_NO_EXCEPTIONS
122 throw luabind::error(L
);
124 error_callback_fun e
= get_error_callback();
127 assert(0 && "the lua function threw an error and exceptions are disabled."
128 "If you want to handle this error use luabind::set_error_callback()");
133 // pops the return values from the function
134 stack_pop
pop(L
, lua_gettop(L
) - top
);
136 #ifndef LUABIND_NO_ERROR_CHECKING
138 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
140 assert(lua_gettop(L
) == top
+ 1);
141 #ifndef LUABIND_NO_EXCEPTIONS
142 throw cast_failed(L
, typeid(Ret
));
144 cast_failed_callback_fun e
= get_cast_failed_callback();
145 if (e
) e(L
, typeid(Ret
));
147 assert(0 && "the lua function's return value could not be converted."
148 "If you want to handle this error use luabind::set_error_callback()");
153 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
156 template<class Policies
>
157 Ret
operator[](const Policies
& p
)
159 typedef typename find_conversion_policy
<0, Policies
>::type converter_policy
;
160 typename
mpl::apply_wrap2
<converter_policy
,Ret
,lua_to_cpp
>::type converter
;
164 // don't count the function and self-reference
165 // since those will be popped by pcall
166 int top
= lua_gettop(L
) - 2;
168 // pcall will pop the function and self reference
169 // and all the parameters
171 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
172 if (pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 1))
174 assert(lua_gettop(L
) == top
+ 1);
175 #ifndef LUABIND_NO_EXCEPTIONS
178 error_callback_fun e
= get_error_callback();
181 assert(0 && "the lua function threw an error and exceptions are disabled."
182 "If you want to handle this error use luabind::set_error_callback()");
187 // pops the return values from the function
188 stack_pop
pop(L
, lua_gettop(L
) - top
);
190 #ifndef LUABIND_NO_ERROR_CHECKING
192 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
194 assert(lua_gettop(L
) == top
+ 1);
195 #ifndef LUABIND_NO_EXCEPTIONS
196 throw cast_failed(L
, typeid(Ret
));
198 cast_failed_callback_fun e
= get_cast_failed_callback();
199 if (e
) e(L
, typeid(Ret
));
201 assert(0 && "the lua function's return value could not be converted."
202 "If you want to handle this error use luabind::set_error_callback()");
207 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
214 mutable bool m_called
;
218 // if the proxy_member_caller returns void
219 template<class Tuple
>
220 class proxy_member_void_caller
222 friend class luabind::object
;
225 proxy_member_void_caller(lua_State
* L_
, const Tuple args
)
232 proxy_member_void_caller(const proxy_member_void_caller
& rhs
)
235 , m_called(rhs
.m_called
)
240 ~proxy_member_void_caller()
242 if (m_called
) return;
246 // don't count the function and self-reference
247 // since those will be popped by pcall
248 int top
= lua_gettop(L
) - 2;
250 // pcall will pop the function and self reference
251 // and all the parameters
253 push_args_from_tuple
<1>::apply(L
, m_args
);
254 if (pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 0))
256 assert(lua_gettop(L
) == top
+ 1);
257 #ifndef LUABIND_NO_EXCEPTIONS
258 throw luabind::error(L
);
260 error_callback_fun e
= get_error_callback();
263 assert(0 && "the lua function threw an error and exceptions are disabled."
264 "If you want to handle this error use luabind::set_error_callback()");
268 // pops the return values from the function
269 stack_pop
pop(L
, lua_gettop(L
) - top
);
272 template<class Policies
>
273 void operator[](const Policies
& p
)
277 // don't count the function and self-reference
278 // since those will be popped by pcall
279 int top
= lua_gettop(L
) - 2;
281 // pcall will pop the function and self reference
282 // and all the parameters
284 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
285 if (pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 0))
287 assert(lua_gettop(L
) == top
+ 1);
288 #ifndef LUABIND_NO_EXCEPTIONS
291 error_callback_fun e
= get_error_callback();
294 assert(0 && "the lua function threw an error and exceptions are disabled."
295 "If you want to handle this error use luabind::set_error_callback()");
299 // pops the return values from the function
300 stack_pop
pop(L
, lua_gettop(L
) - top
);
306 mutable bool m_called
;
312 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/detail/call_member.hpp>, 1))
313 #include BOOST_PP_ITERATE()
317 #endif // LUABIND_CALL_MEMBER_HPP_INCLUDED
319 #elif BOOST_PP_ITERATION_FLAGS() == 1
321 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
322 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
324 template<class R
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
325 typename
boost::mpl::if_
<boost::is_void
<R
>
326 , luabind::detail::proxy_member_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
327 , luabind::detail::proxy_member_caller
<R
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type
328 call_member(object
const& obj
, const char* name
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS
, _
))
330 typedef boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> tuple_t
;
331 #if BOOST_PP_ITERATION() == 0
334 tuple_t
args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a
));
337 typedef typename
boost::mpl::if_
<boost::is_void
<R
>
338 , luabind::detail::proxy_member_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
339 , luabind::detail::proxy_member_caller
<R
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type proxy_type
;
341 // this will be cleaned up by the proxy object
342 // once the call has been made
345 obj
.push(obj
.interpreter());
346 lua_pushstring(obj
.interpreter(), name
);
347 lua_gettable(obj
.interpreter(), -2);
348 // duplicate the self-object
349 lua_pushvalue(obj
.interpreter(), -2);
350 // remove the bottom self-object
351 lua_remove(obj
.interpreter(), -3);
353 // now the function and self objects
354 // are on the stack. These will both
355 // be popped by pcall
356 return proxy_type(obj
.interpreter(), args
);
359 #undef LUABIND_OPERATOR_PARAMS
360 #undef LUABIND_TUPLE_PARAMS