Getting a C++ object back from a luabind::object?

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

Getting a C++ object back from a luabind::object?

Josh Klint

Here’s my function to push a C++ object onto the Lua stack.  I use a base class all classes are derived from, called “Object”.  Each Object gets a luabind::object associated with it when it is pushed onto the stack.  The luabind::object is stored in an std::map so it can be retrieved quickly:

 

void Interpreter::PushObject(Object* o)

{

      if (objectmap[o]==NULL) objectmap[o] = new luabind::object(L,o);

      objectmap[o]->push(L);

}

 

I can retrieve a luabind::object off the stack.  But how do I retrieve the original Object* I used in the luabind::object constructor?:

 

Object* Interpreter::ToObject(const int& index)

{

      luabind::object o = luabind::object(L,index);

      return (Object*) o.what?

}

 

Incidentally, the reason the luabind::object is stored in an std::map instead of as a member of the Object class is because I want to keep all the Lua code contained within the interpreter, as we have some customers that want to replace the interpreter with alternate script languages.

 

Thanks.

 

 

Best Regards,

 

Josh Klint

CEO

Leadwerks Software

www.leadwerks.com

 


------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its
next-generation tools to help Windows* and Linux* C/C++ and Fortran
developers boost performance applications - including clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Getting a C++ object back from a luabind::object?

Reko Tiira
I think what you want is:

return luabind::touserdata<Object>(o);

Regards,
Reko Tiira

------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its
next-generation tools to help Windows* and Linux* C/C++ and Fortran
developers boost performance applications - including clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Getting a C++ object back from a luabind::object?

Iliya Trendafilov
Or

return luabind::object_cast<Object*>(o)

On Wed, May 18, 2011 at 11:34 AM, Reko Tiira <[hidden email]> wrote:
I think what you want is:

return luabind::touserdata<Object>(o);

Regards,
Reko Tiira

------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its
next-generation tools to help Windows* and Linux* C/C++ and Fortran
developers boost performance applications - including clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its
next-generation tools to help Windows* and Linux* C/C++ and Fortran
developers boost performance applications - including clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Getting a C++ object back from a luabind::object?

Josh Klint
In reply to this post by Reko Tiira
Here's what I'm trying:

void Interpreter::PushObject(Object* o)
{
        if (objectmap[o]==NULL) objectmap[o] = new luabind::object(L,o);
        objectmap[o]->push(L);
}
               
Object* Interpreter::ToObject(const int& index)
{
        luabind::object o = luabind::object(L,index);
        return luabind::touserdata<Object>(o);
}

But if I use this code:
Entity* entity = new Entity; //The class "Entity" is derived from "Object"
PushObject(entity);
entity = (Entity*)ToObject(-1);

The value returned from ToObject() is NULL.


Best Regards,

Josh Klint
CEO
Leadwerks Software
www.leadwerks.com

-----Original Message-----
From: Reko Tiira [mailto:[hidden email]]
Sent: Wednesday, May 18, 2011 1:35 AM
To: [hidden email]
Subject: Re: [luabind] Getting a C++ object back from a luabind::object?

I think what you want is:

return luabind::touserdata<Object>(o);

Regards,
Reko Tiira

----------------------------------------------------------------------------
--
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its
next-generation tools to help Windows* and Linux* C/C++ and Fortran
developers boost performance applications - including clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its
next-generation tools to help Windows* and Linux* C/C++ and Fortran
developers boost performance applications - including clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Getting a C++ object back from a luabind::object?

Nigel Atkinson-2
Perhaps

return luabind::touserdata<Object*>(o);

instead?  I.e. cast to a pointer rather than an object it self.  Perhaps
underneath, it uses dynamic_cast.

I haven't used luabind::touserdata before though - I'm just guessing.

Personally I would have gone with

return luabind::object_cast<Object*>(o);

Nigel

On Wed, 2011-05-18 at 09:26 -0700, Josh Klint wrote:

> Here's what I'm trying:
>
> void Interpreter::PushObject(Object* o)
> {
> if (objectmap[o]==NULL) objectmap[o] = new luabind::object(L,o);
> objectmap[o]->push(L);
> }
>
> Object* Interpreter::ToObject(const int& index)
> {
> luabind::object o = luabind::object(L,index);
> return luabind::touserdata<Object>(o);
> }
>
> But if I use this code:
> Entity* entity = new Entity; //The class "Entity" is derived from "Object"
> PushObject(entity);
> entity = (Entity*)ToObject(-1);
>
> The value returned from ToObject() is NULL.
>
>
> Best Regards,
>
> Josh Klint
> CEO
> Leadwerks Software
> www.leadwerks.com
>
> -----Original Message-----
> From: Reko Tiira [mailto:[hidden email]]
> Sent: Wednesday, May 18, 2011 1:35 AM
> To: [hidden email]
> Subject: Re: [luabind] Getting a C++ object back from a luabind::object?
>
> I think what you want is:
>
> return luabind::touserdata<Object>(o);
>
> Regards,
> Reko Tiira
>
> ----------------------------------------------------------------------------
> --
> What Every C/C++ and Fortran developer Should Know!
> Read this article and learn how Intel has extended the reach of its
> next-generation tools to help Windows* and Linux* C/C++ and Fortran
> developers boost performance applications - including clusters.
> http://p.sf.net/sfu/intel-dev2devmay
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>
> ------------------------------------------------------------------------------
> What Every C/C++ and Fortran developer Should Know!
> Read this article and learn how Intel has extended the reach of its
> next-generation tools to help Windows* and Linux* C/C++ and Fortran
> developers boost performance applications - including clusters.
> http://p.sf.net/sfu/intel-dev2devmay
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user



------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its
next-generation tools to help Windows* and Linux* C/C++ and Fortran
developers boost performance applications - including clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Getting a C++ object back from a luabind::object?

Josh Klint
Your suggestion gives me the following error in VS 2008:
"Unhandled exception at 0x7c812afb in LE3.exe: Microsoft C++ exception:
luabind::cast_failed at memory location 0x0012f894..."

Object* Interpreter::ToObject(const int& index)
{
        luabind::object o = luabind::object(L,index);
        //return luabind::touserdata<Object>(o);
        return luabind::object_cast<Object*>(o);
}


Best Regards,

Josh Klint
CEO
Leadwerks Software
www.leadwerks.com

-----Original Message-----
From: Nigel Atkinson [mailto:[hidden email]]
Sent: Wednesday, May 18, 2011 4:31 PM
To: [hidden email]
Subject: Re: [luabind] Getting a C++ object back from a luabind::object?

Perhaps

return luabind::touserdata<Object*>(o);

instead?  I.e. cast to a pointer rather than an object it self.  Perhaps
underneath, it uses dynamic_cast.

I haven't used luabind::touserdata before though - I'm just guessing.

Personally I would have gone with

return luabind::object_cast<Object*>(o);

Nigel

On Wed, 2011-05-18 at 09:26 -0700, Josh Klint wrote:

> Here's what I'm trying:
>
> void Interpreter::PushObject(Object* o)
> {
> if (objectmap[o]==NULL) objectmap[o] = new luabind::object(L,o);
> objectmap[o]->push(L);
> }
>
> Object* Interpreter::ToObject(const int& index)
> {
> luabind::object o = luabind::object(L,index);
> return luabind::touserdata<Object>(o);
> }
>
> But if I use this code:
> Entity* entity = new Entity; //The class "Entity" is derived from "Object"
> PushObject(entity);
> entity = (Entity*)ToObject(-1);
>
> The value returned from ToObject() is NULL.
>
>
> Best Regards,
>
> Josh Klint
> CEO
> Leadwerks Software
> www.leadwerks.com
>
> -----Original Message-----
> From: Reko Tiira [mailto:[hidden email]]
> Sent: Wednesday, May 18, 2011 1:35 AM
> To: [hidden email]
> Subject: Re: [luabind] Getting a C++ object back from a luabind::object?
>
> I think what you want is:
>
> return luabind::touserdata<Object>(o);
>
> Regards,
> Reko Tiira
>
>
----------------------------------------------------------------------------

> --
> What Every C/C++ and Fortran developer Should Know!
> Read this article and learn how Intel has extended the reach of its
> next-generation tools to help Windows* and Linux* C/C++ and Fortran
> developers boost performance applications - including clusters.
> http://p.sf.net/sfu/intel-dev2devmay
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>
>
----------------------------------------------------------------------------
--
> What Every C/C++ and Fortran developer Should Know!
> Read this article and learn how Intel has extended the reach of its
> next-generation tools to help Windows* and Linux* C/C++ and Fortran
> developers boost performance applications - including clusters.
> http://p.sf.net/sfu/intel-dev2devmay
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user



----------------------------------------------------------------------------
--
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its
next-generation tools to help Windows* and Linux* C/C++ and Fortran
developers boost performance applications - including clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its
next-generation tools to help Windows* and Linux* C/C++ and Fortran
developers boost performance applications - including clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user