From ebbceeb9e2f6a8f929e089d6182ab14bc24b5be6 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Fri, 13 Dec 2013 15:40:20 -0500 Subject: [PATCH] Add support for const table operator[] retrieval --- sol/table.hpp | 17 +++++++++++------ tests.cpp | 9 +++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/sol/table.hpp b/sol/table.hpp index 462bb34e..4a444e52 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -41,7 +41,7 @@ T* get_ptr(T* val) { } // detail class table : virtual public reference { - template struct proxy; + template struct proxy; public: table() noexcept : reference() {} table(lua_State* L, int index = -1) : reference(L, index) { @@ -82,8 +82,13 @@ public: } template - proxy operator[](T&& key) { - return proxy(*this, std::forward(key)); + auto operator[](T&& key) -> decltype(proxy(*this, std::forward(key))) { + return proxy(*this, std::forward(key)); + } + + template + auto operator[](T&& key) const -> decltype(proxy(*this, std::forward(key))) { + return proxy(*this, std::forward(key)); } size_t size() const { @@ -91,13 +96,13 @@ public: return lua_rawlen(state(), -1); } private: - template + template struct proxy { private: - table& t; + Table t; T& key; public: - proxy(table& t, T& key) : t(t), key(key) {} + proxy(Table t, T& key) : t(t), key(key) {} template EnableIf>> operator=(U&& other) { diff --git a/tests.cpp b/tests.cpp index 10a57b80..a24e92a4 100644 --- a/tests.cpp +++ b/tests.cpp @@ -233,4 +233,13 @@ TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works // 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