diff --git a/sol/lua_function.hpp b/sol/lua_function.hpp index f2ddae0b..a294db0c 100644 --- a/sol/lua_function.hpp +++ b/sol/lua_function.hpp @@ -175,7 +175,7 @@ template struct functor_lua_func : public lua_func { typedef decltype(&TFx::operator()) fx_t; typedef function_traits fx_traits; - TFx fx; + fx_t fx; template functor_lua_func(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} @@ -208,7 +208,7 @@ template::type>::type fx_t; typedef function_traits fx_traits; - TFx fx; + fx_t fx; template function_lua_func(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} @@ -241,12 +241,12 @@ template struct function_lua_func : public lua_func { typedef typename std::remove_pointer::type>::type fx_t; typedef function_traits fx_traits; - struct lambda { + struct functor { T member; - TFx invocation; + fx_t invocation; template - lambda(T m, FxArgs&&... fxargs): member(std::move(m)), invocation(std::forward(fxargs)...) {} + functor(T m, FxArgs&&... fxargs): member(std::move(m)), invocation(std::forward(fxargs)...) {} template typename fx_traits::return_type operator()(Args&&... args) { @@ -281,6 +281,58 @@ struct function_lua_func : public lua_func { } }; +template +struct class_lua_func : public lua_func { + typedef typename std::remove_pointer::type>::type fx_t; + typedef function_traits fx_traits; + struct functor { + T& member; + fx_t invocation; + + template + functor(FxArgs&&... fxargs): member(*static_cast(nullptr)), invocation(std::forward(fxargs)...) {} + + void pre_call( lua_State* L ) { + void* userdata = lua_touserdata( L, 0 ); + T* item = static_cast( userdata ); + member = *item; + } + + template + typename fx_traits::return_type operator()(Args&&... args) { + 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) { + fx.pre_call(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; + fx.pre_call(L); + 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 fx_traits::args_type(), L); + } +}; + } // sol #endif // SOL_LUA_FUNC_HPP