Re: Using luabind wrapped types as keys into lua tables
Daniel Wallin <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
On Thu, Mar 04, 2010 at 02:28:41PM +1100, Greg Santucci wrote:
> Any type can be used as a key in a lua table, so I'd like to use
> references to luabind wrapped types. The problem is that different lua
> variables pointing to the same luabind C++ data are going to be seen
> as different keys from the point of view of a lua table. For example:
>
> mypin = c.pins:at(0)
> mypin2 = c.pins:at(0)
>
> pinmap = {}
> pinmap[mypin] = "hello"
> print(pinmap[mypin2]) -- Results in (null) printed.
>
> -- But:
>
> mypin = c.pins:at(0)
> mypin2 = mypin
> pinmap[mypin] = "hello"
> pinr(pinmap[mypin2]) -- Results in "hello" printed, no problem
>
> mypin and mypin2 can't be used as the same key from the point of view
> of a lua table.
>
> Is there a way to get to the raw data pointer, so that can be used as a key
> instead?
You could write a small function that returns it:
void get_raw_key(lua_State* L, pin const& p)
{
lua_pushlightuserdata(L, &p);
}
...
module(L) [
class_<pin>("pin")
.def("raw_key", &get_raw_key)
];
--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com
------------------------------------------------------------------------------
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