mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Add support for const table operator[] retrieval
This commit is contained in:
parent
b2e83d4fca
commit
ebbceeb9e2
|
@ -41,7 +41,7 @@ T* get_ptr(T* val) {
|
|||
} // detail
|
||||
|
||||
class table : virtual public reference {
|
||||
template<typename T> struct proxy;
|
||||
template<typename T, typename U> struct proxy;
|
||||
public:
|
||||
table() noexcept : reference() {}
|
||||
table(lua_State* L, int index = -1) : reference(L, index) {
|
||||
|
@ -82,8 +82,13 @@ public:
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
proxy<T> operator[](T&& key) {
|
||||
return proxy<T>(*this, std::forward<T>(key));
|
||||
auto operator[](T&& key) -> decltype(proxy<decltype(*this), T>(*this, std::forward<T>(key))) {
|
||||
return proxy<decltype(*this), T>(*this, std::forward<T>(key));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
auto operator[](T&& key) const -> decltype(proxy<decltype(*this), T>(*this, std::forward<T>(key))) {
|
||||
return proxy<decltype(*this), T>(*this, std::forward<T>(key));
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
|
@ -91,13 +96,13 @@ public:
|
|||
return lua_rawlen(state(), -1);
|
||||
}
|
||||
private:
|
||||
template<typename T>
|
||||
template<typename Table, typename T>
|
||||
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<typename U>
|
||||
EnableIf<Function<Unqualified<U>>> operator=(U&& other) {
|
||||
|
|
|
@ -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<int>(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()));
|
||||
}
|
Loading…
Reference in New Issue
Block a user