Using luabind wrapped types as keys into lua tables

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

Using luabind wrapped types as keys into lua tables

Greg Santucci
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?

Regards,
Greg Santucci


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

Re: Using luabind wrapped types as keys into lua tables

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

Re: Using luabind wrapped types as keys into lua tables

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