patch for direct access to STL containers from lua
I guess the good thing about being laid off is I have time to work on
fun stuff like this.
This is a patch that provides direct read only access to STL containers
from Lua. The containers must be passed by pointer into the Lua VM as
the return value from a function. I have intentionally omitted the
match() and lua->cpp apply() from the template to discourage use as
parameters (I hope that is sufficient). There are stubs for that
functionality but there are big nasty fuzzy dragons living there.
I left the __newindex code in as well but it has been commented out
(it's in stl_container_adapter.hpp), but again, down that path lies only
Iteration on containers requires next() and pairs() be aware of the
__next and __index metamethods and so these functions should be patched
in any code that uses these adapters (see documentation that follows).
And as I mentioned in the earlier post the # operator doesn't honor the
__len method for tables. I list a work around in the docs.
These converters provide direct read-only access to the STL collection
classes presenting them to Lua as tables. Identity is preserved
so that weak reference so that as long as Lua has a reference to the
collection in question the following will always hold true:
tab1 = myObj.Collection;
tab2 = myObj.Collection;
tab1 == tab2;
The converters should not interact with collections passed as
only return values from functions.
The # operator:
The # operator does not correctly check the __len metamethod for tables
in Lua 5.1, to get the length of the table get its metatable and
__len method directly as follows:
local mt = getmetatable(table);
length = mt.__len(table);
In order to correctly iterate a table Lua needs to be taught to use
__next and __index metamethods. And pairs needs to be expressed in
of next. The following code should be somewhere in your Lua program:
rawnext = next;
local m = getmetatable(t);
local n = m and m.__next or rawnext;
return next, t, nil;
vector: keys are the index in the array. Indexes are adjusted
1 based in keeping with Lua conventions.
In other words:
c++ vector is equivalent to Lua table
deque: same as vector
list: same as vector
set: STL sets are inherently ordered. The key is the set
position in the set. (1 based)