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>
42 // if the proxy_member_caller returns non-void
43 template<class Ret
, class Tuple
>
44 class proxy_member_caller
46 // friend class luabind::object;
49 proxy_member_caller(luabind::object
const* o
, const char* name
, const Tuple args
)
57 proxy_member_caller(const proxy_member_caller
& rhs
)
59 , m_member_name(rhs
.m_member_name
)
61 , m_called(rhs
.m_called
)
66 ~proxy_member_caller()
71 lua_State
* L
= m_obj
->lua_state();
72 detail::stack_pop(L
, 1); // pop the self reference
76 lua_pushstring(L
, m_member_name
);
79 // push the self-object
82 push_args_from_tuple
<1>::apply(L
, m_args
);
83 if (pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 0))
85 #ifndef LUABIND_NO_EXCEPTIONS
86 throw luabind::error(L
);
88 error_callback_fun e
= get_error_callback();
91 assert(0 && "the lua function threw an error and exceptions are disabled."
92 "If you want to handle this error use luabind::set_error_callback()");
100 typename
default_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
103 lua_State
* L
= m_obj
->lua_state();
104 detail::stack_pop
p(L
, 2); // pop the return value and the self reference
108 lua_pushstring(L
, m_member_name
);
111 // push the self-object
114 push_args_from_tuple
<1>::apply(L
, m_args
);
115 if (pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 1))
117 #ifndef LUABIND_NO_EXCEPTIONS
118 throw luabind::error(L
);
120 error_callback_fun e
= get_error_callback();
123 assert(0 && "the lua function threw an error and exceptions are disabled."
124 "If you want to handle this error use luabind::set_error_callback()");
129 #ifndef LUABIND_NO_ERROR_CHECKING
131 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
133 #ifndef LUABIND_NO_EXCEPTIONS
134 throw cast_failed(L
, LUABIND_TYPEID(Ret
));
136 cast_failed_callback_fun e
= get_cast_failed_callback();
137 if (e
) e(L
, LUABIND_TYPEID(Ret
));
139 assert(0 && "the lua function's return value could not be converted."
140 "If you want to handle this error use luabind::set_error_callback()");
145 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
148 template<class Policies
>
149 Ret
operator[](const Policies
& p
)
151 typedef typename find_conversion_policy
<0, Policies
>::type converter_policy
;
152 typename
converter_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
155 lua_State
* L
= m_obj
->lua_state();
156 detail::stack_pop
popper(L
, 2); // pop the return value and the self reference
160 lua_pushstring(L
, m_member_name
);
163 // push the self-object
166 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
167 if (pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 1))
169 #ifndef LUABIND_NO_EXCEPTIONS
172 error_callback_fun e
= get_error_callback();
175 assert(0 && "the lua function threw an error and exceptions are disabled."
176 "If you want to handle this error use luabind::set_error_callback()");
181 #ifndef LUABIND_NO_ERROR_CHECKING
183 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
185 #ifndef LUABIND_NO_EXCEPTIONS
186 throw cast_failed(L
, LUABIND_TYPEID(Ret
));
188 cast_failed_callback_fun e
= get_cast_failed_callback();
189 if (e
) e(L
, LUABIND_TYPEID(Ret
));
191 assert(0 && "the lua function's return value could not be converted."
192 "If you want to handle this error use luabind::set_error_callback()");
197 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
202 luabind::object
const* m_obj
;
203 const char* m_member_name
;
205 mutable bool m_called
;
209 // if the proxy_member_caller returns void
210 template<class Tuple
>
211 class proxy_member_void_caller
213 friend class luabind::object
;
216 proxy_member_void_caller(luabind::object
const* o
, const char* name
, const Tuple args
)
218 , m_member_name(name
)
224 proxy_member_void_caller(const proxy_member_void_caller
& rhs
)
226 , m_member_name(rhs
.m_member_name
)
228 , m_called(rhs
.m_called
)
233 ~proxy_member_void_caller()
235 if (m_called
) return;
238 lua_State
* L
= m_obj
->lua_state();
239 detail::stack_pop(L
, 1); // pop the self reference
243 lua_pushstring(L
, m_member_name
);
246 // push the self-object
249 push_args_from_tuple
<1>::apply(L
, m_args
);
250 if (pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 0))
252 #ifndef LUABIND_NO_EXCEPTIONS
253 throw luabind::error(L
);
255 error_callback_fun e
= get_error_callback();
258 assert(0 && "the lua function threw an error and exceptions are disabled."
259 "If you want to handle this error use luabind::set_error_callback()");
265 template<class Policies
>
266 void operator[](const Policies
& p
)
269 lua_State
* L
= m_obj
->lua_state();
270 detail::stack_pop(L
, 1); // pop the self reference
274 lua_pushstring(L
, m_member_name
);
277 // push the self-object
281 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
282 if (pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 0))
284 #ifndef LUABIND_NO_EXCEPTIONS
287 error_callback_fun e
= get_error_callback();
290 assert(0 && "the lua function threw an error and exceptions are disabled."
291 "If you want to handle this error use luabind::set_error_callback()");
299 luabind::object
const* m_obj
;
300 const char* m_member_name
;
302 mutable bool m_called
;
309 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/detail/call_member.hpp>, 1))
310 #include BOOST_PP_ITERATE()
314 #endif // LUABIND_CALL_MEMBER_HPP_INCLUDED
318 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
319 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
321 template<class R
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
322 typename
boost::mpl::if_
<boost::is_void
<R
>
323 , luabind::detail::proxy_member_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
324 , luabind::detail::proxy_member_caller
<R
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type
325 call_member(luabind::object
const& obj
, const char* name
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION())BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS
, _
))
327 typedef boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> tuple_t
;
328 #if BOOST_PP_ITERATION() == 0
331 tuple_t
args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a
));
334 typedef typename
boost::mpl::if_
<boost::is_void
<R
>
335 , luabind::detail::proxy_member_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
336 , luabind::detail::proxy_member_caller
<R
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type proxy_type
;
338 return proxy_type(&obj
, name
, args
);
341 #undef LUABIND_OPERATOR_PARAMS
342 #undef LUABIND_TUPLE_PARAMS