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.
24 #if !BOOST_PP_IS_ITERATING
26 #ifndef LUABIND_CALL_FUNCTION_HPP_INCLUDED
27 #define LUABIND_CALL_FUNCTION_HPP_INCLUDED
29 #include <luabind/config.hpp>
31 #include <boost/mpl/if.hpp>
32 #include <boost/tuple/tuple.hpp>
33 #include <boost/mpl/or.hpp>
34 #include <boost/preprocessor/repeat.hpp>
35 #include <boost/preprocessor/iteration/iterate.hpp>
36 #include <boost/preprocessor/repetition/enum.hpp>
37 #include <boost/preprocessor/repetition/enum_params.hpp>
38 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
39 #include <boost/preprocessor/punctuation/comma_if.hpp>
41 #include <luabind/detail/convert_to_lua.hpp>
42 #include <luabind/error.hpp>
51 // if the proxy_function_caller returns non-void
52 template<class Ret
, class Tuple
>
53 class proxy_function_caller
55 // friend class luabind::object;
58 proxy_function_caller(lua_State
* L
, const char* name
, const Tuple args
)
66 proxy_function_caller(const proxy_function_caller
& rhs
)
67 : m_state(rhs
.m_state
)
68 , m_fun_name(rhs
.m_fun_name
)
70 , m_called(rhs
.m_called
)
75 ~proxy_function_caller()
80 lua_State
* L
= m_state
;;
83 lua_pushstring(L
, m_fun_name
);
84 lua_gettable(L
, LUA_GLOBALSINDEX
);
86 push_args_from_tuple
<1>::apply(L
, m_args
);
87 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 0, 0))
89 #ifndef LUABIND_NO_EXCEPTIONS
90 throw luabind::error(L
);
92 error_callback_fun e
= get_error_callback();
95 assert(0 && "the lua function threw an error and exceptions are disabled."
96 " If you want to handle the error you can use luabind::set_error_callback()");
105 typename
default_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
108 lua_State
* L
= m_state
;;
109 detail::stack_pop
p(L
, 1); // pop the return value
112 lua_pushstring(L
, m_fun_name
);
113 lua_gettable(L
, LUA_GLOBALSINDEX
);
115 push_args_from_tuple
<1>::apply(L
, m_args
);
116 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 1, 0))
118 #ifndef LUABIND_NO_EXCEPTIONS
119 throw luabind::error(L
);
121 error_callback_fun e
= get_error_callback();
124 assert(0 && "the lua function threw an error and exceptions are disabled."
125 " If you want to handle the error you can use luabind::set_error_callback()");
130 #ifndef LUABIND_NO_ERROR_CHECKING
132 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
134 #ifndef LUABIND_NO_EXCEPTIONS
135 throw cast_failed(L
, LUABIND_TYPEID(Ret
));
137 cast_failed_callback_fun e
= get_cast_failed_callback();
138 if (e
) e(L
, LUABIND_TYPEID(Ret
));
140 assert(0 && "the lua function's return value could not be converted."
141 " If you want to handle the error you can use luabind::set_error_callback()");
147 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
150 template<class Policies
>
151 Ret
operator[](const Policies
& p
)
153 typedef typename
detail::find_conversion_policy
<0, Policies
>::type converter_policy
;
154 typename
converter_policy::template generate_converter
<Ret
, lua_to_cpp
>::type converter
;
157 lua_State
* L
= m_state
;;
158 detail::stack_pop
popper(L
, 1); // pop the return value
161 lua_pushstring(L
, m_fun_name
);
162 lua_gettable(L
, LUA_GLOBALSINDEX
);
164 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
165 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 1, 0))
167 #ifndef LUABIND_NO_EXCEPTIONS
170 error_callback_fun e
= get_error_callback();
173 assert(0 && "the lua function threw an error and exceptions are disabled."
174 " If you want to handle the error you can use luabind::set_error_callback()");
179 #ifndef LUABIND_NO_ERROR_CHECKING
181 if (converter
.match(L
, LUABIND_DECORATE_TYPE(Ret
), -1) < 0)
183 #ifndef LUABIND_NO_EXCEPTIONS
184 throw cast_failed(L
, LUABIND_TYPEID(Ret
));
186 cast_failed_callback_fun e
= get_cast_failed_callback();
187 if (e
) e(L
, LUABIND_TYPEID(Ret
));
189 assert(0 && "the lua function's return value could not be converted."
190 " If you want to handle the error you can use luabind::set_error_callback()");
196 return converter
.apply(L
, LUABIND_DECORATE_TYPE(Ret
), -1);
202 const char* m_fun_name
;
204 mutable bool m_called
;
208 // if the proxy_member_caller returns void
209 template<class Tuple
>
210 class proxy_function_void_caller
212 friend class luabind::object
;
215 proxy_function_void_caller(lua_State
* L
, const char* name
, const Tuple args
)
223 proxy_function_void_caller(const proxy_function_void_caller
& rhs
)
224 : m_state(rhs
.m_state
)
225 , m_fun_name(rhs
.m_fun_name
)
227 , m_called(rhs
.m_called
)
232 ~proxy_function_void_caller()
234 if (m_called
) return;
237 lua_State
* L
= m_state
;;
240 lua_pushstring(L
, m_fun_name
);
241 lua_gettable(L
, LUA_GLOBALSINDEX
);
243 push_args_from_tuple
<1>::apply(L
, m_args
);
244 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 0, 0))
246 #ifndef LUABIND_NO_EXCEPTIONS
247 throw luabind::error(L
);
249 error_callback_fun e
= get_error_callback();
252 assert(0 && "the lua function threw an error and exceptions are disabled."
253 " If you want to handle the error you can use luabind::set_error_callback()");
259 template<class Policies
>
260 void operator[](const Policies
& p
)
263 lua_State
* L
= m_state
;;
266 lua_pushstring(L
, m_fun_name
);
267 lua_gettable(L
, LUA_GLOBALSINDEX
);
269 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
270 if (lua_pcall(L
, boost::tuples::length
<Tuple
>::value
, 0, 0))
272 #ifndef LUABIND_NO_EXCEPTIONS
275 error_callback_fun e
= get_error_callback();
278 assert(0 && "the lua function threw an error and exceptions are disabled."
279 " If you want to handle the error you can use luabind::set_error_callback()");
288 const char* m_fun_name
;
290 mutable bool m_called
;
296 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/detail/call_function.hpp>, 1))
297 #include BOOST_PP_ITERATE()
301 #endif // LUABIND_CALL_FUNCTION_HPP_INCLUDED
303 #elif BOOST_PP_ITERATION_FLAGS() == 1
305 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
306 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
309 template<class Ret
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
310 typename
boost::mpl::if_
<boost::is_void
<Ret
>
311 , luabind::detail::proxy_function_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
312 , luabind::detail::proxy_function_caller
<Ret
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type
313 call_function(lua_State
* L
, const char* name
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS
, _
) )
315 typedef boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> tuple_t
;
316 #if BOOST_PP_ITERATION() == 0
319 tuple_t
args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a
));
321 typedef typename
boost::mpl::if_
<boost::is_void
<Ret
>
322 , luabind::detail::proxy_function_void_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
323 , luabind::detail::proxy_function_caller
<Ret
, boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> > >::type proxy_type
;
325 return proxy_type(L
, name
, args
);
328 #undef LUABIND_OPERATOR_PARAMS
329 #undef LUABIND_TUPLE_PARAMS