Initial revision
[luabind.git] / luabind / detail / call_member.hpp
blob81ac656c6515db6164e0a81579f20fa1289f2401
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>
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();
86 #else
87 assert(0);
88 #endif
92 operator Ret()
94 typename default_policy::template generate_converter<Ret, lua_to_cpp>::type converter;
96 m_called = true;
97 lua_State* L = m_obj->lua_state();
98 detail::stack_pop p(L, 2); // pop the return value and the self reference
100 // get the function
101 m_obj->pushvalue();
102 lua_pushstring(L, m_member_name);
103 lua_gettable(L, -2);
105 // push the self-object
106 m_obj->pushvalue();
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();
113 #else
114 assert(0);
115 #endif
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();
125 #else
126 assert(converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) >= 0);
127 #endif
128 #endif
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;
138 m_called = true;
139 lua_State* L = m_obj->lua_state();
140 detail::stack_pop popper(L, 2); // pop the return value and the self reference
142 // get the function
143 m_obj->pushvalue();
144 lua_pushstring(L, m_member_name);
145 lua_gettable(L, -2);
147 // push the self-object
148 m_obj->pushvalue();
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
154 throw error();
155 #else
156 assert(0);
157 #endif
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();
167 #else
168 assert(converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) >= 0);
169 #endif
170 #endif
171 return converter.apply(L, LUABIND_DECORATE_TYPE(Ret), -1);
174 private:
176 luabind::object* m_obj;
177 const char* m_member_name;
178 Tuple m_args;
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;
188 public:
190 proxy_member_void_caller(luabind::object* o, const char* name, const Tuple args)
191 : m_obj(o)
192 , m_member_name(name)
193 , m_args(args)
194 , m_called(false)
198 proxy_member_void_caller(const proxy_member_void_caller& rhs)
199 : m_obj(rhs.m_obj)
200 , m_member_name(rhs.m_member_name)
201 , m_args(rhs.m_args)
202 , m_called(rhs.m_called)
204 rhs.m_called = true;
207 ~proxy_member_void_caller()
209 if (m_called) return;
211 m_called = true;
212 lua_State* L = m_obj->lua_state();
213 detail::stack_pop(L, 1); // pop the self reference
215 // get the function
216 m_obj->pushvalue();
217 lua_pushstring(L, m_member_name);
218 lua_gettable(L, -2);
220 // push the self-object
221 m_obj->pushvalue();
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();
228 #else
229 assert(0);
230 #endif
234 template<class Policies>
235 void operator[](const Policies& p)
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();
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
254 throw error();
255 #else
256 assert(0);
257 #endif
261 private:
263 luabind::object* m_obj;
264 const char* m_member_name;
265 Tuple m_args;
266 mutable bool m_called;
271 } // detail
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
280 #else
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
293 tuple_t args;
294 #else
295 tuple_t args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a));
296 #endif
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
308 #endif