Re: Using luabind wrapped types as keys into lua tables

Greg Santucci <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
That does the trick. I templated it so I can apply it to other classes:

template <class T>
 void GetRawKey(lua_State* L, const T & p)
 {
     lua_pushlightuserdata(L, (void *)&p);
 }

Thanks!

On Thu, Mar 4, 2010 at 6:29 PM, Daniel Wallin <[email protected]> wrote:

> 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&#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
>

------------------------------------------------------------------------------
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
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.