moved some headers out of detail/
[luabind.git] / luabind / detail / call_member.hpp
blobc76379684e181902c95a538006b3a24488e193c2
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>
33 namespace luabind
35 namespace detail
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;
46 public:
48 proxy_member_caller(luabind::object* o, const char* name, const Tuple args)
49 : m_obj(o)
50 , m_member_name(name)
51 , m_args(args)
52 , m_called(false)
56 proxy_member_caller(const proxy_member_caller& rhs)
57 : m_obj(rhs.m_obj)
58 , m_member_name(rhs.m_member_name)
59 , m_args(rhs.m_args)
60 , m_called(rhs.m_called)
62 rhs.m_called = true;
65 ~proxy_member_caller()
67 if (m_called) return;
69 m_called = true;
70 lua_State* L = m_obj->lua_state();
71 detail::stack_pop(L, 1); // pop the self reference
73 // get the function
74 m_obj->pushvalue();
75 lua_pushstring(L, m_member_name);
76 lua_gettable(L, -2);
78 // push the self-object
79 m_obj->pushvalue();
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);
86 #else
87 error_callback_fun e = detail::error_callback::get().err;
88 if (e) e(L);
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()");
92 std::terminate();
93 #endif
97 operator Ret()
99 typename default_policy::template generate_converter<Ret, lua_to_cpp>::type converter;
101 m_called = true;
102 lua_State* L = m_obj->lua_state();
103 detail::stack_pop p(L, 2); // pop the return value and the self reference
105 // get the function
106 m_obj->pushvalue();
107 lua_pushstring(L, m_member_name);
108 lua_gettable(L, -2);
110 // push the self-object
111 m_obj->pushvalue();
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);
118 #else
119 error_callback_fun e = detail::error_callback::get().err;
120 if (e) e(L);
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()");
124 std::terminate();
125 #endif
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));
134 #else
135 cast_failed_callback_fun e = detail::error_callback::get().cast;
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()");
140 std::terminate();
141 #endif
143 #endif
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;
153 m_called = true;
154 lua_State* L = m_obj->lua_state();
155 detail::stack_pop popper(L, 2); // pop the return value and the self reference
157 // get the function
158 m_obj->pushvalue();
159 lua_pushstring(L, m_member_name);
160 lua_gettable(L, -2);
162 // push the self-object
163 m_obj->pushvalue();
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
169 throw error(L);
170 #else
171 error_callback_fun e = detail::error_callback::get().err;
172 if (e) e(L);
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()");
176 std::terminate();
177 #endif
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));
186 #else
187 cast_failed_callback_fun e = detail::error_callback::get().cast;
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()");
192 std::terminate();
193 #endif
195 #endif
196 return converter.apply(L, LUABIND_DECORATE_TYPE(Ret), -1);
199 private:
201 luabind::object* m_obj;
202 const char* m_member_name;
203 Tuple m_args;
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;
213 public:
215 proxy_member_void_caller(luabind::object* o, const char* name, const Tuple args)
216 : m_obj(o)
217 , m_member_name(name)
218 , m_args(args)
219 , m_called(false)
223 proxy_member_void_caller(const proxy_member_void_caller& rhs)
224 : m_obj(rhs.m_obj)
225 , m_member_name(rhs.m_member_name)
226 , m_args(rhs.m_args)
227 , m_called(rhs.m_called)
229 rhs.m_called = true;
232 ~proxy_member_void_caller()
234 if (m_called) return;
236 m_called = true;
237 lua_State* L = m_obj->lua_state();
238 detail::stack_pop(L, 1); // pop the self reference
240 // get the function
241 m_obj->pushvalue();
242 lua_pushstring(L, m_member_name);
243 lua_gettable(L, -2);
245 // push the self-object
246 m_obj->pushvalue();
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);
253 #else
254 error_callback_fun e = detail::error_callback::get().err;
255 if (e) e(L);
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()");
259 std::terminate();
260 #endif
264 template<class Policies>
265 void operator[](const Policies& p)
267 m_called = true;
268 lua_State* L = m_obj->lua_state();
269 detail::stack_pop(L, 1); // pop the self reference
271 // get the function
272 m_obj->pushvalue();
273 lua_pushstring(L, m_member_name);
274 lua_gettable(L, -2);
276 // push the self-object
277 m_obj->pushvalue();
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
284 throw error(L);
285 #else
286 error_callback_fun e = detail::error_callback::get().err;
287 if (e) e(L);
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()");
291 std::terminate();
292 #endif
296 private:
298 luabind::object* m_obj;
299 const char* m_member_name;
300 Tuple m_args;
301 mutable bool m_called;
306 } // detail
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
315 #else
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
328 tuple_t args;
329 #else
330 tuple_t args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a));
331 #endif
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
343 #endif