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/error.hpp>
41 // if the proxy_member_caller returns non-void
42 template<class Ret
, class Tuple
>
43 class proxy_member_caller
45 // friend class luabind::object;
48 proxy_member_caller(luabind::object
* o
, const char* name
, const Tuple args
)
56 proxy_member_caller(const proxy_member_caller
& rhs
)
58 , m_member_name(rhs
.m_member_name
)
60 , m_called(rhs
.m_called
)
65 ~proxy_member_caller()
70 lua_State
* L
= m_obj
->lua_state();
71 detail::stack_pop(L
, 1); // pop the self reference
75 lua_pushstring(L
, m_member_name
);
78 // push the self-object
81 push_args_from_tuple
<1>::apply(L
, m_args
);
82 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 0, 0))
84 #ifndef LUABIND_NO_EXCEPTIONS
85 throw luabind::error(L
);
87 error_callback_fun e
= get_error_callback();
90 assert(0 && "the lua function threw an error and exceptions are disabled."
91 "If you want to handle this error use luabind::set_error_callback()");
99 typename
default_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
102 lua_State
* L
= m_obj
->lua_state();
103 detail::stack_pop
p(L
, 2); // pop the return value and the self reference
107 lua_pushstring(L
, m_member_name
);
110 // push the self-object
113 push_args_from_tuple
<1>::apply(L
, m_args
);
114 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 1, 0))
116 #ifndef LUABIND_NO_EXCEPTIONS
117 throw luabind::error(L
);
119 error_callback_fun e
= get_error_callback();
122 assert(0 && "the lua function threw an error and exceptions are disabled."
123 "If you want to handle this error use luabind::set_error_callback()");
128 #ifndef LUABIND_NO_ERROR_CHECKING
130 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
132 #ifndef LUABIND_NO_EXCEPTIONS
133 throw cast_failed(L
, LUABIND_TYPEID(Ret
));
135 cast_failed_callback_fun e
= get_cast_failed_callback();
136 if (e
) e(L
, LUABIND_TYPEID(Ret
));
138 assert(0 && "the lua function's return value could not be converted."
139 "If you want to handle this error use luabind::set_error_callback()");
144 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
147 template<class Policies
>
148 Ret
operator[](const Policies
& p
)
150 typedef typename find_conversion_policy
<0, Policies
>::type converter_policy
;
151 typename
converter_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
154 lua_State
* L
= m_obj
->lua_state();
155 detail::stack_pop
popper(L
, 2); // pop the return value and the self reference
159 lua_pushstring(L
, m_member_name
);
162 // push the self-object
165 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
166 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 1, 0))
168 #ifndef LUABIND_NO_EXCEPTIONS
171 error_callback_fun e
= get_error_callback();
174 assert(0 && "the lua function threw an error and exceptions are disabled."
175 "If you want to handle this error use luabind::set_error_callback()");
180 #ifndef LUABIND_NO_ERROR_CHECKING
182 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
184 #ifndef LUABIND_NO_EXCEPTIONS
185 throw cast_failed(L
, LUABIND_TYPEID(Ret
));
187 cast_failed_callback_fun e
= get_cast_failed_callback();
188 if (e
) e(L
, LUABIND_TYPEID(Ret
));
190 assert(0 && "the lua function's return value could not be converted."
191 "If you want to handle this error use luabind::set_error_callback()");
196 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
201 luabind::object
* m_obj
;
202 const char* m_member_name
;
204 mutable bool m_called
;
208 // if the proxy_member_caller returns void
209 template<class Tuple
>
210 class proxy_member_void_caller
212 friend class luabind::object
;
215 proxy_member_void_caller(luabind::object
* o
, const char* name
, const Tuple args
)
217 , m_member_name(name
)
223 proxy_member_void_caller(const proxy_member_void_caller
& rhs
)
225 , m_member_name(rhs
.m_member_name
)
227 , m_called(rhs
.m_called
)
232 ~proxy_member_void_caller()
234 if (m_called
) return;
237 lua_State
* L
= m_obj
->lua_state();
238 detail::stack_pop(L
, 1); // pop the self reference
242 lua_pushstring(L
, m_member_name
);
245 // push the self-object
248 push_args_from_tuple
<1>::apply(L
, m_args
);
249 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 0, 0))
251 #ifndef LUABIND_NO_EXCEPTIONS
252 throw luabind::error(L
);
254 error_callback_fun e
= get_error_callback();
257 assert(0 && "the lua function threw an error and exceptions are disabled."
258 "If you want to handle this error use luabind::set_error_callback()");
264 template<class Policies
>
265 void operator[](const Policies
& p
)
268 lua_State
* L
= m_obj
->lua_state();
269 detail::stack_pop(L
, 1); // pop the self reference
273 lua_pushstring(L
, m_member_name
);
276 // push the self-object
280 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
281 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 0, 0))
283 #ifndef LUABIND_NO_EXCEPTIONS
286 error_callback_fun e
= get_error_callback();
289 assert(0 && "the lua function threw an error and exceptions are disabled."
290 "If you want to handle this error use luabind::set_error_callback()");
298 luabind::object
* m_obj
;
299 const char* m_member_name
;
301 mutable bool m_called
;
308 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/detail/call_member.hpp>, 1))
309 #include BOOST_PP_ITERATE()
313 #endif // LUABIND_CALL_MEMBER_HPP_INCLUDED
317 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
318 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
320 template<class R
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
321 typename
boost::mpl::if_
<boost::is_void
<R
>
322 , luabind::detail::proxy_member_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
323 , luabind::detail::proxy_member_caller
<R
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type
324 call_member(luabind::object
& obj
, const char* name
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION())BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS
, _
))
326 typedef boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> tuple_t
;
327 #if BOOST_PP_ITERATION() == 0
330 tuple_t
args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a
));
333 typedef typename
boost::mpl::if_
<boost::is_void
<R
>
334 , luabind::detail::proxy_member_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
335 , luabind::detail::proxy_member_caller
<R
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type proxy_type
;
337 return proxy_type(const_cast<luabind::object
*>(&obj
), name
, args
);
340 #undef LUABIND_OPERATOR_PARAMS
341 #undef LUABIND_TUPLE_PARAMS