lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I've been thinking recently about how Lua bindings could be
generically designed but still as efficient as possible for handling
vector data in the general sense.  I have the situation where it is
necessary to design a point datatype in C++ as a class with an ID
associated with it.  I'd like to make the Lua bindings of the Point
class work with as wide a range of Lua modules as possible that deal
with 3D vector data such as LuaGL, LuaODE etc., but I also need to to
make sure the ID stays with the vector.  There are 2 possible ways to
go with this:

1) make a userdata from the Point class and create an __index
metamethod that responds appropriately to array indexing like a table
2) convert the C++ data to a Lua table when it is pushed into Lua as
so: { x, y, z, id = id}

Clearly there isn't much data here so the second option isn't overly
burdensome and I will automatically gain compatability wherever tables
as arrays are required as args, but thinking about the situation led
me to wonder what there was no natural number indexing equivalent with
metamethods to lua_rawgeti.  I see lua_getfield but making an integer
table index into a string argument seems way too much to go through.
Obviously I could push the number on the stack and do lua_get, but I
wanted to avoid the extra method call per array index and it seems
that the decision had been made in the API by virtue of lua_getfield
being there that this was something worth having at the expense of
growing the number of API functions.  To me it seems to be "symmetric"
with what already exists in the API and thus warranted, so I was
curious as to what, if any decisions had gone into not including it.

thanks for listening,
wes