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/detail/pcall.hpp>
40 #include <luabind/error.hpp>
53 // if the proxy_functor_caller returns non-void
54 template<class Ret
, class Tuple
>
55 class proxy_functor_caller
57 // template<class T> friend class luabind::functor;
60 proxy_functor_caller(luabind::functor
<Ret
>* o
, const Tuple args
)
67 proxy_functor_caller(const proxy_functor_caller
& rhs
)
70 , m_called(rhs
.m_called
)
75 ~proxy_functor_caller()
80 lua_State
* L
= m_func
->lua_state();
85 push_args_from_tuple
<1>::apply(L
, m_args
);
86 if (pcall(L
, boost::tuples::length
<Tuple
>::value
, 0))
88 #ifndef LUABIND_NO_EXCEPTIONS
89 throw luabind::error(L
);
91 error_callback_fun e
= get_error_callback();
94 assert(0 && "the lua function threw an error and exceptions are disabled."
95 "if you want to handle this error use luabind::set_error_callback()");
103 typename
default_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
106 lua_State
* L
= m_func
->lua_state();
107 #ifndef LUABIND_NO_ERROR_CHECKING
110 #ifndef LUABIND_NO_EXCEPTIONS
113 error_callback_fun e
= get_error_callback();
116 assert(0 && "tried to call uninitialized functor object."
117 "if you want to handle this error use luabind::set_error_callback()");
124 detail::stack_pop
p(L
, 1); // pop the return value
129 push_args_from_tuple
<1>::apply(L
, m_args
);
130 if (pcall(L
, boost::tuples::length
<Tuple
>::value
, 1))
132 #ifndef LUABIND_NO_EXCEPTIONS
133 throw luabind::error(L
);
135 error_callback_fun e
= get_error_callback();
138 assert(0 && "the lua function threw an error and exceptions are disabled."
139 "if you want to handle this error use luabind::set_error_callback()");
144 #ifndef LUABIND_NO_ERROR_CHECKING
146 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
148 #ifndef LUABIND_NO_EXCEPTIONS
149 throw cast_failed(L
, LUABIND_TYPEID(Ret
));
151 cast_failed_callback_fun e
= get_cast_failed_callback();
152 if (e
) e(L
, LUABIND_TYPEID(Ret
));
154 assert(0 && "the lua function's return value could not be converted."
155 "if you want to handle this error use luabind::set_error_callback()");
160 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
163 template<class Policies
>
164 Ret
operator[](const Policies
& p
)
166 typedef typename
detail::find_conversion_policy
<0, Policies
>::type converter_policy
;
167 typename
converter_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
170 lua_State
* L
= m_func
->lua_state();
171 #ifndef LUABIND_NO_ERROR_CHECKING
174 #ifndef LUABIND_NO_EXCEPTIONS
177 error_callback_fun e
= get_error_callback();
180 assert(0 && "tried to call uninitialized functor object."
181 "if you want to handle this error use luabind::set_error_callback()");
187 detail::stack_pop
popper(L
, 1); // pop the return value
192 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
193 if (pcall(L
, boost::tuples::length
<Tuple
>::value
, 1))
195 #ifndef LUABIND_NO_EXCEPTIONS
198 error_callback_fun e
= get_error_callback();
201 assert(0 && "the lua function threw an error and exceptions are disabled."
202 "if you want to handle this error use luabind::set_error_callback()");
207 #ifndef LUABIND_NO_ERROR_CHECKING
209 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
211 #ifndef LUABIND_NO_EXCEPTIONS
212 throw cast_failed(L
, LUABIND_TYPEID(Ret
));
214 cast_failed_callback_fun e
= get_cast_failed_callback();
215 if (e
) e(L
, LUABIND_TYPEID(Ret
));
217 assert(0 && "the lua function's return value could not be converted."
218 "if you want to handle this error use luabind::set_error_callback()");
223 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
228 luabind::functor
<Ret
>* m_func
;
230 mutable bool m_called
;
234 // if the proxy_member_caller returns void
235 template<class Tuple
>
236 class proxy_functor_void_caller
238 // template<class T> friend class luabind::functor;
241 proxy_functor_void_caller(luabind::functor
<void>* o
, const Tuple args
)
248 proxy_functor_void_caller(const proxy_functor_void_caller
& rhs
)
251 , m_called(rhs
.m_called
)
256 ~proxy_functor_void_caller()
258 if (m_called
) return;
261 lua_State
* L
= m_func
->lua_state();
262 #ifndef LUABIND_NO_ERROR_CHECKING
265 #ifndef LUABIND_NO_EXCEPTIONS
268 error_callback_fun e
= get_error_callback();
271 assert(0 && "tried to call uninitialized functor object."
272 "if you want to handle this error use luabind::set_error_callback()");
282 push_args_from_tuple
<1>::apply(L
, m_args
);
283 if (pcall(L
, boost::tuples::length
<Tuple
>::value
, 0))
285 #ifndef LUABIND_NO_EXCEPTIONS
286 throw luabind::error(L
);
288 error_callback_fun e
= get_error_callback();
291 assert(0 && "the lua function threw an error and exceptions are disabled."
292 "if you want to handle this error use luabind::set_error_callback()");
298 template<class Policies
>
299 void operator[](const Policies
& p
)
302 lua_State
* L
= m_func
->lua_state();
303 #ifndef LUABIND_NO_ERROR_CHECKING
306 #ifndef LUABIND_NO_EXCEPTIONS
309 error_callback_fun e
= get_error_callback();
312 assert(0 && "tried to call uninitialized functor object."
313 "if you want to handle this error use luabind::set_error_callback()");
323 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
324 if (pcall(L
, boost::tuples::length
<Tuple
>::value
, 0))
326 #ifndef LUABIND_NO_EXCEPTIONS
329 error_callback_fun e
= get_error_callback();
332 assert(0 && "the lua function threw an error and exceptions are disabled."
333 "if you want to handle this error use luabind::set_error_callback()");
341 luabind::functor
<void>* m_func
;
343 mutable bool m_called
;
354 functor(lua_State
* L
, const char* name
)
357 lua_pushstring(L
, name
);
358 lua_gettable(L
, LUA_GLOBALSINDEX
);
367 functor(const functor
<Ret
>& obj
)
373 // this is a safe substitute for an implicit converter to bool
374 typedef void (functor::*member_ptr
)() const;
375 operator member_ptr() const
377 if (is_valid()) return &functor::dummy
;
381 const functor
<Ret
>& operator=(const functor
<Ret
>& rhs
)
388 bool operator==(const functor
<Ret
>& rhs
) const
390 if (!ref_
.is_valid() || !rhs
.ref_
.is_valid()) return false;
393 bool result
= lua_equal(L_
, -1, -2) != 0;
398 bool operator!=(const functor
<Ret
>& rhs
) const
400 if (!ref_
.is_valid() || !rhs
.ref_
.is_valid()) return true;
403 bool result
= lua_equal(L_
, -1, -2) == 0;
408 inline bool is_valid() const { return ref_
.is_valid(); }
410 lua_State
* lua_state() const { return L_
; }
411 void pushvalue() const { ref_
.get(L_
); }
413 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/functor.hpp>, 1))
414 #include BOOST_PP_ITERATE()
416 // TODO: should be private
418 functor(lua_State
* L
, detail::lua_reference
const& ref
)
426 void dummy() const {}
429 detail::lua_reference ref_
;
433 #endif // LUABIND_FUNCTOR_HPP_INCLUDED
437 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
438 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
440 #if BOOST_PP_ITERATION() > 0
441 template<BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
443 typename
boost::mpl::if_
<boost::is_void
<Ret
>
444 , luabind::detail::proxy_functor_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
445 , luabind::detail::proxy_functor_caller
<Ret
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type
446 operator()(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS
, _
)) const
448 typedef boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> tuple_t
;
449 #if BOOST_PP_ITERATION() == 0
452 tuple_t
args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a
));
455 typedef typename
boost::mpl::if_
<boost::is_void
<Ret
>
456 , luabind::detail::proxy_functor_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
457 , luabind::detail::proxy_functor_caller
<Ret
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type proxy_type
;
459 return proxy_type(const_cast<luabind::functor
<Ret
>*>(this), args
);
462 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
463 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n