STL table using metatable to access

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

STL table using metatable to access

Thomas Nelson-10
Before I dive into this too far, has anyone thought of/implemented using
a metatable directly access a C++ STL container as a lua table?

Correct me if I'm wrong but container_policy.hpp just copies the values
of the existing table into a lua table and returns that.  Couldn't we
apply a meta table with "__index" "__newindex" and "__len" methods
specialized to the container type.

TomN


------------------------------------------------------------------------------
Download Intel® 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Proof of concept Re: STL table using metatable to access

Thomas Nelson-10
This code is still WAY preliminary but I wanted to throw it out there as
a proof of concept and see if anybody had any thoughts on it.  I've not
even tested it beyond checking to see if I can access the items in the
table.

It consists of two basic parts, a converter for translating a pointer to
an STL collection into a lua table and an adapter that uses meta table
methods to call the underlying STL collection access methods. There is
also an attempt to use a weak table to preserve identity but I'm not
convinced that works yet.

Outstanding issues:
- This has only been specialized for std::vector at this point.  I've
kept the metamethods split out from the adapter so they can be
specialized individually.
- Lua 5.1 does not respect the __length metamethod for tables (this
appears to have been fixed for 5.2)  Additionally I'm not sure what the
right behavior should be since #table is defined to be the index of the
last non nil entry and if I have a vector of pointers some of which are
NULL, returning the length of the vector would violate the rule.
- [i]pairs and [i]next.  Lua does not directly support meta methods for
these and inext calls rawgeti and so does not call the __index
metamethod.  I've found a patch/extension here
(http://lua-users.org/wiki/GeneralizedPairsAndIpairs) that fixes this
for inext, but I don't know if that's a good final solution.
- I'm storing some book-keeping info in the table itself... I've not
decided yet if that's a good idea or not.
- The identity code isn't quite right yet.  I should be able to do a =
source; b = source; a == b and get back true.
- It sure as heck isn't thread safe.

Things I'd like to add someday...
- a const pointer will create a read only table
- triggers on add/set (rather than putting values directly into the
tables.)

TomN

On 3/25/2010 1:25 PM, Thomas Nelson wrote:

> Before I dive into this too far, has anyone thought of/implemented using
> a metatable directly access a C++ STL container as a lua table?
>
> Correct me if I'm wrong but container_policy.hpp just copies the values
> of the existing table into a lua table and returns that.  Couldn't we
> apply a meta table with "__index" "__newindex" and "__len" methods
> specialized to the container type.
>
> TomN
>    

------------------------------------------------------------------------------
Download Intel® 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

stl_container_converter.hpp (1K) Download Attachment
stl_container_adapter.hpp (8K) Download Attachment
stl_container_adapter.cpp (1K) Download Attachment