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 detail::stack_pop
p(L
, 1); // pop the return value
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
);
116 error_callback_fun e
= detail::error_callback::get().err
;
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()");
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
));
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()");
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
;
151 lua_State
* L
= m_func
->lua_state();
152 detail::stack_pop
popper(L
, 1); // pop the return value
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
163 error_callback_fun e
= detail::error_callback::get().err
;
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()");
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
));
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()");
188 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
193 luabind::functor
<Ret
>* m_func
;
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;
206 proxy_functor_void_caller(luabind::functor
<void>* o
, const Tuple args
)
213 proxy_functor_void_caller(const proxy_functor_void_caller
& rhs
)
216 , m_called(rhs
.m_called
)
221 ~proxy_functor_void_caller()
223 if (m_called
) return;
226 lua_State
* L
= m_func
->lua_state();
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
);
237 error_callback_fun e
= detail::error_callback::get().err
;
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()");
247 template<class Policies
>
248 void operator[](const Policies
& p
)
251 lua_State
* L
= m_func
->lua_state();
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
262 error_callback_fun e
= detail::error_callback::get().err
;
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()");
274 luabind::functor
<void>* m_func
;
276 mutable bool m_called
;
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
);
300 functor(const functor
<Ret
>& obj
): L_(obj
.L_
)
302 lua_getref(L_
, obj
.ref_
);
303 ref_
= detail::ref(L_
);
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
;
319 const functor
<Ret
>& operator=(const functor
<Ret
>& rhs
)
322 lua_getref(L_
, rhs
.ref_
);
323 ref_
= detail::ref(L_
);
327 bool operator==(const functor
<Ret
>& rhs
) const
329 if (ref_
== LUA_NOREF
|| rhs
.ref_
== LUA_NOREF
) return false;
332 bool result
= lua_equal(L_
, -1, -2) != 0;
337 bool operator!=(const functor
<Ret
>& rhs
) const
339 if (ref_
== LUA_NOREF
|| rhs
.ref_
== LUA_NOREF
) return true;
342 bool result
= lua_equal(L_
, -1, -2) == 0;
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
)
365 void dummy() const {}
372 #endif // LUABIND_FUNCTOR_HPP_INCLUDED
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
)>
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
391 tuple_t
args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a
));
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