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/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();
94 typename
default_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
97 lua_State
* L
= m_obj
->lua_state();
98 detail::stack_pop
p(L
, 2); // pop the return value and the self reference
102 lua_pushstring(L
, m_member_name
);
105 // push the self-object
108 push_args_from_tuple
<1>::apply(L
, m_args
);
109 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 1, 0))
111 #ifndef LUABIND_NO_EXCEPTIONS
112 throw luabind::error();
118 #ifndef LUABIND_NO_ERROR_CHECKING
119 #ifndef LUABIND_NO_EXCEPTIONS
121 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
123 throw cant_convert_return_value();
126 assert(converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) >= 0);
129 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
132 template<class Policies
>
133 Ret
operator[](const Policies
& p
)
135 typedef typename find_conversion_policy
<0, Policies
>::type converter_policy
;
136 typename
converter_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
139 lua_State
* L
= m_obj
->lua_state();
140 detail::stack_pop
popper(L
, 2); // pop the return value and the self reference
144 lua_pushstring(L
, m_member_name
);
147 // push the self-object
150 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
151 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 1, 0))
153 #ifndef LUABIND_NO_EXCEPTIONS
160 #ifndef LUABIND_NO_ERROR_CHECKING
161 #ifndef LUABIND_NO_EXCEPTIONS
163 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
165 throw cant_convert_return_value();
168 assert(converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) >= 0);
171 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
176 luabind::object
* m_obj
;
177 const char* m_member_name
;
179 mutable bool m_called
;
183 // if the proxy_member_caller returns void
184 template<class Tuple
>
185 class proxy_member_void_caller
187 friend class luabind::object
;
190 proxy_member_void_caller(luabind::object
* o
, const char* name
, const Tuple args
)
192 , m_member_name(name
)
198 proxy_member_void_caller(const proxy_member_void_caller
& rhs
)
200 , m_member_name(rhs
.m_member_name
)
202 , m_called(rhs
.m_called
)
207 ~proxy_member_void_caller()
209 if (m_called
) return;
212 lua_State
* L
= m_obj
->lua_state();
213 detail::stack_pop(L
, 1); // pop the self reference
217 lua_pushstring(L
, m_member_name
);
220 // push the self-object
223 push_args_from_tuple
<1>::apply(L
, m_args
);
224 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 0, 0))
226 #ifndef LUABIND_NO_EXCEPTIONS
227 throw luabind::error();
234 template<class Policies
>
235 void operator[](const Policies
& p
)
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
250 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
251 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
+ 1, 0, 0))
253 #ifndef LUABIND_NO_EXCEPTIONS
263 luabind::object
* m_obj
;
264 const char* m_member_name
;
266 mutable bool m_called
;
273 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/detail/call_member.hpp>, 1))
274 #include BOOST_PP_ITERATE()
278 #endif // LUABIND_CALL_MEMBER_HPP_INCLUDED
282 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
283 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
285 template<class R
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
286 typename
boost::mpl::if_
<boost::is_void
<R
>
287 , luabind::detail::proxy_member_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
288 , luabind::detail::proxy_member_caller
<R
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type
289 call_member(luabind::object
& obj
, const char* name
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION())BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS
, _
))
291 typedef boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> tuple_t
;
292 #if BOOST_PP_ITERATION() == 0
295 tuple_t
args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a
));
298 typedef typename
boost::mpl::if_
<boost::is_void
<R
>
299 , luabind::detail::proxy_member_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
300 , luabind::detail::proxy_member_caller
<R
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type proxy_type
;
302 return proxy_type(const_cast<luabind::object
*>(&obj
), name
, args
);
305 #undef LUABIND_OPERATOR_PARAMS
306 #undef LUABIND_TUPLE_PARAMS