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/prefix.hpp>
36 #include <luabind/config.hpp>
37 #include <luabind/detail/policy.hpp>
38 #include <luabind/detail/convert_to_lua.hpp>
39 #include <luabind/error.hpp>
52 // if the proxy_functor_caller returns non-void
53 template<class Ret
, class Tuple
>
54 class proxy_functor_caller
56 // template<class T> friend class luabind::functor;
59 proxy_functor_caller(luabind::functor
<Ret
>* o
, const Tuple args
)
66 proxy_functor_caller(const proxy_functor_caller
& rhs
)
69 , m_called(rhs
.m_called
)
74 ~proxy_functor_caller()
79 lua_State
* L
= m_func
->lua_state();
84 push_args_from_tuple
<1>::apply(L
, m_args
);
85 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 0, 0))
87 #ifndef LUABIND_NO_EXCEPTIONS
88 throw luabind::error(L
);
90 error_callback_fun e
= detail::error_callback::get().err
;
93 assert(0 && "the lua function threw an error and exceptions are disabled."
94 "if you want to handle this error use luabind::set_error_callback()");
102 typename
default_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
105 lua_State
* L
= m_func
->lua_state();
106 #ifndef LUABIND_NO_ERROR_CHECKING
109 #ifndef LUABIND_NO_EXCEPTIONS
112 error_callback_fun e
= detail::error_callback::get().err
;
115 assert(0 && "tried to call uninitialized functor object."
116 "if you want to handle this error use luabind::set_error_callback()");
123 detail::stack_pop
p(L
, 1); // pop the return value
128 push_args_from_tuple
<1>::apply(L
, m_args
);
129 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 1, 0))
131 #ifndef LUABIND_NO_EXCEPTIONS
132 throw luabind::error(L
);
134 error_callback_fun e
= detail::error_callback::get().err
;
137 assert(0 && "the lua function threw an error and exceptions are disabled."
138 "if you want to handle this error use luabind::set_error_callback()");
143 #ifndef LUABIND_NO_ERROR_CHECKING
145 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
147 #ifndef LUABIND_NO_EXCEPTIONS
148 throw cast_failed(L
, LUABIND_TYPEID(Ret
));
150 cast_failed_callback_fun e
= detail::error_callback::get().cast
;
151 if (e
) e(L
, LUABIND_TYPEID(Ret
));
153 assert(0 && "the lua function's return value could not be converted."
154 "if you want to handle this error use luabind::set_error_callback()");
159 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
162 template<class Policies
>
163 Ret
operator[](const Policies
& p
)
165 typedef typename
detail::find_conversion_policy
<0, Policies
>::type converter_policy
;
166 typename
converter_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
169 lua_State
* L
= m_func
->lua_state();
170 #ifndef LUABIND_NO_ERROR_CHECKING
173 #ifndef LUABIND_NO_EXCEPTIONS
176 error_callback_fun e
= detail::error_callback::get().err
;
179 assert(0 && "tried to call uninitialized functor object."
180 "if you want to handle this error use luabind::set_error_callback()");
186 detail::stack_pop
popper(L
, 1); // pop the return value
191 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
192 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 1, 0))
194 #ifndef LUABIND_NO_EXCEPTIONS
197 error_callback_fun e
= detail::error_callback::get().err
;
200 assert(0 && "the lua function threw an error and exceptions are disabled."
201 "if you want to handle this error use luabind::set_error_callback()");
206 #ifndef LUABIND_NO_ERROR_CHECKING
208 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
210 #ifndef LUABIND_NO_EXCEPTIONS
211 throw cast_failed(L
, LUABIND_TYPEID(Ret
));
213 cast_failed_callback_fun e
= detail::error_callback::get().cast
;
214 if (e
) e(L
, LUABIND_TYPEID(Ret
));
216 assert(0 && "the lua function's return value could not be converted."
217 "if you want to handle this error use luabind::set_error_callback()");
222 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
227 luabind::functor
<Ret
>* m_func
;
229 mutable bool m_called
;
233 // if the proxy_member_caller returns void
234 template<class Tuple
>
235 class proxy_functor_void_caller
237 // template<class T> friend class luabind::functor;
240 proxy_functor_void_caller(luabind::functor
<void>* o
, const Tuple args
)
247 proxy_functor_void_caller(const proxy_functor_void_caller
& rhs
)
250 , m_called(rhs
.m_called
)
255 ~proxy_functor_void_caller()
257 if (m_called
) return;
260 lua_State
* L
= m_func
->lua_state();
261 #ifndef LUABIND_NO_ERROR_CHECKING
264 #ifndef LUABIND_NO_EXCEPTIONS
267 error_callback_fun e
= detail::error_callback::get().err
;
270 assert(0 && "tried to call uninitialized functor object."
271 "if you want to handle this error use luabind::set_error_callback()");
281 push_args_from_tuple
<1>::apply(L
, m_args
);
282 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 0, 0))
284 #ifndef LUABIND_NO_EXCEPTIONS
285 throw luabind::error(L
);
287 error_callback_fun e
= detail::error_callback::get().err
;
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()");
297 template<class Policies
>
298 void operator[](const Policies
& p
)
301 lua_State
* L
= m_func
->lua_state();
302 #ifndef LUABIND_NO_ERROR_CHECKING
305 #ifndef LUABIND_NO_EXCEPTIONS
308 error_callback_fun e
= detail::error_callback::get().err
;
311 assert(0 && "tried to call uninitialized functor object."
312 "if you want to handle this error use luabind::set_error_callback()");
322 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
323 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 0, 0))
325 #ifndef LUABIND_NO_EXCEPTIONS
328 error_callback_fun e
= detail::error_callback::get().err
;
331 assert(0 && "the lua function threw an error and exceptions are disabled."
332 "if you want to handle this error use luabind::set_error_callback()");
340 luabind::functor
<void>* m_func
;
342 mutable bool m_called
;
353 functor(lua_State
* L
, const char* name
)
356 lua_pushstring(L
, name
);
357 lua_gettable(L
, LUA_GLOBALSINDEX
);
366 functor(const functor
<Ret
>& obj
)
372 // this is a safe substitute for an implicit converter to bool
373 typedef void (functor::*member_ptr
)() const;
374 operator member_ptr() const
376 if (is_valid()) return &functor::dummy
;
380 const functor
<Ret
>& operator=(const functor
<Ret
>& rhs
)
387 bool operator==(const functor
<Ret
>& rhs
) const
389 if (!ref_
.is_valid() || !rhs
.ref_
.is_valid()) return false;
392 bool result
= lua_equal(L_
, -1, -2) != 0;
397 bool operator!=(const functor
<Ret
>& rhs
) const
399 if (!ref_
.is_valid() || !rhs
.ref_
.is_valid()) return true;
402 bool result
= lua_equal(L_
, -1, -2) == 0;
407 inline bool is_valid() const { return ref_
.is_valid(); }
409 lua_State
* lua_state() const { return L_
; }
410 void pushvalue() const { ref_
.get(L_
); }
412 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/functor.hpp>, 1))
413 #include BOOST_PP_ITERATE()
415 // TODO: should be private
417 functor(lua_State
* L
, detail::lua_reference
const& ref
)
425 void dummy() const {}
428 detail::lua_reference ref_
;
432 #endif // LUABIND_FUNCTOR_HPP_INCLUDED
436 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
437 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
439 #if BOOST_PP_ITERATION() > 0
440 template<BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
442 typename
boost::mpl::if_
<boost::is_void
<Ret
>
443 , luabind::detail::proxy_functor_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
444 , luabind::detail::proxy_functor_caller
<Ret
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type
445 operator()(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS
, _
)) const
447 typedef boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> tuple_t
;
448 #if BOOST_PP_ITERATION() == 0
451 tuple_t
args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a
));
454 typedef typename
boost::mpl::if_
<boost::is_void
<Ret
>
455 , luabind::detail::proxy_functor_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
456 , luabind::detail::proxy_functor_caller
<Ret
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type proxy_type
;
458 return proxy_type(const_cast<luabind::functor
<Ret
>*>(this), args
);
461 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
462 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n