*** empty log message ***
[luabind.git] / luabind / functor.hpp
blob2aad94ba8f1a5e54aff26bd87395851b5e415bbc
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.
23 #if !BOOST_PP_IS_ITERATING
25 #ifndef LUABIND_FUNCTOR_HPP_INCLUDED
26 #define LUABIND_FUNCTOR_HPP_INCLUDED
28 #include <boost/preprocessor/repeat.hpp>
29 #include <boost/preprocessor/repetition/enum_params.hpp>
30 #include <boost/preprocessor/repetition/enum_params.hpp>
31 #include <boost/preprocessor/iteration/iterate.hpp>
32 #include <boost/type_traits/is_void.hpp>
33 #include <boost/mpl/if.hpp>
35 #include <luabind/config.hpp>
36 #include <luabind/detail/policy.hpp>
37 #include <luabind/detail/convert_to_lua.hpp>
38 #include <luabind/detail/error.hpp>
40 namespace luabind
42 //template<class Ret>
43 //class functor;
45 namespace detail
48 struct functor_from;
51 // if the proxy_functor_caller returns non-void
52 template<class Ret, class Tuple>
53 class proxy_functor_caller
55 // template<class T> friend class luabind::functor;
56 public:
58 proxy_functor_caller(luabind::functor<Ret>* o, const Tuple args)
59 : m_func(o)
60 , m_args(args)
61 , m_called(false)
65 proxy_functor_caller(const proxy_functor_caller& rhs)
66 : m_func(rhs.m_func)
67 , m_args(rhs.m_args)
68 , m_called(rhs.m_called)
70 rhs.m_called = true;
73 ~proxy_functor_caller()
75 if (m_called) return;
77 m_called = true;
78 lua_State* L = m_func->lua_state();
80 // get the function
81 m_func->pushvalue();
83 push_args_from_tuple<1>::apply(L, m_args);
84 if (lua_pcall(L, boost::tuples::length<Tuple>::value, 0, 0))
86 #ifndef LUABIND_NO_EXCEPTIONS
87 throw luabind::error(L);
88 #else
89 error_callback_fun e = detail::error_callback::get().err;
90 if (e) e(L);
92 assert(0 && "the lua function threw an error and exceptions are disabled."
93 "if you want to handle this error use luabind::set_error_callback()");
94 std::terminate();
95 #endif
99 operator Ret()
101 typename default_policy::template generate_converter<Ret, lua_to_cpp>::type converter;
103 m_called = true;
104 lua_State* L = m_func->lua_state();
105 #ifndef LUABIND_NO_ERROR_CHECKING
106 if (L == 0)
108 #ifndef LUABIND_NO_EXCEPTIONS
109 throw error(L);
110 #else
111 error_callback_fun e = detail::error_callback::get().err;
112 if (e) e(L);
114 assert(0 && "tried to call uninitialized functor object."
115 "if you want to handle this error use luabind::set_error_callback()");
116 std::terminate();
117 #endif
119 #endif
122 detail::stack_pop p(L, 1); // pop the return value
124 // get the function
125 m_func->pushvalue();
127 push_args_from_tuple<1>::apply(L, m_args);
128 if (lua_pcall(L, boost::tuples::length<Tuple>::value, 1, 0))
130 #ifndef LUABIND_NO_EXCEPTIONS
131 throw luabind::error(L);
132 #else
133 error_callback_fun e = detail::error_callback::get().err;
134 if (e) e(L);
136 assert(0 && "the lua function threw an error and exceptions are disabled."
137 "if you want to handle this error use luabind::set_error_callback()");
138 std::terminate();
139 #endif
142 #ifndef LUABIND_NO_ERROR_CHECKING
144 if (converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) < 0)
146 #ifndef LUABIND_NO_EXCEPTIONS
147 throw cast_failed(L, LUABIND_TYPEID(Ret));
148 #else
149 cast_failed_callback_fun e = detail::error_callback::get().cast;
150 if (e) e(L, LUABIND_TYPEID(Ret));
152 assert(0 && "the lua function's return value could not be converted."
153 "if you want to handle this error use luabind::set_error_callback()");
154 std::terminate();
155 #endif
157 #endif
158 return converter.apply(L, LUABIND_DECORATE_TYPE(Ret), -1);
161 template<class Policies>
162 Ret operator[](const Policies& p)
164 typedef typename detail::find_conversion_policy<0, Policies>::type converter_policy;
165 typename converter_policy::template generate_converter<Ret, lua_to_cpp>::type converter;
167 m_called = true;
168 lua_State* L = m_func->lua_state();
169 #ifndef LUABIND_NO_ERROR_CHECKING
170 if (L == 0)
172 #ifndef LUABIND_NO_EXCEPTIONS
173 throw error(L);
174 #else
175 error_callback_fun e = detail::error_callback::get().err;
176 if (e) e(L);
178 assert(0 && "tried to call uninitialized functor object."
179 "if you want to handle this error use luabind::set_error_callback()");
180 std::terminate();
181 #endif
183 #endif
185 detail::stack_pop popper(L, 1); // pop the return value
187 // get the function
188 m_func->pushvalue();
190 detail::push_args_from_tuple<1>::apply(L, m_args, p);
191 if (lua_pcall(L, boost::tuples::length<Tuple>::value, 1, 0))
193 #ifndef LUABIND_NO_EXCEPTIONS
194 throw error(L);
195 #else
196 error_callback_fun e = detail::error_callback::get().err;
197 if (e) e(L);
199 assert(0 && "the lua function threw an error and exceptions are disabled."
200 "if you want to handle this error use luabind::set_error_callback()");
201 std::terminate();
202 #endif
205 #ifndef LUABIND_NO_ERROR_CHECKING
207 if (converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) < 0)
209 #ifndef LUABIND_NO_EXCEPTIONS
210 throw cast_failed(L, LUABIND_TYPEID(Ret));
211 #else
212 cast_failed_callback_fun e = detail::error_callback::get().cast;
213 if (e) e(L, LUABIND_TYPEID(Ret));
215 assert(0 && "the lua function's return value could not be converted."
216 "if you want to handle this error use luabind::set_error_callback()");
217 std::terminate();
218 #endif
220 #endif
221 return converter.apply(L, LUABIND_DECORATE_TYPE(Ret), -1);
224 private:
226 luabind::functor<Ret>* m_func;
227 Tuple m_args;
228 mutable bool m_called;
232 // if the proxy_member_caller returns void
233 template<class Tuple>
234 class proxy_functor_void_caller
236 // template<class T> friend class luabind::functor;
237 public:
239 proxy_functor_void_caller(luabind::functor<void>* o, const Tuple args)
240 : m_func(o)
241 , m_args(args)
242 , m_called(false)
246 proxy_functor_void_caller(const proxy_functor_void_caller& rhs)
247 : m_func(rhs.m_func)
248 , m_args(rhs.m_args)
249 , m_called(rhs.m_called)
251 rhs.m_called = true;
254 ~proxy_functor_void_caller()
256 if (m_called) return;
258 m_called = true;
259 lua_State* L = m_func->lua_state();
260 #ifndef LUABIND_NO_ERROR_CHECKING
261 if (L == 0)
263 #ifndef LUABIND_NO_EXCEPTIONS
264 throw error(L);
265 #else
266 error_callback_fun e = detail::error_callback::get().err;
267 if (e) e(L);
269 assert(0 && "tried to call uninitialized functor object."
270 "if you want to handle this error use luabind::set_error_callback()");
271 std::terminate();
272 #endif
274 #endif
277 // get the function
278 m_func->pushvalue();
280 push_args_from_tuple<1>::apply(L, m_args);
281 if (lua_pcall(L, boost::tuples::length<Tuple>::value, 0, 0))
283 #ifndef LUABIND_NO_EXCEPTIONS
284 throw luabind::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 template<class Policies>
297 void operator[](const Policies& p)
299 m_called = true;
300 lua_State* L = m_func->lua_state();
301 #ifndef LUABIND_NO_ERROR_CHECKING
302 if (L == 0)
304 #ifndef LUABIND_NO_EXCEPTIONS
305 throw error(L);
306 #else
307 error_callback_fun e = detail::error_callback::get().err;
308 if (e) e(L);
310 assert(0 && "tried to call uninitialized functor object."
311 "if you want to handle this error use luabind::set_error_callback()");
312 std::terminate();
313 #endif
315 #endif
318 // get the function
319 m_func->pushvalue();
321 detail::push_args_from_tuple<1>::apply(L, m_args, p);
322 if (lua_pcall(L, boost::tuples::length<Tuple>::value, 0, 0))
324 #ifndef LUABIND_NO_EXCEPTIONS
325 throw error(L);
326 #else
327 error_callback_fun e = detail::error_callback::get().err;
328 if (e) e(L);
330 assert(0 && "the lua function threw an error and exceptions are disabled."
331 "if you want to handle this error use luabind::set_error_callback()");
332 std::terminate();
333 #endif
337 private:
339 luabind::functor<void>* m_func;
340 Tuple m_args;
341 mutable bool m_called;
345 } // detail
347 template<class Ret>
348 class functor
350 public:
352 functor(lua_State* L, const char* name)
353 : L_(L)
355 lua_pushstring(L, name);
356 lua_gettable(L, LUA_GLOBALSINDEX);
357 ref_.set(L_);
360 functor()
361 : L_(0)
365 functor(const functor<Ret>& obj)
366 : L_(obj.L_)
367 , ref_(obj.ref_)
371 // this is a safe substitute for an implicit converter to bool
372 typedef void (functor::*member_ptr)() const;
373 operator member_ptr() const
375 if (is_valid()) return &functor::dummy;
376 return 0;
379 const functor<Ret>& operator=(const functor<Ret>& rhs)
381 L_ = rhs.L_;
382 ref_ = rhs.ref_;
383 return *this;
386 bool operator==(const functor<Ret>& rhs) const
388 if (!ref_.is_valid() || !rhs.ref_.is_valid()) return false;
389 pushvalue();
390 rhs.pushvalue();
391 bool result = lua_equal(L_, -1, -2) != 0;
392 lua_pop(L_, 2);
393 return result;
396 bool operator!=(const functor<Ret>& rhs) const
398 if (!ref_.is_valid() || !rhs.ref_.is_valid()) return true;
399 pushvalue();
400 rhs.pushvalue();
401 bool result = lua_equal(L_, -1, -2) == 0;
402 lua_pop(L_, 2);
403 return result;
406 inline bool is_valid() const { return ref_.is_valid(); }
408 lua_State* lua_state() const { return L_; }
409 void pushvalue() const { ref_.get(L_); }
411 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/functor.hpp>, 1))
412 #include BOOST_PP_ITERATE()
414 // TODO: should be private
416 functor(lua_State* L, detail::lua_reference const& ref)
417 : L_(L)
418 , ref_(ref)
422 private:
424 void dummy() const {}
426 lua_State* L_;
427 detail::lua_reference ref_;
431 #endif // LUABIND_FUNCTOR_HPP_INCLUDED
433 #else
435 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
436 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
438 #if BOOST_PP_ITERATION() > 0
439 template<BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
440 #endif
441 typename boost::mpl::if_<boost::is_void<Ret>
442 , luabind::detail::proxy_functor_void_caller<boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> >
443 , luabind::detail::proxy_functor_caller<Ret, boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> > >::type
444 operator()(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS, _)) const
446 typedef boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> tuple_t;
447 #if BOOST_PP_ITERATION() == 0
448 tuple_t args;
449 #else
450 tuple_t args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a));
451 #endif
453 typedef typename boost::mpl::if_<boost::is_void<Ret>
454 , luabind::detail::proxy_functor_void_caller<boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> >
455 , luabind::detail::proxy_functor_caller<Ret, boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> > >::type proxy_type;
457 return proxy_type(const_cast<luabind::functor<Ret>*>(this), args);
460 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
461 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
463 #endif