// The MIT License (MIT) // Copyright (c) 2013 Danny Y., Rapptz // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #ifndef SOL_LUA_FUNC_HPP #define SOL_LUA_FUNC_HPP #include "stack.hpp" #include namespace sol { template struct static_lua_func { typedef typename std::remove_pointer::type>::type function_type; typedef function_traits traits_type; template static int typed_call(types, types t, function_type* fx, lua_State* L) { stack::pop_call(L, fx, t); return 0; } template static int typed_call(types<>, types t, function_type* fx, lua_State* L) { return typed_call(types(), t, fx, L); } template static int typed_call(types, types t, function_type* fx, lua_State* L) { typedef typename multi_return::type return_type; return_type r = stack::pop_call(L, fx, t); stack::push(L, std::move(r)); return sizeof...(Ret); } static int call(lua_State* L) { auto udata = stack::get_user(L); function_type* fx = udata.first; int r = typed_call(tuple_types(), typename traits_type::args_type(), fx, L); return r; } int operator()(lua_State* L) { return call(L); } }; template struct static_object_lua_func { typedef typename std::remove_pointer::type>::type function_type; typedef function_traits traits_type; template static int typed_call(types, types, T& item, function_type& ifx, lua_State* L) { auto fx = [&item, &ifx](Args&&... args) -> void { (item.*ifx)(std::forward(args)...); }; stack::pop_call(L, fx, types()); return 0; } template static int typed_call(types<>, types t, T& item, function_type& ifx, lua_State* L) { return typed_call(types(), t, item, ifx, L); } template static int typed_call(types, types, T& item, function_type& ifx, lua_State* L) { typedef typename multi_return::type return_type; auto fx = [&item, &ifx](Args&&... args) -> return_type { return (item.*ifx)(std::forward(args)...); }; return_type r = stack::pop_call(L, fx, types()); stack::push(L, std::move(r)); return sizeof...(Ret); } static int call(lua_State* L) { auto memberdata = stack::get_user(L, 1); auto objdata = stack::get_user(L, memberdata.second); function_type& memfx = memberdata.first; T& obj = *objdata.first; int r = typed_call(tuple_types(), typename traits_type::args_type(), obj, memfx, L); return r; } int operator()(lua_State* L) { return call(L); } }; struct lua_func { static int call(lua_State* L) { void** pinheritancedata = static_cast(stack::get(L, 1).value); void* inheritancedata = *pinheritancedata; if (inheritancedata == nullptr) { throw sol_error("call from Lua to C++ function has null data"); } lua_func* pfx = static_cast(inheritancedata); lua_func& fx = *pfx; int r = fx(L); return r; } static int gc(lua_State* L) { void** puserdata = static_cast(stack::get(L, 1).value); void* userdata = *puserdata; lua_func* ptr = static_cast(userdata); std::default_delete dx{}; dx(ptr); return 0; } virtual int operator()(lua_State*) { throw sol_error("failure to call specialized wrapped C++ function from Lua"); } virtual ~lua_func() {} }; template struct functor_lua_func : public lua_func { typedef decltype(&Function::operator()) function_type; typedef function_traits traits_type; Function fx; template functor_lua_func(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} template int operator()(types, types t, lua_State* L) { stack::pop_call(L, fx, t); return 0; } template int operator()(types<>, types t, lua_State* L) { return (*this)(types(), t, L); } template int operator()(types, types t, lua_State* L) { typedef typename multi_return::type return_type; return_type r = stack::pop_call(L, fx, t); stack::push(L, r); return sizeof...(Ret); } virtual int operator()(lua_State* L) override { return (*this)(tuple_types(), typename traits_type::args_type(), L); } }; template::value> struct function_lua_func : public lua_func { typedef typename std::remove_pointer::type>::type function_type; typedef function_traits traits_type; function_type fx; template function_lua_func(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} template int operator()(types, types t, lua_State* L) { stack::pop_call(L, fx, t); return 0; } template int operator()(types<>, types t, lua_State* L) { return (*this)(types(), t, L); } template int operator()(types, types t, lua_State* L) { typedef typename multi_return::type return_type; return_type r = stack::pop_call(L, fx, t); stack::push(L, std::move(r)); return sizeof...(Ret); } virtual int operator()(lua_State* L) override { return (*this)(tuple_types(), typename traits_type::args_type(), L); } }; template struct function_lua_func : public lua_func { typedef typename std::remove_pointer::type>::type function_type; typedef function_traits traits_type; struct functor { T member; function_type invocation; template functor(T m, FxArgs&&... fxargs): member(std::move(m)), invocation(std::forward(fxargs)...) {} template typename traits_type::return_type operator()(Args&&... args) { return (member.*invocation)(std::forward(args)...); } } fx; template function_lua_func(T m, FxArgs&&... fxargs): fx(std::move(m), std::forward(fxargs)...) {} template int operator()(types, types t, lua_State* L) { stack::pop_call(L, fx, t); return 0; } template int operator()(types<>, types t, lua_State* L) { return (*this)(types(), t, L); } template int operator()(types, types t, lua_State* L) { typedef typename multi_return::type return_type; return_type r = stack::pop_call(L, fx, t); stack::push(L, std::move(r)); return sizeof...(Ret); } virtual int operator()(lua_State* L) override { return (*this)(tuple_types(), typename traits_type::args_type(), L); } }; template struct class_lua_func : public lua_func { typedef typename std::remove_pointer::type>::type function_type; typedef function_traits traits_type; struct functor { T* item; function_type invocation; template functor(FxArgs&&... fxargs): item(nullptr), invocation(std::forward(fxargs)...) {} void pre_call(lua_State* L) { void* userdata = lua_touserdata(L, 0); item = static_cast(userdata); } template typename traits_type::return_type operator()(Args&&... args) { T& member = *item; return (member.*invocation)(std::forward(args)...); } } fx; template class_lua_func(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} template int operator()(types, types t, lua_State* L) { stack::pop_call(L, fx, t); return 0; } template int operator()(types<>, types t, lua_State* L) { return (*this)(types(), t, L); } template int operator()(types, types t, lua_State* L) { typedef typename multi_return::type return_type; return_type r = stack::pop_call(L, fx, t); stack::push(L, std::move(r)); return sizeof...(Ret); } virtual int operator()(lua_State* L) override { fx.pre_call(L); return (*this)(tuple_types(), typename traits_type::args_type(), L); } }; } // sol #endif // SOL_LUA_FUNC_HPP