added some cases to held_type_test.
[luabind.git] / luabind / function.hpp
blob91480c05c7a5de6e62a8a1a084074858015f7cc1
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_FUNCTION_HPP_INCLUDED
27 #define LUABIND_FUNCTION_HPP_INCLUDED
29 #include <luabind/config.hpp>
31 #include <boost/config.hpp>
32 #include <boost/preprocessor/repeat.hpp>
33 #include <boost/preprocessor/iteration/iterate.hpp>
34 #include <boost/preprocessor/repetition/enum.hpp>
35 #include <boost/preprocessor/repetition/enum_params.hpp>
36 #include <boost/preprocessor/cat.hpp>
38 #include <luabind/detail/signature_match.hpp>
39 #include <luabind/detail/call_function.hpp>
40 #include <luabind/detail/get_overload_signature.hpp>
41 #include <luabind/detail/overload_rep_base.hpp>
43 #include <luabind/scope.hpp>
45 namespace luabind
47 namespace detail
50 namespace free_functions
53 struct overload_rep: public overload_rep_base
56 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 1))
57 #include BOOST_PP_ITERATE()
59 inline bool operator==(const overload_rep& o) const
61 if (o.m_arity != m_arity) return false;
62 if (o.m_num_args != m_num_args) return false;
63 for (int i = 0; i < m_num_args; ++i)
64 if (!(LUABIND_TYPE_INFO_EQUAL(m_params[i], o.m_params[i]))) return false;
65 return true;
68 typedef int(*call_ptr)(lua_State*, void(*)());
70 inline void set_fun(call_ptr f) { call_fun = f; }
71 inline int call(lua_State* L, void(*f)()) const { return call_fun(L, f); }
73 // this is the actual function pointer to be called when this overload is invoked
74 void (*fun)();
76 private:
78 int m_num_args;
79 call_ptr call_fun;
81 // the number of parameters this overload takes
82 // these are used to detect when a function by a derived class.
83 // int m_arity;
84 // the types of the parameter it takes
85 LUABIND_TYPE_INFO m_params[LUABIND_MAX_ARITY];
91 struct function_rep
93 void add_overload(const free_functions::overload_rep& o)
95 std::vector<free_functions::overload_rep>::iterator i = std::find(m_overloads.begin(), m_overloads.end(), o);
97 // if the overload already exists, overwrite the existing function
98 if (i != m_overloads.end())
100 *i = o;
102 else
104 m_overloads.push_back(o);
108 const std::vector<overload_rep>& overloads() const throw() { return m_overloads; }
110 #ifdef LUABIND_DONT_COPY_STRINGS
111 const char* name;
112 #else
113 std::string name;
114 #endif
116 private:
117 // this have to be write protected, since each time an overload is
118 // added it has to be checked for existence. add_overload() should
119 // be used.
120 std::vector<free_functions::overload_rep> m_overloads;
125 // returns generates functions that calls function pointers
127 #define LUABIND_DECL(z, n, text) typedef typename detail::get_policy<n+1,Policies>::type BOOST_PP_CAT(a##n,_policy); \
128 typedef typename BOOST_PP_CAT(a##n,_policy)::head BOOST_PP_CAT(a##n,_param_converter_intermediate); \
129 typedef typename BOOST_PP_CAT(a##n,_param_converter_intermediate)::template generate_converter<A##n, lua_to_cpp>::type BOOST_PP_CAT(p##n,_conv);
131 #define LUABIND_PARAMS(z,n,text) BOOST_PP_CAT(p##n,_conv)::apply(L, LUABIND_DECORATE_TYPE(A##n), n + 1)
134 #define LUABIND_DECL(z, n, text) typedef typename find_conversion_policy<n + 1, Policies>::type BOOST_PP_CAT(converter_policy,n); \
135 typename BOOST_PP_CAT(converter_policy,n)::template generate_converter<A##n, lua_to_cpp>::type BOOST_PP_CAT(c,n);
137 #define LUABIND_ADD_INDEX(z,n,text) + BOOST_PP_CAT(converter_policy,n)::has_arg
138 #define LUABIND_INDEX_MAP(z,n,text) 1 BOOST_PP_REPEAT(n, LUABIND_ADD_INDEX, _)
139 #define LUABIND_PARAMS(z,n,text) BOOST_PP_CAT(c,n).apply(L, LUABIND_DECORATE_TYPE(A##n), LUABIND_INDEX_MAP(_,n,_))
140 #define LUABIND_POSTCALL(z,n,text) BOOST_PP_CAT(c,n).converter_postcall(L, LUABIND_DECORATE_TYPE(A##n), LUABIND_INDEX_MAP(_,n,_));
142 template<class Policies>
143 struct maybe_yield
145 static inline int apply(lua_State* L, int nret)
147 return ret(L, nret, boost::mpl::bool_<has_yield<Policies>::value>());
150 static inline int ret(lua_State* L, int nret, boost::mpl::bool_<true>)
152 return lua_yield(L, nret);
155 static inline int ret(lua_State*, int nret, boost::mpl::bool_<false>)
157 return nret;
162 template<class T>
163 struct returns
165 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 2))
166 #include BOOST_PP_ITERATE()
169 template<>
170 struct returns<void>
172 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 3))
173 #include BOOST_PP_ITERATE()
176 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 4))
177 #include BOOST_PP_ITERATE()
180 #undef LUABIND_PARAMS
181 #undef LUABIND_DECL
182 #undef LUABIND_POSTCALL
183 #undef LUABIND_ADD_INDEX
184 #undef LUABIND_INDEX_MAP
187 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 5))
188 #include BOOST_PP_ITERATE()
193 // TODO: instead of registering all functions here, maybe we should
194 // check the scope where we are registering a new function to see if there's one with
195 // the same name, and in that case, put the new overload in there.
196 struct function_registry
198 std::map<const char*, free_functions::function_rep, ltstr> m_functions;
199 #ifndef LUABIND_DONT_COPY_STRINGS
200 std::vector<char*> m_strings;
202 ~function_registry()
204 for (std::vector<char*>::iterator i = m_strings.begin(); i != m_strings.end(); ++i)
205 delete[] *i;
207 #endif
210 template<class F, class Policies>
211 struct function_callback_s
213 static inline int apply(lua_State* L, void(*fun)())
215 return free_functions::call(reinterpret_cast<F>(fun), L, static_cast<const Policies*>(0));
219 template<class F, class Policies>
220 struct match_function_callback_s
222 static inline int apply(lua_State* L)
224 F fptr = 0;
225 return free_functions::match(fptr, L, static_cast<Policies*>(0));
228 static int callback(lua_State* L)
230 F fptr = 0;
231 return free_functions::match(fptr, L, static_cast<Policies*>(0));
235 int function_dispatcher(lua_State* L);
239 template<class F, class Policies>
240 void function(lua_State* L, const char* name, F f, const Policies&)
242 detail::free_functions::function_registry* registry = 0;
243 lua_pushstring(L, "__lua_free_functions");
244 lua_gettable(L, LUA_REGISTRYINDEX);
246 registry = static_cast<detail::free_functions::function_registry*>(lua_touserdata(L, -1));
247 lua_pop(L, 1);
249 // if you hit this assert you have not called luabind::open() for
250 // this lua_State. See the documentation for more information.
251 assert(registry != 0);
253 #ifdef LUABIND_DONT_COPY_STRINGS
254 detail::free_functions::function_rep& rep = registry->m_functions[name];
255 #else
256 registry->m_strings.push_back(detail::dup_string(name));
257 detail::free_functions::function_rep& rep = registry->m_functions[registry->m_strings.back()];
258 #endif
260 detail::free_functions::overload_rep o(f, static_cast<Policies*>(0));
262 o.set_match_fun(&detail::free_functions::match_function_callback_s<F, Policies>::apply);
263 o.set_fun(&detail::free_functions::function_callback_s<F, Policies>::apply);
265 #ifndef LUABIND_NO_ERROR_CHECKING
266 o.set_sig_fun(&detail::get_free_function_signature<F>::apply);
267 #endif
269 rep.add_overload(o);
270 rep.name = name;
272 lua_pushstring(L, name);
273 lua_pushlightuserdata(L, &rep);
274 lua_pushcclosure(L, detail::free_functions::function_dispatcher, 1);
275 lua_settable(L, LUA_GLOBALSINDEX);
278 template<class F>
279 void function(lua_State* L, const char* name, F f)
281 luabind::function(L, name, f, detail::null_type());
284 namespace detail
286 template<class F, class Policies>
287 struct function_commiter : detail::scoped_object
289 function_commiter(const char* n, F f, const Policies& p)
290 : name(n)
291 , fun(f)
292 , policies(p)
295 virtual detail::scoped_object* clone()
297 return new function_commiter(*this);
300 virtual void commit(lua_State* L)
302 detail::free_functions::function_registry* registry = 0;
303 lua_pushstring(L, "__lua_free_functions");
304 lua_gettable(L, LUA_REGISTRYINDEX);
306 registry = static_cast<detail::free_functions::function_registry*>(lua_touserdata(L, -1));
307 lua_pop(L, 1);
309 // if you hit this assert you have not called luabind::open() for
310 // this lua_State. See the documentation for more information.
311 assert(registry != 0);
313 #ifdef LUABIND_DONT_COPY_STRINGS
314 detail::free_functions::function_rep& rep = registry->m_functions[name.c_str()];
315 #else
316 registry->m_strings.push_back(detail::dup_string(name.c_str()));
317 detail::free_functions::function_rep& rep = registry->m_functions[registry->m_strings.back()];
318 #endif
320 detail::free_functions::overload_rep o(fun, static_cast<Policies*>(0));
322 o.set_match_fun(&detail::free_functions::match_function_callback_s<F, Policies>::apply);
323 o.set_fun(&detail::free_functions::function_callback_s<F, Policies>::apply);
325 #ifndef LUABIND_NO_ERROR_CHECKING
326 o.set_sig_fun(&detail::get_free_function_signature<F>::apply);
327 #endif
329 rep.add_overload(o);
330 rep.name = name.c_str();
332 detail::getref(L, scope_stack::top(L));
333 lua_pushstring(L, name.c_str());
334 lua_pushlightuserdata(L, &rep);
335 lua_pushcclosure(L, free_functions::function_dispatcher, 1);
336 lua_settable(L, -3);
339 std::string name;
340 F fun;
341 Policies policies;
345 template<class F, class Policies>
346 detail::function_commiter<F,Policies>
347 def(const char* name, F f, const Policies& policies)
349 return detail::function_commiter<F,Policies>(name, f, policies);
352 template<class F>
353 detail::function_commiter<F, detail::null_type>
354 def(const char* name, F f)
356 return detail::function_commiter<F,detail::null_type>(name, f, detail::null_type());
359 } // namespace luabind
362 #endif // LUABIND_FUNCTION_HPP_INCLUDED
364 #elif BOOST_PP_ITERATION_FLAGS() == 1
366 // overloaded template funtion that initializes the parameter list
367 // called m_params and the m_arity member.
369 #define LUABIND_INIT_PARAM(z, n, _) m_params[n] = LUABIND_TYPEID(A##n);
370 #define LUABIND_POLICY_DECL(z,n,text) typedef typename find_conversion_policy<n + 1, Policies>::type BOOST_PP_CAT(p,n);
371 #define LUABIND_ARITY(z,n,text) + BOOST_PP_CAT(p,n)::has_arg
373 template<class R BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A), class Policies>
374 overload_rep(R(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), Policies*)
375 : fun(reinterpret_cast<void(*)()>(f))
376 , m_num_args(BOOST_PP_ITERATION())
378 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_INIT_PARAM, _)
379 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_POLICY_DECL, _)
381 m_arity = 0 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_ARITY, _);
384 #undef LUABIND_INIT_PARAM
385 #undef LUABIND_POLICY_DECL
386 #undef LUABIND_ARITY
388 #elif BOOST_PP_ITERATION_FLAGS() == 2
390 template<class Policies BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
391 static int call(T(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, const Policies*)
393 /* typedef typename get_policy<0, Policies>::type ret_policy;
394 typedef typename ret_policy::head return_value_converter_intermediate;
395 typedef typename return_value_converter_intermediate::template generate_converter<T, cpp_to_lua>::type ret_conv;*/
397 int nargs = lua_gettop(L);
399 // typedef typename get_policy_list<0, Policies>::type policy_list_ret;
400 // typedef typename find_converter_policy<policy_list_ret>::type converter_policy_ret;
401 typedef typename find_conversion_policy<0, Policies>::type converter_policy_ret;
402 typename converter_policy_ret::template generate_converter<T, cpp_to_lua>::type converter_ret;
404 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL, _)
405 converter_ret.apply(L, f
407 BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAMS, _)
409 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_POSTCALL, _)
411 int nret = lua_gettop(L) - nargs;
413 const int indices[] =
415 -1 /* self */,
416 nargs + nret /* result*/
417 BOOST_PP_ENUM_TRAILING(BOOST_PP_ITERATION(), LUABIND_INDEX_MAP, _)
420 policy_list_postcall<Policies>::apply(L, indices);
422 return maybe_yield<Policies>::apply(L, nret);
427 #elif BOOST_PP_ITERATION_FLAGS() == 3
429 template<class Policies BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
430 static int call(void(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, const Policies*)
432 int nargs = lua_gettop(L);
434 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL, _)
437 BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAMS, _)
439 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_POSTCALL, _)
441 int nret = lua_gettop(L) - nargs;
443 const int indices[] =
445 -1 /* self */,
446 nargs + nret /* result*/
447 BOOST_PP_ENUM_TRAILING(BOOST_PP_ITERATION(), LUABIND_INDEX_MAP, _)
450 policy_list_postcall<Policies>::apply(L, indices);
452 return maybe_yield<Policies>::apply(L, nret);
456 #elif BOOST_PP_ITERATION_FLAGS() == 4
458 template<class Policies, class R BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
459 int call(R(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, const Policies* policies)
461 return free_functions::returns<R>::call(f, L, policies);
464 #elif BOOST_PP_ITERATION_FLAGS() == 5
466 template<class Policies, class R BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
467 static int match(R(*)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, const Policies* policies)
469 //if (lua_gettop(L) != BOOST_PP_ITERATION()) return -1;
470 typedef constructor<BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)> ParameterTypes;
471 return match_params(L, 1, static_cast<ParameterTypes*>(0), policies);
476 #endif