Hopefully fixed memory leak in detail::free_functions::function_rep.
[luabind.git] / luabind / function.hpp
blob0e73e813acfebf29f298b9251576b6db059a5771
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(*)(), void*);
70 inline void set_fun(call_ptr f) { call_fun = f; }
71 inline int call(lua_State* L, void(*f)(), void* storage) const { return call_fun(L, f, storage); }
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 #ifdef LUABIND_DONT_COPY_STRINGS
94 function_rep(const char* name): m_name(name) {}
95 #else
96 function_rep(const std::string& name): m_name(name) {}
97 #endif
98 void add_overload(const free_functions::overload_rep& o)
100 std::vector<free_functions::overload_rep>::iterator i = std::find(m_overloads.begin(), m_overloads.end(), o);
102 // if the overload already exists, overwrite the existing function
103 if (i != m_overloads.end())
105 *i = o;
107 else
109 m_overloads.push_back(o);
113 const std::vector<overload_rep>& overloads() const throw() { return m_overloads; }
115 #ifdef LUABIND_DONT_COPY_STRINGS
116 const char* name() const { return m_name; }
117 #else
118 const char* name() const { return m_name.c_str(); }
119 #endif
121 private:
123 #ifdef LUABIND_DONT_COPY_STRINGS
124 const char* m_name;
125 #else
126 std::string m_name;
127 #endif
129 // this have to be write protected, since each time an overload is
130 // added it has to be checked for existence. add_overload() should
131 // be used.
132 std::vector<free_functions::overload_rep> m_overloads;
137 // returns generates functions that calls function pointers
139 #define LUABIND_DECL(z, n, text) typedef typename find_conversion_policy<n + 1, Policies>::type BOOST_PP_CAT(converter_policy,n); \
140 typename BOOST_PP_CAT(converter_policy,n)::template generate_converter<A##n, lua_to_cpp>::type BOOST_PP_CAT(c,n);
142 #define LUABIND_ADD_INDEX(z,n,text) + BOOST_PP_CAT(converter_policy,n)::has_arg
143 #define LUABIND_INDEX_MAP(z,n,text) 1 BOOST_PP_REPEAT(n, LUABIND_ADD_INDEX, _)
144 #define LUABIND_PARAMS(z,n,text) BOOST_PP_CAT(c,n).apply(L, LUABIND_DECORATE_TYPE(A##n), LUABIND_INDEX_MAP(_,n,_))
145 #define LUABIND_POSTCALL(z,n,text) BOOST_PP_CAT(c,n).converter_postcall(L, LUABIND_DECORATE_TYPE(A##n), LUABIND_INDEX_MAP(_,n,_));
147 template<class Policies>
148 struct maybe_yield
150 static inline int apply(lua_State* L, int nret)
152 return ret(L, nret, boost::mpl::bool_<has_yield<Policies>::value>());
155 static inline int ret(lua_State* L, int nret, boost::mpl::bool_<true>)
157 return lua_yield(L, nret);
160 static inline int ret(lua_State*, int nret, boost::mpl::bool_<false>)
162 return nret;
167 template<class T>
168 struct returns
170 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 2))
171 #include BOOST_PP_ITERATE()
174 template<>
175 struct returns<void>
177 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 3))
178 #include BOOST_PP_ITERATE()
181 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 4))
182 #include BOOST_PP_ITERATE()
185 #undef LUABIND_PARAMS
186 #undef LUABIND_DECL
187 #undef LUABIND_POSTCALL
188 #undef LUABIND_ADD_INDEX
189 #undef LUABIND_INDEX_MAP
192 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/function.hpp>, 5))
193 #include BOOST_PP_ITERATE()
196 template<class F, class Policies>
197 struct function_callback_s
199 static inline int apply(lua_State* L, void(*fun)(), void* storage)
201 return free_functions::call(reinterpret_cast<F>(fun), L, static_cast<const Policies*>(0), storage);
205 template<class F, class Policies>
206 struct match_function_callback_s
208 static inline int apply(lua_State* L)
210 F fptr = 0;
211 return free_functions::match(fptr, L, static_cast<Policies*>(0));
214 static int callback(lua_State* L)
216 F fptr = 0;
217 return free_functions::match(fptr, L, static_cast<Policies*>(0));
221 LUABIND_API int function_dispatcher(lua_State* L);
225 template<class F, class Policies>
226 void function(lua_State* L, const char* name, F f, const Policies& p)
228 module(L) [ def(name, f, p) ];
231 template<class F>
232 void function(lua_State* L, const char* name, F f)
234 luabind::function(L, name, f, detail::null_type());
237 namespace detail
239 template<class F, class Policies>
240 struct function_commiter : detail::scoped_object
242 function_commiter(const char* n, F f, const Policies& p)
243 : m_name(n)
244 , fun(f)
245 , policies(p)
248 virtual detail::scoped_object* clone()
250 return new function_commiter(*this);
253 virtual void commit(lua_State* L)
255 detail::free_functions::overload_rep o(fun, static_cast<Policies*>(0));
257 o.set_match_fun(&detail::free_functions::match_function_callback_s<F, Policies>::apply);
258 o.set_fun(&detail::free_functions::function_callback_s<F, Policies>::apply);
260 #ifndef LUABIND_NO_ERROR_CHECKING
261 o.set_sig_fun(&detail::get_free_function_signature<F>::apply);
262 #endif
264 detail::getref(L, scope_stack::top(L));
265 lua_pushstring(L, m_name.c_str());
266 lua_gettable(L, -2);
268 detail::free_functions::function_rep* rep = 0;
269 if (lua_iscfunction(L, -1))
271 if (lua_getupvalue(L, -1, 2) != 0)
273 // check the magic number that identifies luabind's functions
274 if (lua_touserdata(L, -1) == (void*)0x1337)
276 if (lua_getupvalue(L, -2, 1) != 0)
278 rep = static_cast<detail::free_functions::function_rep*>(lua_touserdata(L, -1));
279 lua_pop(L, 1);
282 lua_pop(L, 1);
285 lua_pop(L, 1);
287 if (rep == 0)
289 lua_pushstring(L, m_name.c_str());
290 // create a new function_rep
291 rep = static_cast<detail::free_functions::function_rep*>(lua_newuserdata(L, sizeof(detail::free_functions::function_rep)));
292 new(rep) detail::free_functions::function_rep(m_name.c_str());
294 detail::class_registry* r = detail::class_registry::get_registry(L);
295 assert(r && "you must call luabind::open() prior to any function registrations");
296 detail::getref(L, r->lua_function());
297 int ret = lua_setmetatable(L, -2);
298 assert(ret != 0);
300 // this is just a magic number to identify functions that luabind created
301 lua_pushlightuserdata(L, (void*)0x1337);
303 lua_pushcclosure(L, &free_functions::function_dispatcher, 2);
304 lua_settable(L, -3);
307 rep->add_overload(o);
309 lua_pop(L, 1); // pop scope
312 std::string m_name;
313 F fun;
314 Policies policies;
318 template<class F, class Policies>
319 detail::function_commiter<F,Policies>
320 def(const char* name, F f, const Policies& policies)
322 return detail::function_commiter<F,Policies>(name, f, policies);
325 template<class F>
326 detail::function_commiter<F, detail::null_type>
327 def(const char* name, F f)
329 return detail::function_commiter<F,detail::null_type>(name, f, detail::null_type());
332 } // namespace luabind
335 #endif // LUABIND_FUNCTION_HPP_INCLUDED
337 #elif BOOST_PP_ITERATION_FLAGS() == 1
339 // overloaded template funtion that initializes the parameter list
340 // called m_params and the m_arity member.
342 #define LUABIND_INIT_PARAM(z, n, _) m_params[n] = LUABIND_TYPEID(A##n);
343 #define LUABIND_POLICY_DECL(z,n,text) typedef typename find_conversion_policy<n + 1, Policies>::type BOOST_PP_CAT(p,n);
344 #define LUABIND_ARITY(z,n,text) + BOOST_PP_CAT(p,n)::has_arg
346 template<class R BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A), class Policies>
347 overload_rep(R(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), Policies*)
348 : fun(reinterpret_cast<void(*)()>(f))
349 , m_num_args(BOOST_PP_ITERATION())
351 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_INIT_PARAM, _)
352 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_POLICY_DECL, _)
354 m_arity = 0 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_ARITY, _);
357 #undef LUABIND_INIT_PARAM
358 #undef LUABIND_POLICY_DECL
359 #undef LUABIND_ARITY
361 #elif BOOST_PP_ITERATION_FLAGS() == 2
363 template<class Policies BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
364 static int call(T(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, const Policies*, void* storage)
366 /* typedef typename get_policy<0, Policies>::type ret_policy;
367 typedef typename ret_policy::head return_value_converter_intermediate;
368 typedef typename return_value_converter_intermediate::template generate_converter<T, cpp_to_lua>::type ret_conv;*/
370 int nargs = lua_gettop(L);
372 // typedef typename get_policy_list<0, Policies>::type policy_list_ret;
373 // typedef typename find_converter_policy<policy_list_ret>::type converter_policy_ret;
374 typedef typename find_conversion_policy<0, Policies>::type converter_policy_ret;
375 typename converter_policy_ret::template generate_converter<T, cpp_to_lua>::type converter_ret;
377 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL, _)
378 converter_ret.apply(L, f
380 BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAMS, _)
382 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_POSTCALL, _)
384 int nret = lua_gettop(L) - nargs;
386 const int indices[] =
388 -1 /* self */,
389 nargs + nret /* result*/
390 BOOST_PP_ENUM_TRAILING(BOOST_PP_ITERATION(), LUABIND_INDEX_MAP, _)
393 policy_list_postcall<Policies>::apply(L, indices);
395 return maybe_yield<Policies>::apply(L, nret);
400 #elif BOOST_PP_ITERATION_FLAGS() == 3
402 template<class Policies BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
403 static int call(void(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, const Policies*, void* storage)
405 int nargs = lua_gettop(L);
407 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_DECL, _)
410 BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_PARAMS, _)
412 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_POSTCALL, _)
414 int nret = lua_gettop(L) - nargs;
416 const int indices[] =
418 -1 /* self */,
419 nargs + nret /* result*/
420 BOOST_PP_ENUM_TRAILING(BOOST_PP_ITERATION(), LUABIND_INDEX_MAP, _)
423 policy_list_postcall<Policies>::apply(L, indices);
425 return maybe_yield<Policies>::apply(L, nret);
429 #elif BOOST_PP_ITERATION_FLAGS() == 4
431 template<class Policies, class R BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
432 int call(R(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, const Policies* policies, void* storage)
434 return free_functions::returns<R>::call(f, L, policies, storage);
437 #elif BOOST_PP_ITERATION_FLAGS() == 5
439 template<class Policies, class R BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
440 static int match(R(*)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, const Policies* policies)
442 //if (lua_gettop(L) != BOOST_PP_ITERATION()) return -1;
443 typedef constructor<BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)> ParameterTypes;
444 return match_params(L, 1, static_cast<ParameterTypes*>(0), policies);
449 #endif