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();
96 typename
default_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
99 lua_State
* L
= m_func
->lua_state();
100 detail::stack_pop
p(L
, 1); // pop the return value
105 push_args_from_tuple
<1>::apply(L
, m_args
);
106 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 1, 0))
108 #ifndef LUABIND_NO_EXCEPTIONS
109 throw luabind::error();
115 #ifndef LUABIND_NO_ERROR_CHECKING
116 #ifndef LUABIND_NO_EXCEPTIONS
118 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
120 throw cant_convert_return_value();
123 assert(converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) >= 0);
126 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
129 template<class Policies
>
130 Ret
operator[](const Policies
& p
)
132 typedef typename
detail::find_conversion_policy
<0, Policies
>::type converter_policy
;
133 typename
converter_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
136 lua_State
* L
= m_func
->lua_state();
137 detail::stack_pop
popper(L
, 1); // pop the return value
142 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
143 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 1, 0))
145 #ifndef LUABIND_NO_EXCEPTIONS
152 #ifndef LUABIND_NO_ERROR_CHECKING
153 #ifndef LUABIND_NO_EXCEPTIONS
155 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
157 throw cant_convert_return_value();
160 assert(converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) >= 0);
163 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
168 luabind::functor
<Ret
>* m_func
;
170 mutable bool m_called
;
174 // if the proxy_member_caller returns void
175 template<class Tuple
>
176 class proxy_functor_void_caller
178 // template<class T> friend class luabind::functor;
181 proxy_functor_void_caller(luabind::functor
<void>* o
, const Tuple args
)
188 proxy_functor_void_caller(const proxy_functor_void_caller
& rhs
)
191 , m_called(rhs
.m_called
)
196 ~proxy_functor_void_caller()
198 if (m_called
) return;
201 lua_State
* L
= m_func
->lua_state();
206 push_args_from_tuple
<1>::apply(L
, m_args
);
207 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 0, 0))
209 #ifndef LUABIND_NO_EXCEPTIONS
210 throw luabind::error();
217 template<class Policies
>
218 void operator[](const Policies
& p
)
221 lua_State
* L
= m_func
->lua_state();
226 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
227 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 0, 0))
229 #ifndef LUABIND_NO_EXCEPTIONS
239 luabind::functor
<void>* m_func
;
241 mutable bool m_called
;
252 functor(lua_State
* L
, const char* name
) : L_(L
)
254 lua_pushstring(L
, name
);
255 lua_gettable(L
, LUA_GLOBALSINDEX
);
256 ref_
= detail::ref(L
);
265 functor(const functor
<Ret
>& obj
): L_(obj
.L_
)
267 lua_getref(L_
, obj
.ref_
);
268 ref_
= detail::ref(L_
);
273 if (ref_
!= LUA_NOREF
) detail::unref(L_
, ref_
);
276 // this is a safe substitute for an implicit converter to bool
277 typedef void (functor::*member_ptr
)() const;
278 operator member_ptr() const
280 if (is_valid()) return &functor::dummy
;
284 const functor
<Ret
>& operator=(const functor
<Ret
>& rhs
)
287 lua_getref(L_
, rhs
.ref_
);
288 ref_
= detail::ref(L_
);
292 bool operator==(const functor
<Ret
>& rhs
) const
294 if (ref_
== LUA_NOREF
|| rhs
.ref_
== LUA_NOREF
) return false;
297 bool result
= lua_equal(L_
, -1, -2) != 0;
302 bool operator!=(const functor
<Ret
>& rhs
) const
304 if (ref_
== LUA_NOREF
|| rhs
.ref_
== LUA_NOREF
) return true;
307 bool result
= lua_equal(L_
, -1, -2) == 0;
312 inline bool is_valid() const { return ref_
!= LUA_NOREF
; }
314 lua_State
* lua_state() const { return L_
; }
315 void pushvalue() const { lua_getref(L_
, ref_
); }
317 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/functor.hpp>, 1))
318 #include BOOST_PP_ITERATE()
320 // TODO: should be private
322 functor(lua_State
* L
, int ref
)
330 void dummy() const {}
337 #endif // LUABIND_FUNCTOR_HPP_INCLUDED
341 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
342 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
344 #if BOOST_PP_ITERATION() > 0
345 template<BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
347 typename
boost::mpl::if_
<boost::is_void
<Ret
>
348 , luabind::detail::proxy_functor_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
349 , luabind::detail::proxy_functor_caller
<Ret
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type
350 operator()(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS
, _
)) const
352 typedef boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> tuple_t
;
353 #if BOOST_PP_ITERATION() == 0
356 tuple_t
args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a
));
359 typedef typename
boost::mpl::if_
<boost::is_void
<Ret
>
360 , luabind::detail::proxy_functor_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
361 , luabind::detail::proxy_functor_caller
<Ret
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type proxy_type
;
363 return proxy_type(const_cast<luabind::functor
<Ret
>*>(this), args
);
366 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
367 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n