Register an allocated object

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

Register an allocated object

Thales Luis Rodrigues Sabino
Is there a way to register an existing object within lua? 

For instance.

MyClass *a = new MyClass();
a->myMember = 30;

Is there a way to register object a so in lua use it like

print(a.myMember)?

Thanks

--
Anyday, anytime.

http://www.capimlokura.com.br

Thales Luis Rodrigues Sabino
TLuisRS

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Register an allocated object

Nigel Atkinson-2
Hi there!

Short answer: Yes.

However it depends on who should 'own' the object and destruct it when the time comes. This is how I do it, however someone may have a better alternative.

First the easy way, where the class instance is still 'owned' by the C++ side of things:

Assuming your example code and 'MyClass' has been bound....

globals(L)["a"] = a; // Easy huh?

It gets tricky should you want the Lua script take responsibility of the instance and destruct it when finished with it.  You'll need something like:

Lua:
function setglobal( key, value )
_G[key] = value
end

C++:
object setglobal = globals(L)["setglobal"];

setglobal( "a", a )[adopt(_2]; // Kinda roundabout but works.

I'm not sure if there is another way to have Lua 'adopt' the value.  I haven't really looked very hard though ;-)

Regards,

Nigel Atkinson


On 30/04/2012, at 1:10 PM, Thales Luis Rodrigues Sabino wrote:

Is there a way to register an existing object within lua? 

For instance.

MyClass *a = new MyClass();
a->myMember = 30;

Is there a way to register object a so in lua use it like

print(a.myMember)?

Thanks

--
Anyday, anytime.

http://www.capimlokura.com.br

Thales Luis Rodrigues Sabino
TLuisRS
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Register an allocated object

Thales Luis Rodrigues Sabino
Thanks, that is exactly what I'm looking. For now, I'm not interested in destroy the object within lua, just let it available for modifications so I can get a console tool for my application.

On Mon, Apr 30, 2012 at 2:26 AM, Nigel Atkinson <[hidden email]> wrote:
Hi there!

Short answer: Yes.

However it depends on who should 'own' the object and destruct it when the time comes. This is how I do it, however someone may have a better alternative.

First the easy way, where the class instance is still 'owned' by the C++ side of things:

Assuming your example code and 'MyClass' has been bound....

globals(L)["a"] = a; // Easy huh?

It gets tricky should you want the Lua script take responsibility of the instance and destruct it when finished with it.  You'll need something like:

Lua:
function setglobal( key, value )
_G[key] = value
end

C++:
object setglobal = globals(L)["setglobal"];

setglobal( "a", a )[adopt(_2]; // Kinda roundabout but works.

I'm not sure if there is another way to have Lua 'adopt' the value.  I haven't really looked very hard though ;-)

Regards,

Nigel Atkinson


On 30/04/2012, at 1:10 PM, Thales Luis Rodrigues Sabino wrote:

Is there a way to register an existing object within lua? 

For instance.

MyClass *a = new MyClass();
a->myMember = 30;

Is there a way to register object a so in lua use it like

print(a.myMember)?

Thanks

--
Anyday, anytime.

http://www.capimlokura.com.br

Thales Luis Rodrigues Sabino
TLuisRS
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user




--
Anyday, anytime.

http://www.capimlokura.com.br

Thales Luis Rodrigues Sabino
TLuisRS

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user