*** empty log message ***
[luabind.git] / luabind / detail / call_member.hpp
blob29c2e3ba79b67a2d01486e488a50409b13f74513
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>
34 namespace luabind
36 namespace detail
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;
47 public:
49 proxy_member_caller(luabind::object const* o, const char* name, const Tuple args)
50 : m_obj(o)
51 , m_member_name(name)
52 , m_args(args)
53 , m_called(false)
57 proxy_member_caller(const proxy_member_caller& rhs)
58 : m_obj(rhs.m_obj)
59 , m_member_name(rhs.m_member_name)
60 , m_args(rhs.m_args)
61 , m_called(rhs.m_called)
63 rhs.m_called = true;
66 ~proxy_member_caller()
68 if (m_called) return;
70 m_called = true;
71 lua_State* L = m_obj->lua_state();
72 detail::stack_pop(L, 1); // pop the self reference
74 // get the function
75 m_obj->pushvalue();
76 lua_pushstring(L, m_member_name);
77 lua_gettable(L, -2);
79 // push the self-object
80 m_obj->pushvalue();
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);
87 #else
88 error_callback_fun e = get_error_callback();
89 if (e) e(L);
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()");
93 std::terminate();
94 #endif
98 operator Ret()
100 typename default_policy::template generate_converter<Ret, lua_to_cpp>::type converter;
102 m_called = true;
103 lua_State* L = m_obj->lua_state();
104 detail::stack_pop p(L, 2); // pop the return value and the self reference
106 // get the function
107 m_obj->pushvalue();
108 lua_pushstring(L, m_member_name);
109 lua_gettable(L, -2);
111 // push the self-object
112 m_obj->pushvalue();
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);
119 #else
120 error_callback_fun e = get_error_callback();
121 if (e) e(L);
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()");
125 std::terminate();
126 #endif
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));
135 #else
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()");
141 std::terminate();
142 #endif
144 #endif
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;
154 m_called = true;
155 lua_State* L = m_obj->lua_state();
156 detail::stack_pop popper(L, 2); // pop the return value and the self reference
158 // get the function
159 m_obj->pushvalue();
160 lua_pushstring(L, m_member_name);
161 lua_gettable(L, -2);
163 // push the self-object
164 m_obj->pushvalue();
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
170 throw error(L);
171 #else
172 error_callback_fun e = get_error_callback();
173 if (e) e(L);
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()");
177 std::terminate();
178 #endif
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));
187 #else
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()");
193 std::terminate();
194 #endif
196 #endif
197 return converter.apply(L, LUABIND_DECORATE_TYPE(Ret), -1);
200 private:
202 luabind::object const* m_obj;
203 const char* m_member_name;
204 Tuple m_args;
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;
214 public:
216 proxy_member_void_caller(luabind::object const* o, const char* name, const Tuple args)
217 : m_obj(o)
218 , m_member_name(name)
219 , m_args(args)
220 , m_called(false)
224 proxy_member_void_caller(const proxy_member_void_caller& rhs)
225 : m_obj(rhs.m_obj)
226 , m_member_name(rhs.m_member_name)
227 , m_args(rhs.m_args)
228 , m_called(rhs.m_called)
230 rhs.m_called = true;
233 ~proxy_member_void_caller()
235 if (m_called) return;
237 m_called = true;
238 lua_State* L = m_obj->lua_state();
239 detail::stack_pop(L, 1); // pop the self reference
241 // get the function
242 m_obj->pushvalue();
243 lua_pushstring(L, m_member_name);
244 lua_gettable(L, -2);
246 // push the self-object
247 m_obj->pushvalue();
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);
254 #else
255 error_callback_fun e = get_error_callback();
256 if (e) e(L);
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()");
260 std::terminate();
261 #endif
265 template<class Policies>
266 void operator[](const Policies& p)
268 m_called = true;
269 lua_State* L = m_obj->lua_state();
270 detail::stack_pop(L, 1); // pop the self reference
272 // get the function
273 m_obj->pushvalue();
274 lua_pushstring(L, m_member_name);
275 lua_gettable(L, -2);
277 // push the self-object
278 m_obj->pushvalue();
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
285 throw error(L);
286 #else
287 error_callback_fun e = get_error_callback();
288 if (e) e(L);
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()");
292 std::terminate();
293 #endif
297 private:
299 luabind::object const* m_obj;
300 const char* m_member_name;
301 Tuple m_args;
302 mutable bool m_called;
307 } // detail
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
316 #else
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
329 tuple_t args;
330 #else
331 tuple_t args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a));
332 #endif
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
344 #endif