From bf2404bdfde393ed079b25f0295b5d8149e0e03b Mon Sep 17 00:00:00 2001 From: ThePhD Date: Mon, 2 Dec 2013 19:15:23 -0500 Subject: [PATCH] More style fixes and more attempts to make sure this compiles between GCC and MSVC. --- sol/function.hpp | 47 ++++++++++++++++----------- sol/functional.hpp | 20 ++++++++++++ sol/lua_function.hpp | 26 +++++++-------- sol/stack.hpp | 6 ++++ sol/state.hpp | 8 ++--- sol/table.hpp | 76 ++++++++++++++++++++++---------------------- 6 files changed, 110 insertions(+), 73 deletions(-) diff --git a/sol/function.hpp b/sol/function.hpp index 676b4d77..da976d04 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -29,20 +29,12 @@ namespace sol { class function : virtual public reference { private: - template - void push_args(Args&&... args) const { - auto L = state(); - using swallow = char[ ]; - void(swallow{ (stack::push(L, std::forward(args)), '\0')... }); - } template struct invoker { template static std::tuple call(const function& ref, Args&&... args) { - ref.push(); - ref.push_args(std::forward(args)...); - lua_pcall(ref.state(), sizeof...(Args), sizeof...(Ret), 0); + lua_pcall( ref.state( ), sizeof...( Args ), sizeof...( Ret ), 0 ); return std::make_tuple(stack::pop(ref.state())...); } }; @@ -51,9 +43,7 @@ private: struct invoker<> { template static void call(const function& ref, Args&&... args) { - ref.push(); - ref.push_args(std::forward(args)...); - lua_pcall(ref.state(), sizeof...(Args), 0, 0); + lua_pcall( ref.state( ), sizeof...( Args ), 0, 0 ); } }; @@ -61,13 +51,32 @@ private: struct invoker { template static T call(const function& ref, Args&&... args) { - ref.push(); - ref.push_args(std::forward(args)...); - lua_pcall(ref.state(), sizeof...(Args), 1, 0); - return stack::pop(ref.state()); + lua_pcall( ref.state( ), sizeof...( Args ), 1, 0 ); + return stack::pop( ref.state( ) ); + } }; + template + std::tuple invoke( types, std::size_t n ) { + lua_pcall( state( ), n, sizeof...( Ret ), 0 ); + return stack::pop_call( state( ), std::make_tuple, types() ); + } + + template + Ret invoke( types, std::size_t n ) { + lua_pcall( state( ), n, 1, 0 ); + return stack::pop( state( ) ); + } + + void invoke( types, std::size_t n ) { + lua_pcall( state( ), n, 0, 0 ); + } + + void invoke( types<>, std::size_t n ) { + lua_pcall( state( ), n, 0, 0 ); + } + public: function() : reference() {} function(lua_State* L, int index = -1) : reference(L, index) { @@ -75,8 +84,10 @@ public: } template - auto invoke(Args&&... args) { - return invoker::call(*this, std::forward(args)...); + auto invoke(Args&&... args) -> decltype(invoke( types( ), sizeof...( Args ) )) { + push( ); + stack::push_args( state( ), std::forward( args )... ); + return invoke( types( ), sizeof...( Args ) ); } }; } // sol diff --git a/sol/functional.hpp b/sol/functional.hpp index f16fc528..3fe6ae18 100644 --- a/sol/functional.hpp +++ b/sol/functional.hpp @@ -91,6 +91,26 @@ inline auto call(Function&& f, const Tuple& t, indices) -> decltype( } // detail +template +struct lua_return_type { + typedef std::tuple type; +}; + +template +struct lua_return_type { + typedef Ret type; +}; + +template <> +struct lua_return_type<> { + typedef void type; +}; + +template <> +struct lua_return_type { + typedef void type; +}; + template inline auto call(Function&& f, const std::tuple& t) -> decltype(detail::call(std::forward(f), t, build_indices{})) { return call(std::forward(f), t, build_indices{}); diff --git a/sol/lua_function.hpp b/sol/lua_function.hpp index af9a2cab..f295030e 100644 --- a/sol/lua_function.hpp +++ b/sol/lua_function.hpp @@ -29,7 +29,7 @@ namespace sol { namespace detail { struct lua_func { - virtual int operator () (lua_State* L) { + virtual int operator () (lua_State*) { throw sol_error("Failure to call specialized wrapped C++ function from lua"); } @@ -48,7 +48,7 @@ struct lambda_lua_func : public lua_func { } virtual int operator () (lua_State* L) override { - return (*this)(tuple_types(), fx_traits::args_type(), L); + return ( *this )( tuple_types( ), typename fx_traits::args_type( ), L ); } template @@ -77,7 +77,7 @@ struct explicit_lua_func : public lua_func { } virtual int operator () (lua_State* L) override { - return (*this)(tuple_types(), fx_traits::args_type(), L); + return ( *this )( tuple_types( ), typename fx_traits::args_type( ), L ); } template @@ -99,18 +99,18 @@ struct explicit_lua_func : public lua_func { typedef typename std::remove_pointer::type>::type fx_t; typedef function_traits fx_traits; struct lambda { - T* member; - TFx invocation; + T* member; + TFx invocation; - template - lambda(T* m, FxArgs&&... fxargs) : member(m), invocation(std::forward(fxargs)...) { + template + lambda(T* m, FxArgs&&... fxargs) : member(m), invocation(std::forward(fxargs)...) { - } + } - template - typename fx_traits::return_type operator () (Args&&... args) { - return ((*member).*invocation)(std::forward(args)...); - } + template + typename fx_traits::return_type operator () (Args&&... args) { + return ((*member).*invocation)(std::forward(args)...); + } } fx; template @@ -124,7 +124,7 @@ struct explicit_lua_func : public lua_func { } virtual int operator () (lua_State* L) override { - return (*this)(tuple_types(), fx_traits::args_type(), L); + return ( *this )( tuple_types( ), typename fx_traits::args_type( ), L ); } template diff --git a/sol/stack.hpp b/sol/stack.hpp index 02c302bd..a1da5ca2 100644 --- a/sol/stack.hpp +++ b/sol/stack.hpp @@ -183,6 +183,12 @@ inline auto pop_call(lua_State* L, TFx&& fx, types t)->decltype(detail: return detail::ltr_pop(L, fx, t); } +template +void push_args( lua_State* L, Args&&... args ) { + using swallow = char []; + void( swallow{ '\0', ( stack::push( L, std::forward( args ) ), '\0' )... } ); +} + } // stack } // sol diff --git a/sol/state.hpp b/sol/state.hpp index fc56f4c4..f49bf499 100644 --- a/sol/state.hpp +++ b/sol/state.hpp @@ -149,14 +149,14 @@ public: template state& set_function(T&& key, TFx&& fx) { - global.set_function(std::forward(key), std::forward(fx)); - return *this; + global.set_function(std::forward(key), std::forward(fx)); + return *this; } template state& set_function(T&& key, TFx&& fx, TM& mem) { - global.set_function(std::forward(key), std::forward(fx), mem); - return *this; + global.set_function(std::forward(key), std::forward(fx), mem); + return *this; } template diff --git a/sol/table.hpp b/sol/table.hpp index 81525579..9b9d6ab2 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -60,17 +60,17 @@ public: template table& set_function(T&& key, TFx&& fx) { - typedef typename std::remove_pointer::type>::type clean_fx; - const static bool isfunction = std::is_function::value; - return set_fx(std::integral_constant(), - std::forward(key), std::forward(fx)); + typedef typename std::remove_pointer::type>::type clean_fx; + const static bool isfunction = std::is_function::value; + return set_fx(std::integral_constant(), + std::forward(key), std::forward(fx)); } template table& set_function(T&& key, TFx&& fx, TM& mem) { - typedef typename std::remove_pointer::type>::type clean_fx; - std::unique_ptr sptr(new detail::explicit_lua_func(mem, std::forward(fx))); - return set_fx(std::forward(key), std::move(sptr)); + typedef typename std::remove_pointer::type>::type clean_fx; + std::unique_ptr sptr(new detail::explicit_lua_func(mem, std::forward(fx))); + return set_fx(std::forward(key), std::move(sptr)); } size_t size() const { @@ -80,39 +80,39 @@ public: private: - template - table& set_fx(std::true_type, T&& key, TFx&& fx) { - typedef typename std::remove_pointer::type>::type clean_fx; - std::unique_ptr sptr(new detail::lambda_lua_func(std::forward(fx))); - return set_fx(std::forward(key), std::move(sptr)); - } + template + table& set_fx(std::true_type, T&& key, TFx&& fx) { + typedef typename std::remove_pointer::type>::type clean_fx; + std::unique_ptr sptr(new detail::lambda_lua_func(std::forward(fx))); + return set_fx(std::forward(key), std::move(sptr)); + } - template - table& set_fx(std::false_type, T&& key, TFx&& fx) { - typedef typename std::remove_pointer::type>::type clean_fx; - typedef typename std::decay::type ptr_fx; - std::unique_ptr sptr(new detail::explicit_lua_func(std::forward(fx))); - return set_fx(std::forward(key), std::move(sptr)); - } + template + table& set_fx(std::false_type, T&& key, TFx&& fx) { + typedef typename std::remove_pointer::type>::type clean_fx; + typedef typename std::decay::type ptr_fx; + std::unique_ptr sptr(new detail::explicit_lua_func(std::forward(fx))); + return set_fx(std::forward(key), std::move(sptr)); + } - template - table& set_fx(T&& key, std::unique_ptr funcptr) { - std::string fkey(key); - auto hint = funcs.find(fkey); - if (hint == funcs.end()) { - std::shared_ptr sptr(funcptr.release()); - hint = funcs.emplace_hint(hint, fkey, std::move(sptr)); - } - else { - hint->second.reset(funcptr.release()); - } - detail::lua_func* target = target = hint->second.get(); - lua_CFunction freefunc = &detail::lua_cfun; - lua_pushlightuserdata(state(), static_cast(target)); - lua_pushcclosure(state(), freefunc, 1); - lua_setglobal(state(), fkey.c_str()); - return *this; - } + template + table& set_fx(T&& key, std::unique_ptr funcptr) { + std::string fkey(key); + auto hint = funcs.find(fkey); + if (hint == funcs.end()) { + std::shared_ptr sptr(funcptr.release()); + hint = funcs.emplace_hint(hint, fkey, std::move(sptr)); + } + else { + hint->second.reset(funcptr.release()); + } + detail::lua_func* target = hint->second.get(); + lua_CFunction freefunc = &detail::lua_cfun; + lua_pushlightuserdata(state(), static_cast(target)); + lua_pushcclosure(state(), freefunc, 1); + lua_setglobal(state(), fkey.c_str()); + return *this; + } }; } // sol