From dc2493e7525bb7633f697ef10f72b72b46222249 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Fri, 30 Aug 2019 00:45:36 -0400 Subject: Forget what I said, I just need to change git attributes to mark for vendor --- lib/sol2/examples/source/table_proxy.cpp | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lib/sol2/examples/source/table_proxy.cpp (limited to 'lib/sol2/examples/source/table_proxy.cpp') diff --git a/lib/sol2/examples/source/table_proxy.cpp b/lib/sol2/examples/source/table_proxy.cpp new file mode 100644 index 0000000..899143a --- /dev/null +++ b/lib/sol2/examples/source/table_proxy.cpp @@ -0,0 +1,55 @@ +#define SOL_ALL_SAFETIES_ON 1 +#include + +#include "assert.hpp" + +#include + +int main () { + + const auto& code = R"( + bark = { + woof = { + [2] = "arf!" + } + } + )"; + + sol::state lua; + lua.open_libraries(sol::lib::base); + lua.script(code); + + // produces proxy, implicitly converts to std::string, quietly destroys proxy + std::string arf_string = lua["bark"]["woof"][2]; + + // lazy-evaluation of tables + auto x = lua["bark"]; + auto y = x["woof"]; + auto z = y[2]; + + // retrivies value inside of lua table above + std::string value = z; + c_assert(value == "arf!"); + + // Can change the value later... + z = 20; + + // Yay, lazy-evaluation! + int changed_value = z; // now it's 20! + c_assert(changed_value == 20); + lua.script("assert(bark.woof[2] == 20)"); + + lua["a_new_value"] = 24; + lua["chase_tail"] = [](int chasing) { + int r = 2; + for (int i = 0; i < chasing; ++i) { + r *= r; + } + return r; + }; + + lua.script("assert(a_new_value == 24)"); + lua.script("assert(chase_tail(2) == 16)"); + + return 0; +} -- cgit v1.2.3