From e816d07121bbc4c96a6dd7a5c4140f831a106a5a Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 15 May 2015 22:26:18 -0400 Subject: [PATCH] Standard-compliant function signature deduction (cannot use set_function format as that creates an ambiguity) state now has for_each (runs on global table like all other functions) added for_each tests, per @Rapptz request --- sol/function.hpp | 5 ----- sol/proxy.hpp | 4 ++++ sol/state.hpp | 5 +++++ sol/table.hpp | 9 +++----- tests.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 11 deletions(-) diff --git a/sol/function.hpp b/sol/function.hpp index abe9d656..3a2c563a 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -105,11 +105,6 @@ struct pusher> { set_isconvertible_fx(is_convertible(), t, L, std::forward(fx)); } - template::type> - static void set_memfx(types, lua_State* L, Fx&& fx){ - set_memfx(types(), L, std::forward(fx)); - } - template static void set_memfx(types<>, lua_State* L, Fx&& fx) { typedef Unqualified> fx_t; diff --git a/sol/proxy.hpp b/sol/proxy.hpp index 4c46804b..a2fe3254 100644 --- a/sol/proxy.hpp +++ b/sol/proxy.hpp @@ -77,6 +77,10 @@ public: return get(); } + operator Unqualified() const { + return get>(); + } + operator std::string() const { return get(); } diff --git a/sol/state.hpp b/sol/state.hpp index 82470000..0c6dc8f9 100644 --- a/sol/state.hpp +++ b/sol/state.hpp @@ -192,6 +192,11 @@ public: return *this; } + template + void for_each(Fx&& fx) { + global.for_each(std::forward(fx)); + } + template table create_table(T&& key, int narr = 0, int nrec = 0) { lua_createtable(L.get(), narr, nrec); diff --git a/sol/table.hpp b/sol/table.hpp index daef8ebf..d5ab4720 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -113,7 +113,9 @@ public: size_t size() const { push(); - return lua_rawlen(state(), -1); + size_t result = lua_rawlen(state(), -1); + pop(); + return result; } template @@ -166,11 +168,6 @@ private: set_resolved_function(std::forward(key), std::forward(fx)); } - template::type> - void set_fx(types, Key&& key, Fx&& fx){ - set_fx(types(), std::forward(key), std::forward(fx)); - } - template void set_fx(types<>, Key&& key, Fx&& fx) { typedef Unqualified> fx_t; diff --git a/tests.cpp b/tests.cpp index ffa2ccab..fe60325b 100644 --- a/tests.cpp +++ b/tests.cpp @@ -730,6 +730,59 @@ TEST_CASE("tables/arbitrary-creation", "tables should be created from standard c REQUIRE(c.get("project") == "sol"); } +TEST_CASE("tables/for_each", "Testing the use of for_each to get values from a lua table") { + sol::state lua; + lua.open_libraries(sol::lib::base); + + lua.script("arr = {\n" + "[0] = \"Hi\",\n" + "[1] = 123.45,\n" + "[2] = \"String value\",\n" + // Does nothing + //"[3] = nil,\n" + //"[nil] = 3,\n" + "[\"WOOF\"] = 123,\n" + "}"); + sol::table tbl = lua[ "arr" ]; + std::size_t tablesize = 4; + std::size_t iterations = 0; + tbl.for_each( + [&iterations](sol::object key, sol::object value) { + ++iterations; + sol::type keytype = key.get_type(); + switch (keytype) { + case sol::type::number: + switch (key.as()) { + case 0: + REQUIRE((value.as() == "Hi")); + break; + case 1: + REQUIRE((value.as() == 123.45)); + break; + case 2: + REQUIRE((value.as() == "String value")); + break; + case 3: + REQUIRE((value.is())); + break; + } + break; + case sol::type::string: + if (key.as() == "WOOF") { + REQUIRE((value.as() == 123)); + } + break; + case sol::type::nil: + REQUIRE((value.as() == 3)); + break; + default: + break; + } + } + ); + REQUIRE(iterations == tablesize); +} + TEST_CASE("tables/issue-number-twenty-five", "Using pointers and references from C++ classes in Lua") { struct test { int x = 0;