From ed0b83f8b06fbffea018766433285a955c945dca Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sat, 14 Dec 2013 23:25:44 -0500 Subject: [PATCH] Several changes. I took away operator() for the proxy type, because it'd interfere with the `Callable` type and all. Alas, good things do die I suppose. =[ But! I left it on the `sol::function` type, because it's necessary to discard returns. The .call is still there, though, just incase you need it. <3 reverse_indices_builder is also there, to make sure we can push and pop without the lua api taking our types and breaking them for the std::tuple returns. All is at it should be~ --- sol/function.hpp | 2 +- sol/stack.hpp | 9 +++++++-- sol/table.hpp | 10 ---------- sol/traits.hpp | 6 +++--- sol/tuple.hpp | 23 ++++++++++++++++++++++- tests.cpp | 23 +++++++++++------------ 6 files changed, 44 insertions(+), 29 deletions(-) diff --git a/sol/function.hpp b/sol/function.hpp index bf260ac0..1f60a257 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -68,7 +68,7 @@ public: template typename multi_return::type operator()(types, Args&&... args) { - return call(std::forward(args)...); + return call(std::forward(args)...); } template diff --git a/sol/stack.hpp b/sol/stack.hpp index 617bc605..821645f2 100644 --- a/sol/stack.hpp +++ b/sol/stack.hpp @@ -207,7 +207,7 @@ inline int push_user(lua_State* L, T& item) { namespace detail { template -inline void push(lua_State* L, indices, const T& tuplen) { +inline void push_tuple(lua_State* L, indices, T&& tuplen) { using swallow = char[ 1 + sizeof...(I) ]; swallow {'\0', (sol::stack::push(L, std::get(tuplen)), '\0')... }; } @@ -228,7 +228,12 @@ auto ltr_pop(lua_State* L, F&& f, types, Vs&&... vs) -> decltype( template inline void push(lua_State* L, const std::tuple& tuplen) { - detail::push(L, build_indices(), tuplen); + detail::push_tuple(L, build_reverse_indices(), tuplen); +} + +template +inline void push(lua_State* L, std::tuple&& tuplen) { + detail::push_tuple(L, build_reverse_indices(), std::move(tuplen)); } template diff --git a/sol/table.hpp b/sol/table.hpp index df3155e4..b5c44a79 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -119,16 +119,6 @@ private: return t.get(key); } - template - void operator()( Args&&... args ) { - call<>( std::forward( args )... ); - } - - template - typename multi_return::type operator()(types, Args&&... args) { - return call(std::forward(args)...); - } - template typename multi_return::type call(Args&&... args) { return t.get(key)(types(), std::forward(args)...); diff --git a/sol/traits.hpp b/sol/traits.hpp index fdc70d93..c90a08c6 100644 --- a/sol/traits.hpp +++ b/sol/traits.hpp @@ -63,17 +63,17 @@ struct is_function_impl { }; } // detail -template +template struct multi_return { typedef std::tuple type; }; -template +template struct multi_return { typedef T type; }; -template <> +template<> struct multi_return<> : types<>{ typedef void type; }; diff --git a/sol/tuple.hpp b/sol/tuple.hpp index 5a0ed438..31285b04 100644 --- a/sol/tuple.hpp +++ b/sol/tuple.hpp @@ -26,8 +26,23 @@ #include namespace sol { +template +struct reverse_tuple; + +template<> +struct reverse_tuple> { + using type = std::tuple<>; +}; + +template +struct reverse_tuple> { + using head = std::tuple; + using tail = typename reverse_tuple>::type; + using type = decltype(std::tuple_cat(std::declval(), std::declval())); +}; + template -struct indices {}; +struct indices { typedef indices type; }; template struct build_indices : build_indices {}; @@ -35,6 +50,12 @@ struct build_indices : build_indices {}; template struct build_indices<0, Ns...> : indices {}; +template +struct build_reverse_indices : build_reverse_indices {}; + +template +struct build_reverse_indices<0, Ns...> : indices {}; + template struct types : build_indices {}; diff --git a/tests.cpp b/tests.cpp index 98cfde39..5b020278 100644 --- a/tests.cpp +++ b/tests.cpp @@ -117,34 +117,33 @@ TEST_CASE("advanced/callLambdaReturns", "Checks for lambdas returning values") { sol::state lua; REQUIRE_NOTHROW(lua.set_function("a", [ ] { return 42; })); - REQUIRE(lua["a"]( sol::types() ) == 42); + REQUIRE(lua["a"].call() == 42); REQUIRE_NOTHROW(lua.set_function("b", [ ] { return 42u; })); - REQUIRE(lua["b"](sol::types()) == 42u); + REQUIRE(lua["b"].call() == 42u); REQUIRE_NOTHROW(lua.set_function("c", [ ] { return 3.14; })); - REQUIRE(lua["c"](sol::types()) == 3.14); + REQUIRE(lua["c"].call() == 3.14); REQUIRE_NOTHROW(lua.set_function("d", [ ] { return 6.28f; })); - REQUIRE(lua["d"](sol::types()) == 6.28f); + REQUIRE(lua["d"].call() == 6.28f); REQUIRE_NOTHROW(lua.set_function("e", [ ] { return "lol"; })); - REQUIRE(lua["e"](sol::types()) == lol); + REQUIRE(lua["e"].call() == lol); REQUIRE_NOTHROW(lua.set_function("f", [ ] { return true; })); - REQUIRE(lua["f"](sol::types())); + REQUIRE(lua["f"].call()); REQUIRE_NOTHROW(lua.set_function("g", [ ] { return std::string("str"); })); - REQUIRE(lua["g"](sol::types()) == str); + REQUIRE(lua["g"].call() == str); REQUIRE_NOTHROW(lua.set_function("h", [ ] { })); - REQUIRE_NOTHROW(lua["h"](sol::types<>())); + REQUIRE_NOTHROW(lua["h"].call()); REQUIRE_NOTHROW(lua.set_function("i", [ ] { return sol::nil; })); - REQUIRE(lua["i"](sol::types()) == sol::nil); - - REQUIRE_NOTHROW(lua.set_function("j", [ ] { return std::make_tuple(1, 6.28f, 3.14, std::string( "heh" )); } )); - REQUIRE(lua["j"](sol::types()) == heh_tuple); + REQUIRE(lua["i"].call() == sol::nil); + REQUIRE_NOTHROW(lua.set_function("j", [ ] { return std::make_tuple(1, 6.28f, 3.14, std::string("heh")); })); + REQUIRE((lua["j"].call() == heh_tuple)); } TEST_CASE("advanced/callLambda2", "A C++ lambda is exposed to lua and called") {