Re: Remove Luabind ownership on C++ pointer?
Iliya Trendafilov <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
Yeah, my bad, free function is what I meant. Thanks for the correction :) On Thu, Sep 30, 2010 at 3:06 PM, Nigel Atkinson <[email protected]>wrote: > A factory function works well, and also works well for singleton classes > or other times where you need more control over creation and deletion. > > In order to use them however, you have to bind them as a static (either > free or a method), so that you don't need a existing object to call it > on. > > e.g > // Free style > module(lua) > [ > class_<Object>("Object"), > def("CreateObject", &CreateObject) > ]; > > // Method style > module(lua) > [ > class_<Object>("Object") > .scope > [ > def("Create", &CreateObject) > ] > ]; > > obj = CreateObject() // Free function > > obj = Object.Create() // Static method, notice the '.' > > > > On Thu, 2010-09-30 at 14:26 +0200, Iliya Trendafilov wrote: > > Another option is to register a factory function instead of > > constructor. E.g. > > > > C++ > > Object* CreateObject() { return new Object(); } > > > > > > module(lua) > > > > [ > > > > class_<Object>("Object") > > > > .def("CreateObject", &CreateObject) > > > > > > ]; > > > > > > > > Lua: > > > > local object = CreateObject(); > > > > > > You'll be solely responsible for deleting these objects in that case. > > > > > > Another thing I did once is to keep a Lua table with all created > > instances. In Lua: > > > > > > function CreateObject(...) > > > > objects = objects or {} > > > > > > local object = Object() > > > > table.insert(objects, object) > > > > > > return object > > > > > > end > > > > > > and then create all your instances through that function. > > > > > > I'll leave you to decide which of the methods is the most elegant :) > > > > > > > > On Thu, Sep 30, 2010 at 1:30 PM, eduard mueller > > <[email protected]> wrote: > > Hold the object in a boost::shared_ptr or some other reference > > counting smart_ptr in Lua: > > class_<Object, boost::shared_ptr<Object> >("Object") > > > > Then the object will be kept alive as long as another > > shared_ptr uses > > the object in C++. Aka, the instance that is created in Lua > > will only > > release the shared pointer, not destruct the object itself > > when > > garbage collected. > > > > Best regards, > > Eduard > > > > > > > > On Thu, Sep 30, 2010 at 05:16, neosettlers > > <[email protected]> wrote: > > > Greetings, > > > > > > > > > > > > I’m wondering how to remove Luabind ownership on C++ > > pointers: > > > > > > > > > > > > C++ > > > > > > module(lua) > > > > > > [ > > > > > > class_<Object>("Object") > > > > > > .def(constructor<>()) > > > > > > ]; > > > > > > > > > > > > .lua > > > > > > local object = Object(); > > > > > > > > > > > > … > > > > > > > > > > > > I would like Luabind NOT to delete the object C++ pointer > > when lua_gc() is > > > called. > > > > > > > > > > > > If anyone found an elegant solution for this, I’m definitely > > interested to > > > hear about it. > > > > > > > > > > > > Thx, > > > > > > > > > > > > > > > > > > > > > > > ------------------------------------------------------------------------------ > > > Start uncovering the many advantages of virtual appliances > > > and start using them to simplify application deployment and > > > accelerate your shift to cloud computing. > > > http://p.sf.net/sfu/novell-sfdev2dev > > > _______________________________________________ > > > luabind-user mailing list > > > [email protected] > > > https://lists.sourceforge.net/lists/listinfo/luabind-user > > > > > > > > > > > ------------------------------------------------------------------------------ > > Start uncovering the many advantages of virtual appliances > > and start using them to simplify application deployment and > > accelerate your shift to cloud computing. > > http://p.sf.net/sfu/novell-sfdev2dev > > _______________________________________________ > > luabind-user mailing list > > [email protected] > > https://lists.sourceforge.net/lists/listinfo/luabind-user > > > > > ------------------------------------------------------------------------------ > > Start uncovering the many advantages of virtual appliances > > and start using them to simplify application deployment and > > accelerate your shift to cloud computing. > > http://p.sf.net/sfu/novell-sfdev2dev > > _______________________________________________ luabind-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/luabind-user > > > > > ------------------------------------------------------------------------------ > Start uncovering the many advantages of virtual appliances > and start using them to simplify application deployment and > accelerate your shift to cloud computing. > http://p.sf.net/sfu/novell-sfdev2dev > _______________________________________________ > luabind-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/luabind-user > ------------------------------------------------------------------------------ Start uncovering the many advantages of virtual appliances and start using them to simplify application deployment and accelerate your shift to cloud computing. http://p.sf.net/sfu/novell-sfdev2dev _______________________________________________ luabind-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/luabind-user