From ac975872ad9d6faf470e962e76017cd2b4ccfbce Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 16 Dec 2013 11:07:10 -0500 Subject: [PATCH] Remove support for operator[] on tables --- README.md | 1 - sol/state.hpp | 5 ---- sol/table.hpp | 41 ------------------------------ sol/traits.hpp | 28 --------------------- tests.cpp | 68 ++++++++------------------------------------------ 5 files changed, 10 insertions(+), 133 deletions(-) diff --git a/README.md b/README.md index edca8132..8d6887b8 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,5 @@ Sol makes use of C++11 features. GCC 4.7 and Clang 3.3 or higher should be able ## TODO -- Support for `operator[]` based retrieval and modifying of tables (mostly finished). - Possibly document functions and classes via doxygen. - Provide more examples to showcase uses. diff --git a/sol/state.hpp b/sol/state.hpp index aebb136b..d9735404 100644 --- a/sol/state.hpp +++ b/sol/state.hpp @@ -150,11 +150,6 @@ public: return *this; } - template - auto operator[](T&& key) -> decltype(global[std::forward(key)]) { - return global[std::forward(key)]; - } - 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 5ab57618..823a8b4f 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -42,8 +42,6 @@ T* get_ptr(T* val) { class table : public reference { friend class state; - template struct proxy; - template T single_get(U&& key) const { push(); @@ -115,50 +113,11 @@ public: std::forward(key), std::forward(fx), std::forward(obj)); } - template - proxy operator[](T&& key) { - return proxy(*this, std::forward(key)); - } - - template - proxy operator[](T&& key) const { - return proxy(*this, std::forward(key)); - } - size_t size() const { push(); return lua_rawlen(state(), -1); } private: - template - struct proxy { - private: - Table t; - T& key; - public: - proxy(Table t, T& key) : t(t), key(key) {} - - template - EnableIf>> operator=(U&& other) { - t.set_function(key, std::forward(other)); - } - - template - DisableIf>> operator=(U&& other) { - t.set(key, std::forward(other)); - } - - template - operator U() const { - return t.get(key); - } - - template - typename multi_return::type call(Args&&... args) { - return t.get(key)(types(), std::forward(args)...); - } - }; - template table& set_isfunction_fx(std::true_type, T&& key, TFx&& fx) { return set_fx(std::false_type(), std::forward(key), std::forward(fx)); diff --git a/sol/traits.hpp b/sol/traits.hpp index c90a08c6..91b9ed5c 100644 --- a/sol/traits.hpp +++ b/sol/traits.hpp @@ -38,31 +38,6 @@ using Unqualified = typename std::remove_reference::t template using Decay = typename std::decay::type; -namespace detail { -// code borrowed from Gears -// https://github.com/Rapptz/Gears/ -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 multi_return { typedef std::tuple type; @@ -81,9 +56,6 @@ 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 c986f73c..9c6af2f1 100644 --- a/tests.cpp +++ b/tests.cpp @@ -117,33 +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"].call() == 42); + REQUIRE(lua.get("a").call() == 42); REQUIRE_NOTHROW(lua.set_function("b", [ ] { return 42u; })); - REQUIRE(lua["b"].call() == 42u); + REQUIRE(lua.get("b").call() == 42u); REQUIRE_NOTHROW(lua.set_function("c", [ ] { return 3.14; })); - REQUIRE(lua["c"].call() == 3.14); + REQUIRE(lua.get("c").call() == 3.14); REQUIRE_NOTHROW(lua.set_function("d", [ ] { return 6.28f; })); - REQUIRE(lua["d"].call() == 6.28f); + REQUIRE(lua.get("d").call() == 6.28f); REQUIRE_NOTHROW(lua.set_function("e", [ ] { return "lol"; })); - REQUIRE(lua["e"].call() == lol); + REQUIRE(lua.get("e").call() == lol); REQUIRE_NOTHROW(lua.set_function("f", [ ] { return true; })); - REQUIRE(lua["f"].call()); + REQUIRE(lua.get("f").call()); REQUIRE_NOTHROW(lua.set_function("g", [ ] { return std::string("str"); })); - REQUIRE(lua["g"].call() == str); + REQUIRE(lua.get("g").call() == str); REQUIRE_NOTHROW(lua.set_function("h", [ ] { })); - REQUIRE_NOTHROW(lua["h"].call()); + REQUIRE_NOTHROW(lua.get("h").call()); REQUIRE_NOTHROW(lua.set_function("i", [ ] { return sol::nil; })); - REQUIRE(lua["i"].call() == 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((lua["j"].call() == heh_tuple)); + REQUIRE((lua.get("j").call() == heh_tuple)); } TEST_CASE("advanced/callLambda2", "A C++ lambda is exposed to lua and called") { @@ -236,52 +236,4 @@ 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); - - // 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