moved some headers out of detail/
[luabind.git] / luabind / detail / call_function.hpp
blob8e2d0a63e9c565a404347dc81ad84e3f74039e93
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>
44 namespace luabind
46 namespace detail
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;
56 public:
58 proxy_function_caller(lua_State* L, const char* name, const Tuple args)
59 : m_state(L)
60 , m_fun_name(name)
61 , m_args(args)
62 , m_called(false)
66 proxy_function_caller(const proxy_function_caller& rhs)
67 : m_state(rhs.m_state)
68 , m_fun_name(rhs.m_fun_name)
69 , m_args(rhs.m_args)
70 , m_called(rhs.m_called)
72 rhs.m_called = true;
75 ~proxy_function_caller()
77 if (m_called) return;
79 m_called = true;
80 lua_State* L = m_state;;
82 // get the function
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);
91 #else
92 error_callback_fun e = detail::error_callback::get().err;
93 if (e) e(L);
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()");
97 std::terminate();
99 #endif
103 operator Ret()
105 typename default_policy::template generate_converter<Ret, lua_to_cpp>::type converter;
107 m_called = true;
108 lua_State* L = m_state;;
109 detail::stack_pop p(L, 1); // pop the return value
111 // get the function
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);
120 #else
121 error_callback_fun e = detail::error_callback::get().err;
122 if (e) e(L);
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()");
126 std::terminate();
127 #endif
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));
136 #else
137 cast_failed_callback_fun e = detail::error_callback::get().cast;
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()");
142 std::terminate();
144 #endif
146 #endif
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;
156 m_called = true;
157 lua_State* L = m_state;;
158 detail::stack_pop popper(L, 1); // pop the return value
160 // get the function
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
168 throw error(L);
169 #else
170 error_callback_fun e = detail::error_callback::get().err;
171 if (e) e(L);
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()");
175 std::terminate();
176 #endif
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));
185 #else
186 cast_failed_callback_fun e = detail::error_callback::get().cast;
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()");
191 std::terminate();
193 #endif
195 #endif
196 return converter.apply(L, LUABIND_DECORATE_TYPE(Ret), -1);
199 private:
201 lua_State* m_state;
202 const char* m_fun_name;
203 Tuple m_args;
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;
213 public:
215 proxy_function_void_caller(lua_State* L, const char* name, const Tuple args)
216 : m_state(L)
217 , m_fun_name(name)
218 , m_args(args)
219 , m_called(false)
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)
226 , m_args(rhs.m_args)
227 , m_called(rhs.m_called)
229 rhs.m_called = true;
232 ~proxy_function_void_caller()
234 if (m_called) return;
236 m_called = true;
237 lua_State* L = m_state;;
239 // get the function
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);
248 #else
249 error_callback_fun e = detail::error_callback::get().err;
250 if (e) e(L);
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()");
254 std::terminate();
255 #endif
259 template<class Policies>
260 void operator[](const Policies& p)
262 m_called = true;
263 lua_State* L = m_state;;
265 // get the function
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
273 throw error(L);
274 #else
275 error_callback_fun e = detail::error_callback::get().err;
276 if (e) e(L);
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()");
280 std::terminate();
281 #endif
285 private:
287 lua_State* m_state;
288 const char* m_fun_name;
289 Tuple m_args;
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
317 tuple_t args;
318 #else
319 tuple_t args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a));
320 #endif
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
332 #endif