added set_error_callback() and set_cast_failed_callback() and improved the exceptions...
[luabind.git] / luabind / functor.hpp
blobf02387cb6303e00a2ba2444c3a6a86e37307a0fd
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 detail::stack_pop p(L, 1); // pop the return value
107 // get the function
108 m_func->pushvalue();
110 push_args_from_tuple<1>::apply(L, m_args);
111 if (lua_pcall(L, boost::tuples::length<Tuple>::value, 1, 0))
113 #ifndef LUABIND_NO_EXCEPTIONS
114 throw luabind::error(L);
115 #else
116 error_callback_fun e = detail::error_callback::get().err;
117 if (e) e(L);
119 assert(0 && "the lua function threw an error and exceptions are disabled."
120 "if you want to handle this error use luabind::set_error_callback()");
121 std::terminate();
122 #endif
125 #ifndef LUABIND_NO_ERROR_CHECKING
127 if (converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) < 0)
129 #ifndef LUABIND_NO_EXCEPTIONS
130 throw cast_failed(L, LUABIND_TYPEID(Ret));
131 #else
132 cast_failed_callback_fun e = detail::error_callback::get().cast;
133 if (e) e(L, LUABIND_TYPEID(Ret));
135 assert(0 && "the lua function's return value could not be converted."
136 "if you want to handle this error use luabind::set_error_callback()");
137 std::terminate();
138 #endif
140 #endif
141 return converter.apply(L, LUABIND_DECORATE_TYPE(Ret), -1);
144 template<class Policies>
145 Ret operator[](const Policies& p)
147 typedef typename detail::find_conversion_policy<0, Policies>::type converter_policy;
148 typename converter_policy::template generate_converter<Ret, lua_to_cpp>::type converter;
150 m_called = true;
151 lua_State* L = m_func->lua_state();
152 detail::stack_pop popper(L, 1); // pop the return value
154 // get the function
155 m_func->pushvalue();
157 detail::push_args_from_tuple<1>::apply(L, m_args, p);
158 if (lua_pcall(L, boost::tuples::length<Tuple>::value, 1, 0))
160 #ifndef LUABIND_NO_EXCEPTIONS
161 throw error(L);
162 #else
163 error_callback_fun e = detail::error_callback::get().err;
164 if (e) e(L);
166 assert(0 && "the lua function threw an error and exceptions are disabled."
167 "if you want to handle this error use luabind::set_error_callback()");
168 std::terminate();
169 #endif
172 #ifndef LUABIND_NO_ERROR_CHECKING
174 if (converter.match(L, LUABIND_DECORATE_TYPE(Ret), -1) < 0)
176 #ifndef LUABIND_NO_EXCEPTIONS
177 throw cast_failed(L, LUABIND_TYPEID(Ret));
178 #else
179 cast_failed_callback_fun e = detail::error_callback::get().cast;
180 if (e) e(L, LUABIND_TYPEID(Ret));
182 assert(0 && "the lua function's return value could not be converted."
183 "if you want to handle this error use luabind::set_error_callback()");
184 std::terminate();
185 #endif
187 #endif
188 return converter.apply(L, LUABIND_DECORATE_TYPE(Ret), -1);
191 private:
193 luabind::functor<Ret>* m_func;
194 Tuple m_args;
195 mutable bool m_called;
199 // if the proxy_member_caller returns void
200 template<class Tuple>
201 class proxy_functor_void_caller
203 // template<class T> friend class luabind::functor;
204 public:
206 proxy_functor_void_caller(luabind::functor<void>* o, const Tuple args)
207 : m_func(o)
208 , m_args(args)
209 , m_called(false)
213 proxy_functor_void_caller(const proxy_functor_void_caller& rhs)
214 : m_func(rhs.m_func)
215 , m_args(rhs.m_args)
216 , m_called(rhs.m_called)
218 rhs.m_called = true;
221 ~proxy_functor_void_caller()
223 if (m_called) return;
225 m_called = true;
226 lua_State* L = m_func->lua_state();
228 // get the function
229 m_func->pushvalue();
231 push_args_from_tuple<1>::apply(L, m_args);
232 if (lua_pcall(L, boost::tuples::length<Tuple>::value, 0, 0))
234 #ifndef LUABIND_NO_EXCEPTIONS
235 throw luabind::error(L);
236 #else
237 error_callback_fun e = detail::error_callback::get().err;
238 if (e) e(L);
240 assert(0 && "the lua function threw an error and exceptions are disabled."
241 "if you want to handle this error use luabind::set_error_callback()");
242 std::terminate();
243 #endif
247 template<class Policies>
248 void operator[](const Policies& p)
250 m_called = true;
251 lua_State* L = m_func->lua_state();
253 // get the function
254 m_func->pushvalue();
256 detail::push_args_from_tuple<1>::apply(L, m_args, p);
257 if (lua_pcall(L, boost::tuples::length<Tuple>::value, 0, 0))
259 #ifndef LUABIND_NO_EXCEPTIONS
260 throw error(L);
261 #else
262 error_callback_fun e = detail::error_callback::get().err;
263 if (e) e(L);
265 assert(0 && "the lua function threw an error and exceptions are disabled."
266 "if you want to handle this error use luabind::set_error_callback()");
267 std::terminate();
268 #endif
272 private:
274 luabind::functor<void>* m_func;
275 Tuple m_args;
276 mutable bool m_called;
280 } // detail
282 template<class Ret>
283 class functor
285 public:
287 functor(lua_State* L, const char* name) : L_(L)
289 lua_pushstring(L, name);
290 lua_gettable(L, LUA_GLOBALSINDEX);
291 ref_ = detail::ref(L);
294 functor()
295 : L_(0)
296 , ref_(LUA_NOREF)
300 functor(const functor<Ret>& obj): L_(obj.L_)
302 lua_getref(L_, obj.ref_);
303 ref_ = detail::ref(L_);
306 ~functor()
308 if (ref_ != LUA_NOREF) detail::unref(L_, ref_);
311 // this is a safe substitute for an implicit converter to bool
312 typedef void (functor::*member_ptr)() const;
313 operator member_ptr() const
315 if (is_valid()) return &functor::dummy;
316 return 0;
319 const functor<Ret>& operator=(const functor<Ret>& rhs)
321 L_ = rhs.L_;
322 lua_getref(L_, rhs.ref_);
323 ref_ = detail::ref(L_);
324 return *this;
327 bool operator==(const functor<Ret>& rhs) const
329 if (ref_ == LUA_NOREF || rhs.ref_ == LUA_NOREF) return false;
330 pushvalue();
331 rhs.pushvalue();
332 bool result = lua_equal(L_, -1, -2) != 0;
333 lua_pop(L_, 2);
334 return result;
337 bool operator!=(const functor<Ret>& rhs) const
339 if (ref_ == LUA_NOREF || rhs.ref_ == LUA_NOREF) return true;
340 pushvalue();
341 rhs.pushvalue();
342 bool result = lua_equal(L_, -1, -2) == 0;
343 lua_pop(L_, 2);
344 return result;
347 inline bool is_valid() const { return ref_ != LUA_NOREF; }
349 lua_State* lua_state() const { return L_; }
350 void pushvalue() const { lua_getref(L_, ref_); }
352 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/functor.hpp>, 1))
353 #include BOOST_PP_ITERATE()
355 // TODO: should be private
357 functor(lua_State* L, int ref)
358 : L_(L)
359 , ref_(ref)
363 private:
365 void dummy() const {}
367 lua_State* L_;
368 int ref_;
372 #endif // LUABIND_FUNCTOR_HPP_INCLUDED
374 #else
376 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
377 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
379 #if BOOST_PP_ITERATION() > 0
380 template<BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
381 #endif
382 typename boost::mpl::if_<boost::is_void<Ret>
383 , luabind::detail::proxy_functor_void_caller<boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> >
384 , luabind::detail::proxy_functor_caller<Ret, boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> > >::type
385 operator()(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS, _)) const
387 typedef boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> tuple_t;
388 #if BOOST_PP_ITERATION() == 0
389 tuple_t args;
390 #else
391 tuple_t args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a));
392 #endif
394 typedef typename boost::mpl::if_<boost::is_void<Ret>
395 , luabind::detail::proxy_functor_void_caller<boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> >
396 , luabind::detail::proxy_functor_caller<Ret, boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> > >::type proxy_type;
398 return proxy_type(const_cast<luabind::functor<Ret>*>(this), args);
401 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
402 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
404 #endif