From 4dab754b86676f1ea323a9af5619489f0ec81e61 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 13 Dec 2013 17:50:24 -0500 Subject: [PATCH 1/6] VC++ Compatibility and some changes to function I'll need to test. --- sol/function.hpp | 10 ++++++++++ sol/table.hpp | 8 ++++---- tests.cpp | 1 + 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/sol/function.hpp b/sol/function.hpp index 264fc0e9..c6f47d89 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -61,6 +61,16 @@ public: function(const function&) = default; function& operator=(const function&) = default; + template + void operator()( Args&&... args ) { + call<>(std::forward(args)... ); + } + + template + auto operator()( types, Args&&... args ) { + return call(std::forward(args)... ); + } + template auto call(Args&&... args) -> decltype(invoke(types(), sizeof...(Args))) { push(); diff --git a/sol/table.hpp b/sol/table.hpp index 4a444e52..d3de9c48 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -82,13 +82,13 @@ public: } template - auto operator[](T&& key) -> decltype(proxy(*this, std::forward(key))) { - return proxy(*this, std::forward(key)); + proxy operator[](T&& key) { + return proxy(*this, std::forward(key)); } template - auto operator[](T&& key) const -> decltype(proxy(*this, std::forward(key))) { - return proxy(*this, std::forward(key)); + proxy operator[](T&& key) const { + return proxy(*this, std::forward(key)); } size_t size() const { diff --git a/tests.cpp b/tests.cpp index a24e92a4..6e7fdc6e 100644 --- a/tests.cpp +++ b/tests.cpp @@ -120,6 +120,7 @@ TEST_CASE("advanced/callLambdaReturns", "Checks for lambdas returning values") { lua.set_function("g", [ ] { return std::string("str"); }); lua.set_function("h", [ ] { }); lua.set_function("i", [ ] { return sol::nil; }); + //lua.set_function("i", [ ] { return std::make_tuple(1, 6.28f, 3.14, std::string("heh")); }); } TEST_CASE("advanced/callLambda2", "A C++ lambda is exposed to lua and called") { From 9624dd93e774801b49a8c9070f1b1d5841c0aeec Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 13 Dec 2013 20:09:51 -0500 Subject: [PATCH 2/6] VC++ Compat again. --- sol/function.hpp | 8 ++++---- sol/table.hpp | 2 +- sol/traits.hpp | 36 ++++++++++++++++++------------------ tests.cpp | 7 +++++-- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/sol/function.hpp b/sol/function.hpp index c6f47d89..f532962e 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -62,13 +62,13 @@ public: function& operator=(const function&) = default; template - void operator()( Args&&... args ) { - call<>(std::forward(args)... ); + void operator()(Args&&... args) { + call<>(std::forward(args)...); } template - auto operator()( types, Args&&... args ) { - return call(std::forward(args)... ); + auto operator()(types, Args&&... args) { + return call(std::forward(args)...); } template diff --git a/sol/table.hpp b/sol/table.hpp index d3de9c48..774700a1 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -41,7 +41,7 @@ T* get_ptr(T* val) { } // detail class table : virtual public reference { - template struct proxy; + template struct proxy; public: table() noexcept : reference() {} table(lua_State* L, int index = -1) : reference(L, index) { diff --git a/sol/traits.hpp b/sol/traits.hpp index 9b998305..26d71634 100644 --- a/sol/traits.hpp +++ b/sol/traits.hpp @@ -72,56 +72,56 @@ template struct function_traits; template -struct function_traits { - static const std::size_t arity = sizeof...( Args ); +struct function_traits { + static const std::size_t arity = sizeof...(Args); static const bool is_member_function = true; typedef std::tuple arg_tuple_type; typedef types args_type; - typedef R( T::* function_pointer_type )( Args... ); + typedef R(T::* function_pointer_type)(Args...); typedef typename std::remove_pointer::type function_type; - typedef R( *free_function_pointer_type )( Args... ); + typedef R(*free_function_pointer_type)(Args...); typedef R return_type; template using arg = typename std::tuple_element::type; }; template -struct function_traits { - static const std::size_t arity = sizeof...( Args ); +struct function_traits { + static const std::size_t arity = sizeof...(Args); static const bool is_member_function = true; typedef std::tuple arg_tuple_type; typedef types args_type; - typedef R( T::* function_pointer_type )( Args... ); + typedef R(T::* function_pointer_type)(Args...); typedef typename std::remove_pointer::type function_type; - typedef R( *free_function_pointer_type )( Args... ); + typedef R(*free_function_pointer_type)(Args...); typedef R return_type; template using arg = typename std::tuple_element::type; }; template -struct function_traits { - static const std::size_t arity = sizeof...( Args ); +struct function_traits { + static const std::size_t arity = sizeof...(Args); static const bool is_member_function = false; typedef std::tuple arg_tuple_type; typedef types args_type; - typedef R( function_type )( Args... ); - typedef R( *function_pointer_type )( Args... ); - typedef R( *free_function_pointer_type )( Args... ); + typedef R(function_type)(Args...); + typedef R(*function_pointer_type)(Args...); + typedef R(*free_function_pointer_type)(Args...); typedef R return_type; template using arg = typename std::tuple_element::type; }; template -struct function_traits { - static const std::size_t arity = sizeof...( Args ); +struct function_traits { + static const std::size_t arity = sizeof...(Args); static const bool is_member_function = false; typedef std::tuple arg_tuple_type; typedef types args_type; - typedef R( function_type )( Args... ); - typedef R( *function_pointer_type )( Args... ); - typedef R( *free_function_pointer_type )( Args... ); + typedef R(function_type)(Args...); + typedef R(*function_pointer_type)(Args...); + typedef R(*free_function_pointer_type)(Args...); typedef R return_type; template using arg = typename std::tuple_element::type; diff --git a/tests.cpp b/tests.cpp index 6e7fdc6e..7dc3debe 100644 --- a/tests.cpp +++ b/tests.cpp @@ -81,7 +81,10 @@ TEST_CASE("simple/callWithParameters", "Lua function is called with a few parame REQUIRE_NOTHROW(lua.script("function my_add(i, j, k) return i + j + k end")); auto f = lua.get("my_add"); + REQUIRE_NOTHROW(lua.script("function my_nothing(i, j, k) end")); + auto fvoid = lua.get("my_nothing"); int a; + REQUIRE_NOTHROW(fvoid(1, 2, 3)); REQUIRE_NOTHROW(a = f.call(1, 2, 3)); REQUIRE(a == 6); REQUIRE_THROWS(a = f.call(1, 2, "arf")); @@ -166,7 +169,7 @@ TEST_CASE("tables/functions_variables", "Check if tables and function calls work std::cout << "stateless lambda()" << std::endl; return "test"; } - ); + ); REQUIRE_NOTHROW(run_script(lua)); lua.get("os").set_function("fun", &free_function); @@ -184,7 +187,7 @@ TEST_CASE("tables/functions_variables", "Check if tables and function calls work std::cout << "stateless lambda()" << std::endl; return "test"; } - ); + ); REQUIRE_NOTHROW(run_script(lua)); // r-value, cannot optomize From 009a79606af22949a798d7168bde2b035933be71 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 13 Dec 2013 22:34:56 -0500 Subject: [PATCH 3/6] It has to be a reference, not a value. I mistakenly thought `typename Table` was taken by reference... oh well. It's fixed now. I wish VC++ didn't choke so badly on decltype. --- sol/table.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sol/table.hpp b/sol/table.hpp index 774700a1..dcc5a10b 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -82,13 +82,13 @@ public: } template - proxy operator[](T&& key) { - return proxy(*this, std::forward(key)); + proxy operator[](T&& key) { + return proxy(*this, std::forward(key)); } template - proxy operator[](T&& key) const { - return proxy(*this, std::forward(key)); + proxy operator[](T&& key) const { + return proxy(*this, std::forward(key)); } size_t size() const { From c6f6203ab27b2bacbd6456917316a074fd4b581c Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 13 Dec 2013 23:26:59 -0500 Subject: [PATCH 4/6] We can avoid unused variable issues entirely and keep MSVC from borking out by explicitly declaring the size of the `swallow` declaration. This should make all compilers happy. --- sol/stack.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sol/stack.hpp b/sol/stack.hpp index 43212f9b..5bc59091 100644 --- a/sol/stack.hpp +++ b/sol/stack.hpp @@ -208,8 +208,8 @@ inline int push_user(lua_State* L, T& item) { namespace detail { template inline void push(lua_State* L, indices, const T& tuplen) { - using swallow = char[]; - void(swallow{'\0', (sol::stack::push(L, std::get(tuplen)), '\0')... }); + using swallow = char[ 1 + sizeof...(I) ]; + swallow {'\0', (sol::stack::push(L, std::get(tuplen)), '\0')... }; } template From 49ad128493078a3cbc9e31a62af13bd85f232c57 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 13 Dec 2013 23:30:16 -0500 Subject: [PATCH 5/6] If you want to have return types, use `.call` instead of `operator()`. --- sol/function.hpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/sol/function.hpp b/sol/function.hpp index f532962e..6a68d728 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -65,11 +65,6 @@ public: void operator()(Args&&... args) { call<>(std::forward(args)...); } - - template - auto operator()(types, Args&&... args) { - return call(std::forward(args)...); - } template auto call(Args&&... args) -> decltype(invoke(types(), sizeof...(Args))) { From 6c06bddd6e21b0b04146d3abdef5e22c2bd4c9f2 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 13 Dec 2013 23:33:06 -0500 Subject: [PATCH 6/6] Some extra-strength "nothrow" requirements. --- tests.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests.cpp b/tests.cpp index 7dc3debe..4f23f6e4 100644 --- a/tests.cpp +++ b/tests.cpp @@ -114,16 +114,16 @@ TEST_CASE("simple/callLambda", "A C++ lambda is exposed to lua and called") { TEST_CASE("advanced/callLambdaReturns", "Checks for lambdas returning values") { sol::state lua; - lua.set_function("a", [ ] { return 42; }); - lua.set_function("b", [ ] { return 42u; }); - lua.set_function("c", [ ] { return 3.14; }); - lua.set_function("d", [ ] { return 6.28f; }); - lua.set_function("e", [ ] { return "lol"; }); - lua.set_function("f", [ ] { return true; }); - lua.set_function("g", [ ] { return std::string("str"); }); - lua.set_function("h", [ ] { }); - lua.set_function("i", [ ] { return sol::nil; }); - //lua.set_function("i", [ ] { return std::make_tuple(1, 6.28f, 3.14, std::string("heh")); }); + REQUIRE_NOTHROW(lua.set_function("a", [ ] { return 42; })); + REQUIRE_NOTHROW(lua.set_function("b", [ ] { return 42u; })); + REQUIRE_NOTHROW(lua.set_function("c", [ ] { return 3.14; })); + REQUIRE_NOTHROW(lua.set_function("d", [ ] { return 6.28f; })); + REQUIRE_NOTHROW(lua.set_function("e", [ ] { return "lol"; })); + REQUIRE_NOTHROW(lua.set_function("f", [ ] { return true; })); + REQUIRE_NOTHROW(lua.set_function("g", [ ] { return std::string("str"); })); + REQUIRE_NOTHROW(lua.set_function("h", [ ] { })); + REQUIRE_NOTHROW(lua.set_function("i", [ ] { return sol::nil; })); + REQUIRE_NOTHROW(lua.set_function("j", [ ] { return std::make_tuple(1, 6.28f, 3.14, std::string( "heh" )); } )); } TEST_CASE("advanced/callLambda2", "A C++ lambda is exposed to lua and called") {