Re: patch for direct access to STL containers from lua
Christophe Juniet <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
simple.zip
(application/zip, 1.4 KB) - not displayed