Made it work on VC6.5 again. This involves changing generate_converter
[luabind.git] / luabind / function.hpp
blob2c284084c6a5f352dcb6c353e6bbb5bb6d354486
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/prefix.hpp>
30 #include <luabind/config.hpp>
32 #include <boost/config.hpp>
33 #include <boost/preprocessor/repeat.hpp>
34 #include <boost/preprocessor/iteration/iterate.hpp>
35 #include <boost/preprocessor/repetition/enum.hpp>
36 #include <boost/preprocessor/repetition/enum_params.hpp>
37 #include <boost/preprocessor/cat.hpp>
38 #include <boost/mpl/apply_wrap.hpp>
40 #include <luabind/detail/signature_match.hpp>
41 #include <luabind/detail/call_function.hpp>
42 #include <luabind/detail/get_overload_signature.hpp>
43 #include <luabind/detail/overload_rep_base.hpp>
45 #include <luabind/scope.hpp>
47 namespace luabind
49 namespace detail
52 namespace mpl = boost::mpl;
54 namespace free_functions
57 struct overload_rep: public overload_rep_base
60 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 1))
61 #include BOOST_PP_ITERATE()
63 inline bool operator==(const overload_rep& o) const
65 if (o.m_arity != m_arity) return false;
66 if (o.m_params_.size() != m_params_.size()) return false;
67 for (int i = 0; i < (int)m_params_.size(); ++i)
68 if (!(LUABIND_TYPE_INFO_EQUAL(m_params_[i], o.m_params_[i]))) return false;
69 return true;
72 typedef int(*call_ptr)(lua_State*, void(*)());
74 inline void set_fun(call_ptr f) { call_fun = f; }
75 inline int call(lua_State* L, void(*f)()) const { return call_fun(L, f); }
77 // this is the actual function pointer to be called when this overload is invoked
78 void (*fun)();
80 //TODO: private:
82 call_ptr call_fun;
84 // the types of the parameter it takes
85 std::vector<LUABIND_TYPE_INFO> m_params_;
87 char end;
90 struct function_rep
92 function_rep(const char* name): m_name(name) {}
93 void add_overload(const free_functions::overload_rep& o);
95 const std::vector<overload_rep>& overloads() const throw() { return m_overloads; }
97 const char* name() const { return m_name; }
99 private:
100 const char* m_name;
102 // this have to be write protected, since each time an overload is
103 // added it has to be checked for existence. add_overload() should
104 // be used.
105 std::vector<free_functions::overload_rep> m_overloads;
110 // returns generates functions that calls function pointers
112 #define LUABIND_DECL(z, n, text) typedef typename find_conversion_policy<n + 1, Policies>::type BOOST_PP_CAT(converter_policy,n); \
113 typename mpl::apply_wrap2< \
114 BOOST_PP_CAT(converter_policy,n), BOOST_PP_CAT(A,n), lua_to_cpp \
115 >::type BOOST_PP_CAT(c,n);
117 #define LUABIND_ADD_INDEX(z,n,text) + BOOST_PP_CAT(converter_policy,n)::has_arg
118 #define LUABIND_INDEX_MAP(z,n,text) 1 BOOST_PP_REPEAT(n, LUABIND_ADD_INDEX, _)
119 #define LUABIND_PARAMS(z,n,text) BOOST_PP_CAT(c,n).apply(L, LUABIND_DECORATE_TYPE(A##n), LUABIND_INDEX_MAP(_,n,_))
120 #define LUABIND_POSTCALL(z,n,text) BOOST_PP_CAT(c,n).converter_postcall(L, LUABIND_DECORATE_TYPE(A##n), LUABIND_INDEX_MAP(_,n,_));
122 template<class Policies>
123 struct maybe_yield
125 static inline int apply(lua_State* L, int nret)
127 return ret(L, nret, boost::mpl::bool_<has_yield<Policies>::value>());
130 static inline int ret(lua_State* L, int nret, boost::mpl::bool_<true>)
132 return lua_yield(L, nret);
135 static inline int ret(lua_State*, int nret, boost::mpl::bool_<false>)
137 return nret;
142 template<class T>
143 struct returns
145 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 2))
146 #include BOOST_PP_ITERATE()
149 template<>
150 struct returns<void>
152 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 3))
153 #include BOOST_PP_ITERATE()
156 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 4))
157 #include BOOST_PP_ITERATE()
160 #undef LUABIND_PARAMS
161 #undef LUABIND_DECL
162 #undef LUABIND_POSTCALL
163 #undef LUABIND_ADD_INDEX
164 #undef LUABIND_INDEX_MAP
167 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 5))
168 #include BOOST_PP_ITERATE()
171 template<class F, class Policies>
172 struct function_callback_s
174 static inline int apply(lua_State* L, void(*fun)())
176 return free_functions::call(reinterpret_cast<F>(fun), L, static_cast<const Policies*>(0));
180 template<class F, class Policies>
181 struct match_function_callback_s
183 static inline int apply(lua_State* L)
185 F fptr = 0;
186 return free_functions::match(fptr, L, static_cast<Policies*>(0));
189 static int callback(lua_State* L)
191 F fptr = 0;
192 return free_functions::match(fptr, L, static_cast<Policies*>(0));
196 LUABIND_API int function_dispatcher(lua_State* L);
200 // deprecated
201 template<class F, class Policies>
202 void function(lua_State* L, const char* name, F f, const Policies& p)
204 module(L) [ def(name, f, p) ];
207 // deprecated
208 template<class F>
209 void function(lua_State* L, const char* name, F f)
211 luabind::function(L, name, f, detail::null_type());
214 namespace detail
216 template<class F, class Policies>
217 struct function_commiter : detail::registration
219 function_commiter(const char* n, F f, const Policies& p)
220 : m_name(n)
221 , fun(f)
222 , policies(p)
225 virtual void register_(lua_State* L) const
227 detail::free_functions::overload_rep o(fun, static_cast<Policies*>(0));
229 o.set_match_fun(&detail::free_functions::match_function_callback_s<F, Policies>::apply);
230 o.set_fun(&detail::free_functions::function_callback_s<F, Policies>::apply);
232 #ifndef LUABIND_NO_ERROR_CHECKING
233 o.set_sig_fun(&detail::get_free_function_signature<F>::apply);
234 #endif
236 lua_pushstring(L, m_name);
237 lua_gettable(L, -2);
239 detail::free_functions::function_rep* rep = 0;
240 if (lua_iscfunction(L, -1))
242 if (lua_getupvalue(L, -1, 2) != 0)
244 // check the magic number that identifies luabind's functions
245 if (lua_touserdata(L, -1) == (void*)0x1337)
247 if (lua_getupvalue(L, -2, 1) != 0)
249 rep = static_cast<detail::free_functions::function_rep*>(lua_touserdata(L, -1));
250 lua_pop(L, 1);
253 lua_pop(L, 1);
256 lua_pop(L, 1);
258 if (rep == 0)
260 lua_pushstring(L, m_name);
261 // create a new function_rep
262 rep = static_cast<detail::free_functions::function_rep*>(lua_newuserdata(L, sizeof(detail::free_functions::function_rep)));
263 new(rep) detail::free_functions::function_rep(m_name);
265 // STORE IN REGISTRY
266 lua_pushvalue(L, -1);
267 detail::ref(L);
269 detail::class_registry* r = detail::class_registry::get_registry(L);
270 assert(r && "you must call luabind::open() prior to any function registrations");
271 detail::getref(L, r->lua_function());
272 int ret = lua_setmetatable(L, -2);
273 (void)ret;
274 assert(ret != 0);
276 // this is just a magic number to identify functions that luabind created
277 lua_pushlightuserdata(L, (void*)0x1337);
279 lua_pushcclosure(L, &free_functions::function_dispatcher, 2);
280 lua_settable(L, -3);
283 rep->add_overload(o);
286 char const* m_name;
287 F fun;
288 Policies policies;
292 template<class F, class Policies>
293 scope def(const char* name, F f, const Policies& policies)
295 return scope(std::auto_ptr<detail::registration>(
296 new detail::function_commiter<F,Policies>(name, f, policies)));
299 template<class F>
300 scope def(const char* name, F f)
302 return scope(std::auto_ptr<detail::registration>(
303 new detail::function_commiter<F,detail::null_type>(
304 name, f, detail::null_type())));
307 } // namespace luabind
310 #endif // LUABIND_FUNCTION_HPP_INCLUDED
312 #elif BOOST_PP_ITERATION_FLAGS() == 1
314 // overloaded template funtion that initializes the parameter list
315 // called m_params and the m_arity member.
317 #define LUABIND_INIT_PARAM(z, n, _) m_params_.push_back(LUABIND_TYPEID(A##n));
318 #define LUABIND_POLICY_DECL(z,n,text) typedef typename find_conversion_policy<n + 1, Policies>::type BOOST_PP_CAT(p,n);
319 #define LUABIND_ARITY(z,n,text) + BOOST_PP_CAT(p,n)::has_arg
321 template<class R BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A), class Policies>
322 overload_rep(R(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), Policies*)
323 : fun(reinterpret_cast<void(*)()>(f))
325 m_params_.reserve(BOOST_PP_ITERATION());
326 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_INIT_PARAM, _)
327 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_POLICY_DECL, _)
329 m_arity = 0 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_ARITY, _);
332 #undef LUABIND_INIT_PARAM
333 #undef LUABIND_POLICY_DECL
334 #undef LUABIND_ARITY
336 #elif BOOST_PP_ITERATION_FLAGS() == 2
338 template<class Policies BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
339 static int call(T(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, const Policies*)
341 int nargs = lua_gettop(L);
343 typedef typename find_conversion_policy<0, Policies>::type converter_policy_ret;
344 typename mpl::apply_wrap2<converter_policy_ret,T,cpp_to_lua>::type converter_ret;
346 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL, _)
347 converter_ret.apply(L, f
349 BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAMS, _)
351 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_POSTCALL, _)
353 int nret = lua_gettop(L) - nargs;
355 const int indices[] =
357 -1 /* self */,
358 nargs + nret /* result*/
359 BOOST_PP_ENUM_TRAILING(BOOST_PP_ITERATION(), LUABIND_INDEX_MAP, _)
362 policy_list_postcall<Policies>::apply(L, indices);
364 return maybe_yield<Policies>::apply(L, nret);
369 #elif BOOST_PP_ITERATION_FLAGS() == 3
371 template<class Policies BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
372 static int call(void(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, const Policies*)
374 int nargs = lua_gettop(L);
376 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL, _)
379 BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAMS, _)
381 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_POSTCALL, _)
383 int nret = lua_gettop(L) - nargs;
385 const int indices[] =
387 -1 /* self */,
388 nargs + nret /* result*/
389 BOOST_PP_ENUM_TRAILING(BOOST_PP_ITERATION(), LUABIND_INDEX_MAP, _)
392 policy_list_postcall<Policies>::apply(L, indices);
394 return maybe_yield<Policies>::apply(L, nret);
398 #elif BOOST_PP_ITERATION_FLAGS() == 4
400 template<class Policies, class R BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
401 int call(R(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, const Policies* policies)
403 return free_functions::returns<R>::call(f, L, policies);
406 #elif BOOST_PP_ITERATION_FLAGS() == 5
408 template<class Policies, class R BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
409 static int match(R(*)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, const Policies* policies)
411 //if (lua_gettop(L) != BOOST_PP_ITERATION()) return -1;
412 typedef constructor<BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)> ParameterTypes;
413 return match_params(L, 1, static_cast<ParameterTypes*>(0), policies);
418 #endif