patch for direct access to STL containers from lua

Thomas Nelson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
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 
madness.

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.

---------
Docs:
--------
These converters provide direct read-only access to the STL collection
     classes presenting them to Lua as tables. Identity is preserved 
using a,
     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 
parameters,
     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 
call the
     __len method directly as follows:

     local mt = getmetatable(table);
     length = mt.__len(table);

     Iteration:
     In order to correctly iterate a table Lua needs to be taught to use 
the
     __next and __index metamethods. And pairs needs to be expressed in 
terms
     of next. The following code should be somewhere in your Lua program:

     rawnext = next;
     function next(t,k)
         local m = getmetatable(t);
         local n = m and m.__next or rawnext;
         return n(t,k);
     end

     function pairs(t)
         return next, t, nil;
     end

     Supported collections:
         vector: keys are the index in the array.  Indexes are adjusted 
to be
                 1 based in keeping with Lua conventions.
             In other words:
             c++ vector[0] is equivalent to Lua table[1]

         deque:    same as vector

         list:    same as vector

         set:    STL sets are inherently ordered.  The key is the set 
member's
                 position in the set. (1 based)

         multiset:    same as set.

         map:    key/value pairs ordered by the key

     Usage:
     c++:
         class MyObject
         {

             std::vector<std::string> m_MyVector;

             const std::vector<std::string> *getVector() const
             {    return &m_MyVector;
             }

             void bindScripting(lua_State *L)
             {
                 using namespace luabind;
                 module(pState)
                 [
                     class_<MyObject>("MyObject")
                         .property("Vector", &MyObject::getVector)
                 ];
             }
         };

     Lua:
         -- don't forget the "next" and "pairs" changes above
         obj = MyObject;
         tbl = obj.Vector;

         for k, v in pairs(tbl) do
             print( 'tbl[' .. k .. '] = ' .. v );
         end

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
luabindstl.zip (application/octet-stream, 7.7 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.