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>
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;
58 proxy_functor_caller(luabind::functor
<Ret
>* o
, const Tuple args
)
65 proxy_functor_caller(const proxy_functor_caller
& rhs
)
68 , m_called(rhs
.m_called
)
73 ~proxy_functor_caller()
78 lua_State
* L
= m_func
->lua_state();
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
);
89 error_callback_fun e
= detail::error_callback::get().err
;
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()");
101 typename
default_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
104 lua_State
* L
= m_func
->lua_state();
105 #ifndef LUABIND_NO_ERROR_CHECKING
108 #ifndef LUABIND_NO_EXCEPTIONS
111 error_callback_fun e
= detail::error_callback::get().err
;
114 assert(0 && "tried to call uninitialized functor object."
115 "if you want to handle this error use luabind::set_error_callback()");
122 detail::stack_pop
p(L
, 1); // pop the return value
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
);
133 error_callback_fun e
= detail::error_callback::get().err
;
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()");
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
));
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()");
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
;
168 lua_State
* L
= m_func
->lua_state();
169 #ifndef LUABIND_NO_ERROR_CHECKING
172 #ifndef LUABIND_NO_EXCEPTIONS
175 error_callback_fun e
= detail::error_callback::get().err
;
178 assert(0 && "tried to call uninitialized functor object."
179 "if you want to handle this error use luabind::set_error_callback()");
185 detail::stack_pop
popper(L
, 1); // pop the return value
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
196 error_callback_fun e
= detail::error_callback::get().err
;
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()");
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
));
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()");
221 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
226 luabind::functor
<Ret
>* m_func
;
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;
239 proxy_functor_void_caller(luabind::functor
<void>* o
, const Tuple args
)
246 proxy_functor_void_caller(const proxy_functor_void_caller
& rhs
)
249 , m_called(rhs
.m_called
)
254 ~proxy_functor_void_caller()
256 if (m_called
) return;
259 lua_State
* L
= m_func
->lua_state();
260 #ifndef LUABIND_NO_ERROR_CHECKING
263 #ifndef LUABIND_NO_EXCEPTIONS
266 error_callback_fun e
= detail::error_callback::get().err
;
269 assert(0 && "tried to call uninitialized functor object."
270 "if you want to handle this error use luabind::set_error_callback()");
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
);
286 error_callback_fun e
= detail::error_callback::get().err
;
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()");
296 template<class Policies
>
297 void operator[](const Policies
& p
)
300 lua_State
* L
= m_func
->lua_state();
301 #ifndef LUABIND_NO_ERROR_CHECKING
304 #ifndef LUABIND_NO_EXCEPTIONS
307 error_callback_fun e
= detail::error_callback::get().err
;
310 assert(0 && "tried to call uninitialized functor object."
311 "if you want to handle this error use luabind::set_error_callback()");
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
327 error_callback_fun e
= detail::error_callback::get().err
;
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()");
339 luabind::functor
<void>* m_func
;
341 mutable bool m_called
;
352 functor(lua_State
* L
, const char* name
)
355 lua_pushstring(L
, name
);
356 lua_gettable(L
, LUA_GLOBALSINDEX
);
365 functor(const functor
<Ret
>& obj
)
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
;
379 const functor
<Ret
>& operator=(const functor
<Ret
>& rhs
)
386 bool operator==(const functor
<Ret
>& rhs
) const
388 if (!ref_
.is_valid() || !rhs
.ref_
.is_valid()) return false;
391 bool result
= lua_equal(L_
, -1, -2) != 0;
396 bool operator!=(const functor
<Ret
>& rhs
) const
398 if (!ref_
.is_valid() || !rhs
.ref_
.is_valid()) return true;
401 bool result
= lua_equal(L_
, -1, -2) == 0;
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
)
424 void dummy() const {}
427 detail::lua_reference ref_
;
431 #endif // LUABIND_FUNCTOR_HPP_INCLUDED
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
)>
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
450 tuple_t
args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a
));
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