Test object identity with shared_ptr_converter.
[luabind.git] / luabind / detail / call_member.hpp
blobde8d563721896a29a938e999157584996ee77aed
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>
44 namespace luabind
46 namespace detail
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;
56 public:
58 proxy_member_caller(lua_State* L_, const Tuple args)
59 : L(L_)
60 , m_args(args)
61 , m_called(false)
65 proxy_member_caller(const proxy_member_caller& rhs)
66 : L(rhs.L)
67 , m_args(rhs.m_args)
68 , m_called(rhs.m_called)
70 rhs.m_called = true;
73 ~proxy_member_caller()
75 if (m_called) return;
77 m_called = true;
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);
92 #else
93 error_callback_fun e = get_error_callback();
94 if (e) e(L);
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()");
98 std::terminate();
99 #endif
101 // pops the return values from the function
102 stack_pop pop(L, lua_gettop(L) - top);
105 operator Ret()
107 typename mpl::apply_wrap2<default_policy,Ret,lua_to_cpp>::type converter;
109 m_called = true;
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);
123 #else
124 error_callback_fun e = get_error_callback();
125 if (e) e(L);
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()");
129 std::terminate();
130 #endif
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));
143 #else
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()");
149 std::terminate();
150 #endif
152 #endif
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;
162 m_called = true;
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
176 throw error(L);
177 #else
178 error_callback_fun e = get_error_callback();
179 if (e) e(L);
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()");
183 std::terminate();
184 #endif
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));
197 #else
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()");
203 std::terminate();
204 #endif
206 #endif
207 return converter.apply(L, LUABIND_DECORATE_TYPE(Ret), -1);
210 private:
212 lua_State* L;
213 Tuple m_args;
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;
223 public:
225 proxy_member_void_caller(lua_State* L_, const Tuple args)
226 : L(L_)
227 , m_args(args)
228 , m_called(false)
232 proxy_member_void_caller(const proxy_member_void_caller& rhs)
233 : L(rhs.L)
234 , m_args(rhs.m_args)
235 , m_called(rhs.m_called)
237 rhs.m_called = true;
240 ~proxy_member_void_caller()
242 if (m_called) return;
244 m_called = true;
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);
259 #else
260 error_callback_fun e = get_error_callback();
261 if (e) e(L);
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()");
265 std::terminate();
266 #endif
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)
275 m_called = true;
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
289 throw error(L);
290 #else
291 error_callback_fun e = get_error_callback();
292 if (e) e(L);
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()");
296 std::terminate();
297 #endif
299 // pops the return values from the function
300 stack_pop pop(L, lua_gettop(L) - top);
303 private:
304 lua_State* L;
305 Tuple m_args;
306 mutable bool m_called;
310 } // detail
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
332 tuple_t args;
333 #else
334 tuple_t args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a));
335 #endif
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
344 // get the function
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
362 #endif