From 2192e98eecd0ea4956a56a2045451184c86debf0 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 13 Dec 2013 14:16:59 -0500 Subject: [PATCH] MSVC needs explicit overloads and explicit =default operators on `sol::function`. It's quite whacky, really. Anyway, it compiles, so let's just make sure this works for GCC too. --- sol/function.hpp | 2 ++ sol/table.hpp | 56 +++++++++++++++++++++++++++--------------------- sol/traits.hpp | 4 ++-- tests.cpp | 4 ++-- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/sol/function.hpp b/sol/function.hpp index 49319308..264fc0e9 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -58,6 +58,8 @@ public: function(lua_State* L, int index = -1): reference(L, index) { type_assert(L, index, type::function); } + function(const function&) = default; + function& operator=(const function&) = default; template auto call(Args&&... args) -> decltype(invoke(types(), sizeof...(Args))) { diff --git a/sol/table.hpp b/sol/table.hpp index 3ea92e20..5f15f869 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -24,6 +24,7 @@ #include "stack.hpp" #include "lua_function.hpp" +#include "function.hpp" #include #include @@ -90,6 +91,37 @@ public: push(); return lua_rawlen(state(), -1); } + + template + struct proxy { + private: + table& t; + T& key; + public: + proxy(table& t, T& key) : t(t), key(key) {} + + template + void operator=(U&& other) { + typedef Function> isfunction; + assign(std::forward(other), isfunction()); + } + + template + operator U() const { + return t.get(key); + } + + private: + template + void assign(U&& other, std::true_type) { + t.set_function(key, std::forward(other)); + } + + template + void assign(U&& other, std::false_type) { + t.set(key, std::forward(other)); + } + }; private: template table& set_isfunction_fx(std::true_type, T&& key, TFx&& fx) { @@ -207,30 +239,6 @@ private: lua_pop(state(), 1); return *this; } - - template - struct proxy { - private: - table& t; - T& key; - public: - proxy(table& t, T& key): t(t), key(key) {} - - template - DisableIf>> operator=(U&& other) { - t.set(key, std::forward(other)); - } - - template - EnableIf>> operator=(U&& other) { - t.set_function(key, std::forward(other)); - } - - template - operator U() const { - return t.get(key); - } - }; }; } // sol diff --git a/sol/traits.hpp b/sol/traits.hpp index 63f5b9be..5bcd35d3 100644 --- a/sol/traits.hpp +++ b/sol/traits.hpp @@ -40,11 +40,11 @@ using Decay = typename std::decay::type; namespace detail { // code borrowed from Gears // https://github.com/Rapptz/Gears/ -template +template>::value> struct is_function_impl : std::is_function::type> {}; template -struct is_function_impl>>> { +struct is_function_impl { using yes = char; using no = struct { char s[2]; }; diff --git a/tests.cpp b/tests.cpp index 0ed2659a..10a57b80 100644 --- a/tests.cpp +++ b/tests.cpp @@ -165,7 +165,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); @@ -183,7 +183,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