I'm working on integrating Lua with C++. I want to be able to expose C++ values to Lua in a hierarchical way that may not be exactly how the values are stored on the C++ side. I want to be able to do something like
a.b.c = 5
print(a.b.c)
and the a function on the C++ side would receive 'a', 'b', 'c' and 5 for the first case and just 'a', 'b', 'c' for the second case. To do this, I've created a table that holds the keys and a metatable whose __index and __newindex methods take care of the logic, right now it looks something like this:
metatable.__index = function(t, k)
rawset(t, t.index, k)
t.index = t.index + 1
return t
end
metatable.__newindex = function(t, k, v)
rawset(t, t.index, k)
return newindexCallback(t, v)
end
This works fine for setting values, but getting values doesn't work because a.b.c results in a table, not the actual value. Is there a way for the __index method to know that it's the last one in a chain? Or another metamethod that is called when a value is actually used? Or do I have to resort to using getters and setters?