Re: patch for direct access to STL containers from lua

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

Re: patch for direct access to STL containers from lua

Christophe Juniet
Hi Thomas,

First of all, thank you for your excellent work!
This is exactly what I was trying to achieve, except for a few things:
  - I need read/write access, mostly to vector, list and map.
  - I need to remove an element, by assigning nil to it for instance.
  - I must use multiple Lua states, most likely through different threads.

I've tried to modify your patch to address these points and I would be
very thankful if you could have a look at it.
Maybe you have already worked on these issues, in which case I would
be very interested to follow your progress and help you in any way.


Best Regards,
Christophe

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

luabindstl.zip (10K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: patch for direct access to STL containers from lua

Thomas Nelson-10
I will take a look at your code this weekend. I recently started a new job and that is eating a good deal of the time I had been spending on my game project (and even in my game project I've had to switch over to network coding mode and away from the script side)

Off the top of my head from your points:
- Speed wise, vector and map should be fine, std::lists however are more problematic.
I included them mostly as a proof of concept. A list does not allow for random access to its elements which means the only way to access the Nth element is to get the iterator from begin() and increment it N times.  I should be roundly flogged for writing that code even as a PoC.  My std::set implementation has the same issue.
-- Possible solutions: maintain a cursor or "current" iterator and adjust that with each access.

- Removing an element by assigning nil.  I'll have to refresh my memory but I'm not sure that is possible. I would recommend that you use shared pointers in your container to simulate this (and check for null on the C++ side).

- What method are you using to create your multiple states?

TomN

On 6/15/2010 5:33 AM, Christophe Juniet wrote:
Hi Thomas,

First of all, thank you for your excellent work!
This is exactly what I was trying to achieve, except for a few things:
  - I need read/write access, mostly to vector, list and map.
  - I need to remove an element, by assigning nil to it for instance.
  - I must use multiple Lua states, most likely through different threads.

I've tried to modify your patch to address these points and I would be
very thankful if you could have a look at it.
Maybe you have already worked on these issues, in which case I would
be very interested to follow your progress and help you in any way.


Best Regards,
Christophe
  
------------------------------------------------------------------------------ ThinkGeek and WIRED's GeekDad team up for the Ultimate GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the lucky parental unit. See the prize list and enter to win: http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________ luabind-user mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: patch for direct access to STL containers from lua

Christophe Juniet
Hi Thomas and thanks for your time!

> Off the top of my head from your points:
> - Speed wise, vector and map should be fine, std::lists however are more
> problematic.

True. I chose to use lists where I needed to be able to remove many elements.
Otherwise, I used mostly vector and map so it should be OK.

> - Removing an element by assigning nil.  I'll have to refresh my memory but
> I'm not sure that is possible. I would recommend that you use shared
> pointers in your container to simulate this (and check for null on the C++
> side).

Actually, I modified stl_container__newindex to call
container->erase(key) when value in nil.
Of course, the element is lost here and in the case of a pointer type,
you won't be able to delete it on the C++ side unless it's a smart
pointer or you keep another copy. Is it also what you meant?

> - What method are you using to create your multiple states?

I must create multiple instances of a class which has its own private lua_State.
In the constructor of this class, I create a new state with
luaL_newstate() and initialize it with luaL_openlib(),
luabind::open(), luabind::module(), luaL_dofile(), etc.
To make it works, I had to remove the use of static references.

Specifically, in stl_weak_registry_create(), I replaced:

        static int s_stl_weak_registry = LUA_REFNIL;
        ...
        s_stl_weak_registry = luaL_ref(L, LUA_REGISTRYINDEX);

with:

        static const char * container_reg_tag = "__luabind_stl_reg";
        ...
        lua_setfield(L, LUA_REGISTRYINDEX, container_reg_tag);

And in container_create_metatable(), I replaced:

       template<class Container_tt>
       int stl_adapter<Container_tt>::ref_stl_adapter = LUA_REFNIL;
       ...
       ref_stl_adapter = luaL_ref(L, LUA_REGISTRYINDEX);

with:

       template<class Container_tt>
       const std::string stl_adapter<Container_tt>::container_type_tag
= std::string("__luabind_stl_") + typeid(Container_tt).name();
       ...
       lua_setfield(L, LUA_REGISTRYINDEX, container_type_tag.c_str());

This way, I keep all the references on the registry in the states
themselves with (relatively) unique keys.

I'm attaching a sample to better illustrate my intended use of your work.
I've compiled it with GCC 4.4, boost 1.43, luabind 0.9 and my version
of your patch (the same as in my previous attachment).


Best Regards,
Christophe

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

simple.zip (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: patch for direct access to STL containers from lua

Wolfmanfx
Is it possible to iterate through maps and vectors?
Can you make an example where you iterate/insert/delete items from map/vector?
Also do we always need to pass map/vector instances as pointer? Is it possible to copy the content into a lua table (there is something here like copy:table)?
Christophe Juniet wrote
Hi Thomas and thanks for your time!

> Off the top of my head from your points:
> - Speed wise, vector and map should be fine, std::lists however are more
> problematic.

True. I chose to use lists where I needed to be able to remove many elements.
Otherwise, I used mostly vector and map so it should be OK.

> - Removing an element by assigning nil.  I'll have to refresh my memory but
> I'm not sure that is possible. I would recommend that you use shared
> pointers in your container to simulate this (and check for null on the C++
> side).

Actually, I modified stl_container__newindex to call
container->erase(key) when value in nil.
Of course, the element is lost here and in the case of a pointer type,
you won't be able to delete it on the C++ side unless it's a smart
pointer or you keep another copy. Is it also what you meant?

> - What method are you using to create your multiple states?

I must create multiple instances of a class which has its own private lua_State.
In the constructor of this class, I create a new state with
luaL_newstate() and initialize it with luaL_openlib(),
luabind::open(), luabind::module(), luaL_dofile(), etc.
To make it works, I had to remove the use of static references.

Specifically, in stl_weak_registry_create(), I replaced:

        static int s_stl_weak_registry = LUA_REFNIL;
        ...
        s_stl_weak_registry = luaL_ref(L, LUA_REGISTRYINDEX);

with:

        static const char * container_reg_tag = "__luabind_stl_reg";
        ...
        lua_setfield(L, LUA_REGISTRYINDEX, container_reg_tag);

And in container_create_metatable(), I replaced:

       template<class Container_tt>
       int stl_adapter<Container_tt>::ref_stl_adapter = LUA_REFNIL;
       ...
       ref_stl_adapter = luaL_ref(L, LUA_REGISTRYINDEX);

with:

       template<class Container_tt>
       const std::string stl_adapter<Container_tt>::container_type_tag
= std::string("__luabind_stl_") + typeid(Container_tt).name();
       ...
       lua_setfield(L, LUA_REGISTRYINDEX, container_type_tag.c_str());

This way, I keep all the references on the registry in the states
themselves with (relatively) unique keys.

I'm attaching a sample to better illustrate my intended use of your work.
I've compiled it with GCC 4.4, boost 1.43, luabind 0.9 and my version
of your patch (the same as in my previous attachment).


Best Regards,
Christophe

 
------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
luabind-user mailing list
luabind-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/luabind-user