[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Working with table from C++
- From: Eugen-Andrei Gavriloaie <shiretu@...>
- Date: Wed, 9 Apr 2008 04:31:58 +0300
It might sound stupid, but it seems that I can't find a way to iterate
through a table in c++. Suppose we have a table pushed onto the stack
(lua calls a c++ function and passes all the parameters using a table:
key/value pairs). How do I iterate on that table (present on the top
of stack) in c++? I need both elements (key and value).
Please don't make fun of me now because I think I'll try to scratch
with my left on the right side of my head .... :)
One approach would be to globalize the table from the stack by giving
it a name using lua_setglobal and after that evaluate a generated lua
script like this:
for k,v in pairs(_some_random_name) do
evaluation is made using lua_loadstring()/lua_pcall()
After this, we can use lua_setglobal again to set `_some_random_name`
to nil and make the table available for GC.
But this is a huuuuge implementation overhead IMHO, because you must
keep a state in c++ outside the function
(store the key/value pair (now present on the stack like 2 distinct
elements, easy to pick up) into a more c++ manageable form like a
vector or a map). Is there another better solution to this problem?