From ac975872ad9d6faf470e962e76017cd2b4ccfbce Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 16 Dec 2013 11:07:10 -0500 Subject: [PATCH 1/2] 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 From afe435910fe2d0641cb672f7ad347f4e8fb1880f Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 16 Dec 2013 11:17:33 -0500 Subject: [PATCH 2/2] Add basic examples directory --- README.md | 35 ++--------------------------------- examples/config.cpp | 27 +++++++++++++++++++++++++++ examples/config.lua | 3 +++ 3 files changed, 32 insertions(+), 33 deletions(-) create mode 100644 examples/config.cpp create mode 100644 examples/config.lua diff --git a/README.md b/README.md index 8d6887b8..c566df1f 100644 --- a/README.md +++ b/README.md @@ -15,40 +15,9 @@ It should be noted that the library itself depends on `lua.hpp` to be found by y If you want to contribute, please check CONTRIBUTING.md for details. Thank you! -## Example +## Examples -Here's an example on how to load a basic configuration struct with a Lua script. - -```cpp -#include -#include -#include - -struct test { - int foo; - std::string bar; - double baz; -}; - -test load(const sol::table& t) { - return { t.get("foo"), t.get("bar"), t.get("baz") }; -} - -int main() { - try { - sol::state lua; - lua.script("foo = 1234;\n" - "bar = \"hello world\";\n" - "baz = 1.4;"); - - test c = load(lua.global_table()); - std::cout << '(' << c.foo << ", " << c.bar << ", " << c.baz << ")\n"; - } - catch(const std::exception& e) { - std::cerr << e.what() << '\n'; - } -} -``` +Examples are provided in the examples directory. ## License diff --git a/examples/config.cpp b/examples/config.cpp new file mode 100644 index 00000000..fb92eb68 --- /dev/null +++ b/examples/config.cpp @@ -0,0 +1,27 @@ +// shows how to load basic data to a struct + +#include +#include +#include + +struct config { + std::string name; + int width; + int height; + + void print() { + std::cout << "Name: " << name << '\n' + << "Width: " << width << '\n' + << "Height: " << height << '\n'; + } +}; + +int main() { + sol::state lua; + config screen; + lua.open_file("config.lua"); + screen.name = lua.get("name"); + screen.width = lua.get("width"); + screen.height = lua.get("height"); + screen.print(); +} diff --git a/examples/config.lua b/examples/config.lua new file mode 100644 index 00000000..7d84f017 --- /dev/null +++ b/examples/config.lua @@ -0,0 +1,3 @@ +name = "Asus" +width = 1920 +height = 1080