From 4d629e6c73e17e38545573d56ba244666eb9bae6 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Mon, 16 Dec 2013 11:07:21 -0500 Subject: [PATCH 1/9] Have to use cast to solve ambiguity... =/ --- tests.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests.cpp b/tests.cpp index c986f73c..d3f5f9a2 100644 --- a/tests.cpp +++ b/tests.cpp @@ -245,6 +245,11 @@ TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works lua.script("foo = 20\nbar = \"hello world\""); // basic retrieval std::string bar = lua["bar"]; + REQUIRE(bar == "hello world"); + // Ambiguity case on operator= for compilation testing + // Have to use cast, there's no other way... + bar = (std::string)lua["bar"]; + REQUIRE(bar == "hello world"); int foo = lua["foo"]; REQUIRE(bar == "hello world"); REQUIRE(foo == 20); From b90f7899863ac88a35693ec51575b1ac1f7c8955 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sat, 21 Dec 2013 19:30:30 -0500 Subject: [PATCH 2/9] operator[] support~ And all is as it should be in the world~ --- sol/proxy.hpp | 129 +++++++++++++++++++++++++++++++++++++++++++++++++ sol/state.hpp | 10 ++++ sol/table.hpp | 12 +++++ sol/traits.hpp | 25 ++++++++++ tests.cpp | 104 +++++++++++++++++++++++++++++++++++---- 5 files changed, 271 insertions(+), 9 deletions(-) create mode 100644 sol/proxy.hpp diff --git a/sol/proxy.hpp b/sol/proxy.hpp new file mode 100644 index 00000000..05d6c1e2 --- /dev/null +++ b/sol/proxy.hpp @@ -0,0 +1,129 @@ +// The MIT License (MIT) + +// Copyright (c) 2013 Danny Y., Rapptz + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#include "traits.hpp" +#include "object.hpp" +#include "function.hpp" + +namespace sol { +template +struct proxy { +private: + Table& tbl; + Key& key; + +public: + + template + proxy( Table& table, T&& key ) : tbl( table ), key( std::forward( key ) ) { + + } + + template + T get( ) const { + return tbl.get( key ); + } + + template + proxy& set( T&& item ) { + tbl.set( key, std::forward( item ) ); + return *this; + } + + template + proxy& set_function( Args&&... args ) { + tbl.set_function( key, std::forward( args ).. ); + return *this; + } + + template + EnableIf>> operator=( U&& other ) { + tbl.set_function( key, std::forward( other ) ); + } + + template + DisableIf>> operator=( U&& other ) { + tbl.set( key, std::forward( other ) ); + } + + operator nil_t ( ) { + return get( ); + } + + operator bool( ) { + return get( ); + } + + operator std::string( ) { + return get( ); + } + + operator object( ) { + return get( ); + } + + operator function( ) { + return get( ); + } + + operator double( ) { + return get( ); + } + + operator float( ) { + return get( ); + } + + operator int( ) { + return get( ); + } + + template + typename multi_return::type call( Args&&... args ) { + return tbl.get(key)(types(), std::forward( args )...); + } +}; + +template +bool operator== ( T&& left, const proxy& right ) { + typedef Decay T_; + return right.get( ) == left; +} + +template +bool operator== ( const proxy& right, T&& left ) { + typedef Decay T_; + return right.get( ) == left; +} + +template +bool operator!= ( T&& left, const proxy& right ) { + typedef Decay T_; + return right.get( ) != left; +} + +template +bool operator!= ( const proxy& right, T&& left ) { + typedef Decay T_; + return right.get( ) != left; +} + +} // sol \ No newline at end of file diff --git a/sol/state.hpp b/sol/state.hpp index d9735404..714a0f43 100644 --- a/sol/state.hpp +++ b/sol/state.hpp @@ -173,6 +173,16 @@ public: table registry() const { return reg; } + + template + proxy operator[]( T&& key ) { + return global[ std::forward(key) ]; + } + + template + proxy operator[]( T&& key ) const { + return global[ std::forward( key ) ]; + } }; } // sol diff --git a/sol/table.hpp b/sol/table.hpp index 823a8b4f..7f51609a 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -22,6 +22,7 @@ #ifndef SOL_TABLE_HPP #define SOL_TABLE_HPP +#include "proxy.hpp" #include "stack.hpp" #include "lua_function.hpp" #include @@ -117,6 +118,17 @@ public: push(); return lua_rawlen(state(), -1); } + + template + proxy operator[]( T&& key ) { + return proxy( *this, std::forward( key ) ); + } + + template + proxy operator[]( T&& key ) const { + return proxy( *this, std::forward( key ) ); + } + private: template table& set_isfunction_fx(std::true_type, T&& key, TFx&& fx) { diff --git a/sol/traits.hpp b/sol/traits.hpp index 91b9ed5c..328eb4c7 100644 --- a/sol/traits.hpp +++ b/sol/traits.hpp @@ -26,6 +26,29 @@ #include namespace sol { +namespace detail { +template>::value> +struct is_function_impl : std::is_function::type> { }; + +template +struct is_function_impl { + using yes = char; + using no = struct { char s[ 2 ]; }; + + struct F { void operator()( ); }; + struct Derived : T, F { }; + template struct Check; + + template + static no test( Check* ); + + template + static yes test( ... ); + + static const bool value = sizeof( test( 0 ) ) == sizeof( yes ); +}; +} // detail + template using EnableIf = typename std::enable_if::type; @@ -55,6 +78,8 @@ struct multi_return<> : types<>{ template using Bool = std::integral_constant; +template +struct Function : Bool::value> { }; template struct function_traits; diff --git a/tests.cpp b/tests.cpp index 9c6af2f1..355a9546 100644 --- a/tests.cpp +++ b/tests.cpp @@ -56,7 +56,7 @@ TEST_CASE("simple/get", "Tests if the get function works properly.") { REQUIRE(e == true); } -TEST_CASE("simple/addition", "") { +TEST_CASE("simple/addition", "check if addition works and can be gotten through lua.get and lua.set") { sol::state lua; lua.set("b", 0.2); @@ -66,7 +66,7 @@ TEST_CASE("simple/addition", "") { REQUIRE(c == 9.2); } -TEST_CASE("simple/if", "") { +TEST_CASE("simple/if", "check if if statements work through lua") { sol::state lua; std::string program = "if true then f = 0.1 else f = 'test' end"; @@ -74,9 +74,10 @@ TEST_CASE("simple/if", "") { auto f = lua.get("f"); REQUIRE(f == 0.1); + REQUIRE((f == lua["f"])); } -TEST_CASE("simple/callWithParameters", "Lua function is called with a few parameters from C++") { +TEST_CASE("simple/call_with_parameters", "Lua function is called with a few parameters from C++") { sol::state lua; REQUIRE_NOTHROW(lua.script("function my_add(i, j, k) return i + j + k end")); @@ -90,7 +91,7 @@ TEST_CASE("simple/callWithParameters", "Lua function is called with a few parame REQUIRE_THROWS(a = f.call(1, 2, "arf")); } -TEST_CASE("simple/callCppFunction", "C++ function is called from lua") { +TEST_CASE("simple/call_c++_function", "C++ function is called from lua") { sol::state lua; lua.set_function("plop_xyz", plop_xyz); @@ -99,7 +100,7 @@ TEST_CASE("simple/callCppFunction", "C++ function is called from lua") { REQUIRE(lua.get("x") == 11); } -TEST_CASE("simple/callLambda", "A C++ lambda is exposed to lua and called") { +TEST_CASE("simple/call_lambda", "A C++ lambda is exposed to lua and called") { sol::state lua; int x = 0; @@ -111,7 +112,7 @@ TEST_CASE("simple/callLambda", "A C++ lambda is exposed to lua and called") { REQUIRE(x == 1); } -TEST_CASE("advanced/callLambdaReturns", "Checks for lambdas returning values") { +TEST_CASE("advanced/get_and_call", "Checks for lambdas returning values after a get operation") { const static std::string lol = "lol", str = "str"; const static std::tuple heh_tuple = std::make_tuple(1, 6.28f, 3.14, std::string("heh")); sol::state lua; @@ -146,7 +147,42 @@ TEST_CASE("advanced/callLambdaReturns", "Checks for lambdas returning values") { REQUIRE((lua.get("j").call() == heh_tuple)); } -TEST_CASE("advanced/callLambda2", "A C++ lambda is exposed to lua and called") { +TEST_CASE("advanced/operator[]_calls", "Checks for lambdas returning values using operator[]") { + const static std::string lol = "lol", str = "str"; + const static std::tuple heh_tuple = std::make_tuple(1, 6.28f, 3.14, std::string("heh")); + sol::state lua; + + REQUIRE_NOTHROW(lua.set_function("a", [ ] { return 42; })); + REQUIRE(lua["a"].call() == 42); + + REQUIRE_NOTHROW(lua.set_function("b", [ ] { return 42u; })); + REQUIRE(lua["b"].call() == 42u); + + REQUIRE_NOTHROW(lua.set_function("c", [ ] { return 3.14; })); + REQUIRE(lua["c"].call() == 3.14); + + REQUIRE_NOTHROW(lua.set_function("d", [ ] { return 6.28f; })); + REQUIRE(lua["d"].call() == 6.28f); + + REQUIRE_NOTHROW(lua.set_function("e", [ ] { return "lol"; })); + REQUIRE(lua["e"].call() == lol); + + REQUIRE_NOTHROW(lua.set_function("f", [ ] { return true; })); + REQUIRE(lua["f"].call()); + + REQUIRE_NOTHROW(lua.set_function("g", [ ] { return std::string("str"); })); + REQUIRE(lua["g"].call() == str); + + REQUIRE_NOTHROW(lua.set_function("h", [ ] { })); + REQUIRE_NOTHROW(lua["h"].call()); + + REQUIRE_NOTHROW(lua.set_function("i", [ ] { return sol::nil; })); + 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/call_lambdas", "A C++ lambda is exposed to lua and called") { sol::state lua; int x = 0; @@ -159,13 +195,13 @@ TEST_CASE("advanced/callLambda2", "A C++ lambda is exposed to lua and called") { REQUIRE(x == 9); } -TEST_CASE("negative/basicError", "Check if error handling works correctly") { +TEST_CASE("negative/basic_errors", "Check if error handling works correctly") { sol::state lua; REQUIRE_THROWS(lua.script("nil[5]")); } -TEST_CASE("libraries", "Check if we can open libraries through sol") { +TEST_CASE("libraries", "Check if we can open libraries") { sol::state lua; REQUIRE_NOTHROW(lua.open_libraries(sol::lib::base, sol::lib::os)); } @@ -236,4 +272,54 @@ TEST_CASE("functions/return_order_and_multi_get", "Check if return order is in t REQUIRE(tcpp == triple); REQUIRE(tlua == triple); REQUIRE(tluaget == triple); +} + +TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works properly") { + sol::state lua; + lua.open_libraries(sol::lib::base); + + lua.script("foo = 20\nbar = \"hello world\""); + // basic retrieval + std::string bar = lua["bar"]; + int foo = lua["foo"]; + REQUIRE(bar == "hello world"); + REQUIRE(foo == 20); + // test operator= for stringification + bar = lua["bar"]; + + // basic setting + lua["bar"] = 20.4; + lua["foo"] = "goodbye"; + + // doesn't modify the actual values obviously. + REQUIRE(bar == "hello world"); + REQUIRE(foo == 20); + + // function setting + lua["test"] = plop_xyz; + REQUIRE_NOTHROW(lua.script("assert(test(10, 11, \"hello\") == 11)")); + + // function retrieval + sol::function test = lua["test"]; + REQUIRE(test.call(10, 11, "hello") == 11); + + // setting a lambda + lua["lamb"] = [](int x) { + return x * 2; + }; + + REQUIRE_NOTHROW(lua.script("assert(lamb(220) == 440)")); + + // function retrieval of a lambda + sol::function lamb = lua["lamb"]; + REQUIRE(lamb.call(220) == 440); + + // test const table retrieval + auto assert1 = [](const sol::table& t) { + std::string a = t["foo"]; + int b = t["bar"]; + std::cout << a << ',' << b << '\n'; + }; + + REQUIRE_NOTHROW(assert1(lua.global_table())); } \ No newline at end of file From ef223ecaebc595b7437aada7257de575560f0a6f Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sat, 21 Dec 2013 19:39:51 -0500 Subject: [PATCH 3/9] I wish C++ didn't demand that functions have to appear before each other in lexographic order. =/ --- sol/traits.hpp | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/sol/traits.hpp b/sol/traits.hpp index 328eb4c7..57575cc2 100644 --- a/sol/traits.hpp +++ b/sol/traits.hpp @@ -26,29 +26,6 @@ #include namespace sol { -namespace detail { -template>::value> -struct is_function_impl : std::is_function::type> { }; - -template -struct is_function_impl { - using yes = char; - using no = struct { char s[ 2 ]; }; - - struct F { void operator()( ); }; - struct Derived : T, F { }; - template struct Check; - - template - static no test( Check* ); - - template - static yes test( ... ); - - static const bool value = sizeof( test( 0 ) ) == sizeof( yes ); -}; -} // detail - template using EnableIf = typename std::enable_if::type; @@ -78,6 +55,30 @@ struct multi_return<> : types<>{ template using Bool = std::integral_constant; + +namespace detail { +template>::value> +struct is_function_impl : std::is_function::type> { }; + +template +struct is_function_impl { + using yes = char; + using no = struct { char s[ 2 ]; }; + + struct F { void operator()( ); }; + struct Derived : T, F { }; + template struct Check; + + template + static no test( Check* ); + + template + static yes test( ... ); + + static const bool value = sizeof( test( 0 ) ) == sizeof( yes ); +}; +} // detail + template struct Function : Bool::value> { }; From 7ca6ce8d5533aa66fbadffc10603d195841a671c Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sat, 21 Dec 2013 19:42:50 -0500 Subject: [PATCH 4/9] Forgot a dot. --- sol/proxy.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sol/proxy.hpp b/sol/proxy.hpp index 05d6c1e2..199cd77f 100644 --- a/sol/proxy.hpp +++ b/sol/proxy.hpp @@ -50,7 +50,7 @@ public: template proxy& set_function( Args&&... args ) { - tbl.set_function( key, std::forward( args ).. ); + tbl.set_function( key, std::forward( args )... ); return *this; } From 4f279c503308a10dea869ed823c6055e67399d14 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sat, 21 Dec 2013 19:44:31 -0500 Subject: [PATCH 5/9] Strangely enough, GCC can't handle having `T_` as a typdef. --- sol/proxy.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sol/proxy.hpp b/sol/proxy.hpp index 199cd77f..e0de3468 100644 --- a/sol/proxy.hpp +++ b/sol/proxy.hpp @@ -104,26 +104,26 @@ public: template bool operator== ( T&& left, const proxy& right ) { - typedef Decay T_; - return right.get( ) == left; + typedef Decay TDk; + return right.get( ) == left; } template bool operator== ( const proxy& right, T&& left ) { - typedef Decay T_; - return right.get( ) == left; + typedef Decay TDk; + return right.get( ) == left; } template bool operator!= ( T&& left, const proxy& right ) { - typedef Decay T_; - return right.get( ) != left; + typedef Decay TDk; + return right.get( ) != left; } template bool operator!= ( const proxy& right, T&& left ) { - typedef Decay T_; - return right.get( ) != left; + typedef Decay TDk; + return right.get( ) != left; } } // sol \ No newline at end of file From cfda1d34bad10fe8696326674f06e3a0c99a5c05 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sat, 21 Dec 2013 19:51:48 -0500 Subject: [PATCH 6/9] Trying to guess what GCC wants in this case is baffling... --- sol/proxy.hpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/sol/proxy.hpp b/sol/proxy.hpp index e0de3468..bd7a083b 100644 --- a/sol/proxy.hpp +++ b/sol/proxy.hpp @@ -104,26 +104,22 @@ public: template bool operator== ( T&& left, const proxy& right ) { - typedef Decay TDk; - return right.get( ) == left; + return right.get>( ) == left; } template bool operator== ( const proxy& right, T&& left ) { - typedef Decay TDk; - return right.get( ) == left; + return right.get>( ) == left; } template bool operator!= ( T&& left, const proxy& right ) { - typedef Decay TDk; - return right.get( ) != left; + return right.get>( ) != left; } template bool operator!= ( const proxy& right, T&& left ) { - typedef Decay TDk; - return right.get( ) != left; + return right.get>( ) != left; } } // sol \ No newline at end of file From 94c287810faeb0400c440f2834793cbafcdce71f Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sat, 21 Dec 2013 22:00:28 -0500 Subject: [PATCH 7/9] It took a lott of work, but we finally have the right rankings for operator[] to work as intended with as minimal room for screwups as possible. This should enable operator[] to work in all cases that it is to be expected, thanks to a lot of explicit conversions and some basic overload resolution ranking tricks (e.g., making some conversions a template). We also need to remember that GCC expects `. template (function name)` in generic code, as it can parse things in a really dumb and silly manner. --- sol/proxy.hpp | 48 ++++++++++++++++++++++++++---------------------- tests.cpp | 9 +++++---- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/sol/proxy.hpp b/sol/proxy.hpp index bd7a083b..aa0d6129 100644 --- a/sol/proxy.hpp +++ b/sol/proxy.hpp @@ -64,35 +64,39 @@ public: tbl.set( key, std::forward( other ) ); } - operator nil_t ( ) { + operator nil_t ( ) const { return get( ); } - operator bool( ) { - return get( ); - } - - operator std::string( ) { - return get( ); - } - - operator object( ) { + operator object( ) const { return get( ); } - operator function( ) { + operator function( ) const { return get( ); } - operator double( ) { + operator std::string( ) const { + return get( ); + } + + template + operator bool( ) const { + return get( ); + } + + template + operator double( ) const { return get( ); } - operator float( ) { + template + operator float( ) const { return get( ); } - operator int( ) { + template + operator int( ) const { return get( ); } @@ -103,23 +107,23 @@ public: }; template -bool operator== ( T&& left, const proxy& right ) { - return right.get>( ) == left; +inline bool operator== ( T&& left, const proxy& right ) { + return left == right.template get>( ); } template -bool operator== ( const proxy& right, T&& left ) { - return right.get>( ) == left; +inline bool operator== ( const proxy& right, T&& left ) { + return right.template get>( ) == left; } template -bool operator!= ( T&& left, const proxy& right ) { - return right.get>( ) != left; +inline bool operator!= ( T&& left, const proxy& right ) { + return right.template get>( ) != left; } template -bool operator!= ( const proxy& right, T&& left ) { - return right.get>( ) != left; +inline bool operator!= ( const proxy& right, T&& left ) { + return right.template get>( ) != left; } } // sol \ No newline at end of file diff --git a/tests.cpp b/tests.cpp index 355a9546..3a40f181 100644 --- a/tests.cpp +++ b/tests.cpp @@ -285,6 +285,7 @@ TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works REQUIRE(bar == "hello world"); REQUIRE(foo == 20); // test operator= for stringification + // errors due to ambiguous operators bar = lua["bar"]; // basic setting @@ -300,7 +301,7 @@ TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works REQUIRE_NOTHROW(lua.script("assert(test(10, 11, \"hello\") == 11)")); // function retrieval - sol::function test = lua["test"]; + sol::function test = lua[ "test" ]; REQUIRE(test.call(10, 11, "hello") == 11); // setting a lambda @@ -311,15 +312,15 @@ TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works REQUIRE_NOTHROW(lua.script("assert(lamb(220) == 440)")); // function retrieval of a lambda - sol::function lamb = lua["lamb"]; + sol::function lamb;// = lua[ "lamb" ]; REQUIRE(lamb.call(220) == 440); // test const table retrieval auto assert1 = [](const sol::table& t) { std::string a = t["foo"]; - int b = t["bar"]; + int b = t["bar"]; std::cout << a << ',' << b << '\n'; }; REQUIRE_NOTHROW(assert1(lua.global_table())); -} \ No newline at end of file +} From 3d44c6500ed2ae3ab4821a8c2b7cc8418faf085e Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sat, 21 Dec 2013 22:05:19 -0500 Subject: [PATCH 8/9] And a last fix for the test we had to block while fixing implicit conversions, and we're good! --- tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests.cpp b/tests.cpp index 3a40f181..b0072c0a 100644 --- a/tests.cpp +++ b/tests.cpp @@ -312,7 +312,7 @@ TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works REQUIRE_NOTHROW(lua.script("assert(lamb(220) == 440)")); // function retrieval of a lambda - sol::function lamb;// = lua[ "lamb" ]; + sol::function lamb = lua[ "lamb" ]; REQUIRE(lamb.call(220) == 440); // test const table retrieval From 750d110a92d919e5e01524fe0ba55dc608d1ebb0 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sat, 21 Dec 2013 22:11:20 -0500 Subject: [PATCH 9/9] Fixing formatting to make sure everything's nice and pretty. --- sol/proxy.hpp | 134 ++++++++++++++++++++++++------------------------- sol/stack.hpp | 2 +- sol/state.hpp | 8 +-- sol/table.hpp | 14 +++--- sol/traits.hpp | 20 ++++---- tests.cpp | 68 ++++++++++++------------- 6 files changed, 123 insertions(+), 123 deletions(-) diff --git a/sol/proxy.hpp b/sol/proxy.hpp index aa0d6129..09097660 100644 --- a/sol/proxy.hpp +++ b/sol/proxy.hpp @@ -27,103 +27,103 @@ namespace sol { template struct proxy { private: - Table& tbl; - Key& key; + Table& tbl; + Key& key; public: - template - proxy( Table& table, T&& key ) : tbl( table ), key( std::forward( key ) ) { + template + proxy(Table& table, T&& key) : tbl(table), key(std::forward(key)) { - } + } - template - T get( ) const { - return tbl.get( key ); - } + template + T get() const { + return tbl.get(key); + } - template - proxy& set( T&& item ) { - tbl.set( key, std::forward( item ) ); - return *this; - } + template + proxy& set(T&& item) { + tbl.set(key, std::forward(item)); + return *this; + } - template - proxy& set_function( Args&&... args ) { - tbl.set_function( key, std::forward( args )... ); - return *this; - } + template + proxy& set_function(Args&&... args) { + tbl.set_function(key, std::forward(args)...); + return *this; + } - template - EnableIf>> operator=( U&& other ) { - tbl.set_function( key, std::forward( other ) ); - } + template + EnableIf>> operator=(U&& other) { + tbl.set_function(key, std::forward(other)); + } - template - DisableIf>> operator=( U&& other ) { - tbl.set( key, std::forward( other ) ); - } + template + DisableIf>> operator=(U&& other) { + tbl.set(key, std::forward(other)); + } - operator nil_t ( ) const { - return get( ); - } + operator nil_t () const { + return get(); + } - operator object( ) const { - return get( ); - } + operator object() const { + return get(); + } - operator function( ) const { - return get( ); - } + operator function() const { + return get(); + } - operator std::string( ) const { - return get( ); - } + operator std::string() const { + return get(); + } - template - operator bool( ) const { - return get( ); - } + template + operator bool() const { + return get(); + } - template - operator double( ) const { - return get( ); - } + template + operator double() const { + return get(); + } - template - operator float( ) const { - return get( ); - } + template + operator float() const { + return get(); + } - template - operator int( ) const { - return get( ); - } + template + operator int() const { + return get(); + } - template - typename multi_return::type call( Args&&... args ) { - return tbl.get(key)(types(), std::forward( args )...); - } + template + typename multi_return::type call(Args&&... args) { + return tbl.get(key)(types(), std::forward(args)...); + } }; template -inline bool operator== ( T&& left, const proxy& right ) { - return left == right.template get>( ); +inline bool operator== (T&& left, const proxy& right) { + return left == right.template get>(); } template -inline bool operator== ( const proxy& right, T&& left ) { - return right.template get>( ) == left; +inline bool operator== (const proxy& right, T&& left) { + return right.template get>() == left; } template -inline bool operator!= ( T&& left, const proxy& right ) { - return right.template get>( ) != left; +inline bool operator!= (T&& left, const proxy& right) { + return right.template get>() != left; } template -inline bool operator!= ( const proxy& right, T&& left ) { - return right.template get>( ) != left; +inline bool operator!= (const proxy& right, T&& left) { + return right.template get>() != left; } } // sol \ No newline at end of file diff --git a/sol/stack.hpp b/sol/stack.hpp index 6bb1608f..50ca2540 100644 --- a/sol/stack.hpp +++ b/sol/stack.hpp @@ -208,7 +208,7 @@ inline int push_user(lua_State* L, T& item) { namespace detail { template inline void push_tuple(lua_State* L, indices, T&& tuplen) { - using swallow = char[ 1 + sizeof...(I) ]; + using swallow = char[1 + sizeof...(I)]; swallow {'\0', (sol::stack::push(L, std::get(tuplen)), '\0')... }; } diff --git a/sol/state.hpp b/sol/state.hpp index 714a0f43..1c933903 100644 --- a/sol/state.hpp +++ b/sol/state.hpp @@ -175,13 +175,13 @@ public: } template - proxy operator[]( T&& key ) { - return global[ std::forward(key) ]; + proxy operator[](T&& key) { + return global[std::forward(key)]; } template - proxy operator[]( T&& key ) const { - return global[ std::forward( key ) ]; + proxy operator[](T&& key) const { + return global[std::forward(key)]; } }; } // sol diff --git a/sol/table.hpp b/sol/table.hpp index 7f51609a..39d18914 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -120,13 +120,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)); } private: @@ -182,7 +182,7 @@ private: void* userobjdata = static_cast(detail::get_ptr(obj)); lua_CFunction freefunc = &static_object_lua_func, TFx>::call; const char* freefuncname = fkey.c_str(); - const luaL_Reg funcreg[ 2 ] = { + const luaL_Reg funcreg[2] = { { freefuncname, freefunc }, { nullptr, nullptr } }; @@ -204,7 +204,7 @@ private: Decay target(std::forward(fx)); lua_CFunction freefunc = &static_lua_func::call; const char* freefuncname = fkey.c_str(); - const luaL_Reg funcreg[ 2 ] = { + const luaL_Reg funcreg[2] = { { freefuncname, freefunc }, { nullptr, nullptr } }; @@ -229,7 +229,7 @@ private: lua_CFunction freefunc = &lua_func::call; const char* freefuncname = fkey.c_str(); const char* metatablename = metakey.c_str(); - const luaL_Reg funcreg[ 2 ] = { + const luaL_Reg funcreg[2] = { { freefuncname, freefunc }, { nullptr, nullptr } }; diff --git a/sol/traits.hpp b/sol/traits.hpp index 57575cc2..cb082ca7 100644 --- a/sol/traits.hpp +++ b/sol/traits.hpp @@ -62,20 +62,20 @@ struct is_function_impl : std::is_function::type template struct is_function_impl { - using yes = char; - using no = struct { char s[ 2 ]; }; + using yes = char; + using no = struct { char s[2]; }; - struct F { void operator()( ); }; - struct Derived : T, F { }; - template struct Check; + struct F { void operator()(); }; + struct Derived : T, F { }; + template struct Check; - template - static no test( Check* ); + template + static no test(Check*); - template - static yes test( ... ); + template + static yes test(...); - static const bool value = sizeof( test( 0 ) ) == sizeof( yes ); + static const bool value = sizeof(test(0)) == sizeof(yes); }; } // detail diff --git a/tests.cpp b/tests.cpp index b0072c0a..8ec1a907 100644 --- a/tests.cpp +++ b/tests.cpp @@ -105,7 +105,7 @@ TEST_CASE("simple/call_lambda", "A C++ lambda is exposed to lua and called") { int x = 0; - lua.set_function("foo", [ &x ] { x = 1; }); + lua.set_function("foo", [&x] { x = 1; }); lua.script("foo()"); @@ -117,33 +117,33 @@ TEST_CASE("advanced/get_and_call", "Checks for lambdas returning values after a const static std::tuple heh_tuple = std::make_tuple(1, 6.28f, 3.14, std::string("heh")); sol::state lua; - REQUIRE_NOTHROW(lua.set_function("a", [ ] { return 42; })); + REQUIRE_NOTHROW(lua.set_function("a", [] { return 42; })); REQUIRE(lua.get("a").call() == 42); - REQUIRE_NOTHROW(lua.set_function("b", [ ] { return 42u; })); + REQUIRE_NOTHROW(lua.set_function("b", [] { return 42u; })); REQUIRE(lua.get("b").call() == 42u); - REQUIRE_NOTHROW(lua.set_function("c", [ ] { return 3.14; })); + REQUIRE_NOTHROW(lua.set_function("c", [] { return 3.14; })); REQUIRE(lua.get("c").call() == 3.14); - REQUIRE_NOTHROW(lua.set_function("d", [ ] { return 6.28f; })); + REQUIRE_NOTHROW(lua.set_function("d", [] { return 6.28f; })); REQUIRE(lua.get("d").call() == 6.28f); - REQUIRE_NOTHROW(lua.set_function("e", [ ] { return "lol"; })); + REQUIRE_NOTHROW(lua.set_function("e", [] { return "lol"; })); REQUIRE(lua.get("e").call() == lol); - REQUIRE_NOTHROW(lua.set_function("f", [ ] { return true; })); + REQUIRE_NOTHROW(lua.set_function("f", [] { return true; })); REQUIRE(lua.get("f").call()); - REQUIRE_NOTHROW(lua.set_function("g", [ ] { return std::string("str"); })); + REQUIRE_NOTHROW(lua.set_function("g", [] { return std::string("str"); })); REQUIRE(lua.get("g").call() == str); - REQUIRE_NOTHROW(lua.set_function("h", [ ] { })); + REQUIRE_NOTHROW(lua.set_function("h", [] { })); REQUIRE_NOTHROW(lua.get("h").call()); - REQUIRE_NOTHROW(lua.set_function("i", [ ] { return sol::nil; })); + REQUIRE_NOTHROW(lua.set_function("i", [] { return sol::nil; })); REQUIRE(lua.get("i").call() == sol::nil); - REQUIRE_NOTHROW(lua.set_function("j", [ ] { return std::make_tuple(1, 6.28f, 3.14, std::string("heh")); })); + REQUIRE_NOTHROW(lua.set_function("j", [] { return std::make_tuple(1, 6.28f, 3.14, std::string("heh")); })); REQUIRE((lua.get("j").call() == heh_tuple)); } @@ -152,33 +152,33 @@ TEST_CASE("advanced/operator[]_calls", "Checks for lambdas returning values usin const static std::tuple heh_tuple = std::make_tuple(1, 6.28f, 3.14, std::string("heh")); sol::state lua; - REQUIRE_NOTHROW(lua.set_function("a", [ ] { return 42; })); + REQUIRE_NOTHROW(lua.set_function("a", [] { return 42; })); REQUIRE(lua["a"].call() == 42); - REQUIRE_NOTHROW(lua.set_function("b", [ ] { return 42u; })); + REQUIRE_NOTHROW(lua.set_function("b", [] { return 42u; })); REQUIRE(lua["b"].call() == 42u); - REQUIRE_NOTHROW(lua.set_function("c", [ ] { return 3.14; })); + REQUIRE_NOTHROW(lua.set_function("c", [] { return 3.14; })); REQUIRE(lua["c"].call() == 3.14); - REQUIRE_NOTHROW(lua.set_function("d", [ ] { return 6.28f; })); + REQUIRE_NOTHROW(lua.set_function("d", [] { return 6.28f; })); REQUIRE(lua["d"].call() == 6.28f); - REQUIRE_NOTHROW(lua.set_function("e", [ ] { return "lol"; })); + REQUIRE_NOTHROW(lua.set_function("e", [] { return "lol"; })); REQUIRE(lua["e"].call() == lol); - REQUIRE_NOTHROW(lua.set_function("f", [ ] { return true; })); + REQUIRE_NOTHROW(lua.set_function("f", [] { return true; })); REQUIRE(lua["f"].call()); - REQUIRE_NOTHROW(lua.set_function("g", [ ] { return std::string("str"); })); + REQUIRE_NOTHROW(lua.set_function("g", [] { return std::string("str"); })); REQUIRE(lua["g"].call() == str); - REQUIRE_NOTHROW(lua.set_function("h", [ ] { })); + REQUIRE_NOTHROW(lua.set_function("h", [] { })); REQUIRE_NOTHROW(lua["h"].call()); - REQUIRE_NOTHROW(lua.set_function("i", [ ] { return sol::nil; })); + REQUIRE_NOTHROW(lua.set_function("i", [] { return sol::nil; })); 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_NOTHROW(lua.set_function("j", [] { return std::make_tuple(1, 6.28f, 3.14, std::string("heh")); })); REQUIRE((lua["j"].call() == heh_tuple)); } @@ -186,7 +186,7 @@ TEST_CASE("advanced/call_lambdas", "A C++ lambda is exposed to lua and called") sol::state lua; int x = 0; - lua.set_function("set_x", [ &] (int new_x) { + lua.set_function("set_x", [&] (int new_x) { x = new_x; return 0; }); @@ -216,16 +216,16 @@ TEST_CASE("tables/variables", "Check if tables and variables work as intended") TEST_CASE("tables/functions_variables", "Check if tables and function calls work as intended") { sol::state lua; lua.open_libraries(sol::lib::base, sol::lib::os); - auto run_script = [ ] (sol::state& lua) -> void { + auto run_script = [] (sol::state& lua) -> void { lua.script("assert(os.fun() == \"test\")"); }; lua.get("os").set_function("fun", - [ ] () { + [] () { std::cout << "stateless lambda()" << std::endl; return "test"; } - ); + ); REQUIRE_NOTHROW(run_script(lua)); lua.get("os").set_function("fun", &free_function); @@ -239,11 +239,11 @@ TEST_CASE("tables/functions_variables", "Check if tables and function calls work // stateful lambda: non-convertible, unoptomizable int breakit = 50; lua.get("os").set_function("fun", - [ &breakit ] () { + [&breakit] () { std::cout << "stateless lambda()" << std::endl; return "test"; } - ); + ); REQUIRE_NOTHROW(run_script(lua)); // r-value, cannot optomize @@ -256,12 +256,12 @@ TEST_CASE("tables/functions_variables", "Check if tables and function calls work REQUIRE_NOTHROW(run_script(lua)); } -TEST_CASE("functions/return_order_and_multi_get", "Check if return order is in the same reading order specified in Lua" ) { +TEST_CASE("functions/return_order_and_multi_get", "Check if return order is in the same reading order specified in Lua") { const static std::tuple triple = std::make_tuple(10, 11, 12); sol::state lua; - lua.set_function( "f", [ ] { - return std::make_tuple( 10, 11, 12 ); - } ); + lua.set_function("f", [] { + return std::make_tuple(10, 11, 12); + }); lua.script("function g() return 10, 11, 12 end\nx,y,z = g()"); auto tcpp = lua.get("f").call(); auto tlua = lua.get("g").call(); @@ -301,7 +301,7 @@ TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works REQUIRE_NOTHROW(lua.script("assert(test(10, 11, \"hello\") == 11)")); // function retrieval - sol::function test = lua[ "test" ]; + sol::function test = lua["test"]; REQUIRE(test.call(10, 11, "hello") == 11); // setting a lambda @@ -312,13 +312,13 @@ TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works REQUIRE_NOTHROW(lua.script("assert(lamb(220) == 440)")); // function retrieval of a lambda - sol::function lamb = lua[ "lamb" ]; + sol::function lamb = lua["lamb"]; REQUIRE(lamb.call(220) == 440); // test const table retrieval auto assert1 = [](const sol::table& t) { std::string a = t["foo"]; - int b = t["bar"]; + int b = t["bar"]; std::cout << a << ',' << b << '\n'; };