Re: [luabind] weak_ref issue patch
Brought to you by:
arvidn,
daniel_wallin
From: Christian <neu...@ya...> - 2013-07-23 10:17:49
|
On Tuesday 23 July 2013, at 12:00:29, Glen Fraser <hol...@gm...> wrote: > > The struct name "only_accepts_nonconst_pointers" is meant as an error > > message: adopt only works with non-const pointers (meaning that the > > adopted type must be both a pointer and a non-const one). > > Thanks, Christian -- I saw that, but I don't know how to fix it. Presumably > this is related to the fact that the object I'm trying to "adopt" is held > in a smart pointer (boost::shared_ptr). But I haven't declared the > smart_ptr as const anywhere… > Adopt checks exactly for const plain pointers and will issue said error message otherwise. And if you think about it, it makes sense: Plain pointers do not have any particular ownership semantics, so you must tell luabind about it using policies such as adopt. Smart pointers on the other hand, do already have ownership semantics: shared_ptr e.g. has shared ownership. > I declare my class binding like this: > > class_<Actor, Actor_wrapper, boost::shared_ptr<Actor> >("Actor") > .def(constructor<const string &>()) > .def("draw", &Actor::draw, &Actor_wrapper::default_draw) > ]; > > And the function I'm trying to call from C++ is like this: > > boost::shared_ptr<Actor> fromLua = > luabind::call_function<boost::shared_ptr<Actor> >(mLua, > "makeActorSubclass", "bobby")[luabind::adopt(luabind::result)]; > > The function makeActorSubclass is a simple Lua script that creates an > instance of a class derived from Actor in Lua (this creation code works > fine without the adopt policy stuff...except it doesn't transfer ownership > to C++, so the object gets garbage-collected later on!). > > function makeActorSubclass(name) > return ActorSubclass(name) > end > So the lua part of your object get's collected? I think I had a similar problem, which I solved using a hack in my wrapper class, by doing the (here) crucial part of adopt manually using a member function like this: > Any ideas on how to get rid of my const problem? I do see the following > comment in the luabind docs: > > get_const_holder() has been removed. Automatic conversions between > smart_ptr<X>and smart_ptr<X const> no longer work. > > But I'm not sure what that means, and whether it applies in my case (I don't > have any smart_ptr<X const> usages that I know of). > > Glen. > > On Jul 23, 2013, at 10:55 AM, Christian <neu...@ya...> wrote: > > On Tuesday 23 July 2013, at 10:11:13, Glen Fraser <hol...@gm...> wrote: > >> Now, when I try to use the adopt policy, I get errors like Sympaval had > >> (in > >> this thread: > >> http://lua.2524044.n2.nabble.com/Remove-an-object-from-the-lua-state-tp75 > >> 83 > >> 423p7583425.html). He didn't manage to get adopt working when binding, > >> but > >> rather chose to instead use it with using call_function. > >> > >> Same as him -- I get errors like this, when trying to use adopt(_2) in a > >> def(): > >> > >> .../boost/include/boost/preprocessor/iteration/detail/local.hpp:37:9: No > >> member named 'consumed_args' in 'luabind::detail::adopt_policy<2, > >> void>::only_accepts_nonconst_pointers' > >> .../boost/include/boost/preprocessor/iteration/detail/local.hpp:37:9: No > >> member named 'converter_postcall' in 'luabind::detail::adopt_policy<2, > >> void>::only_accepts_nonconst_pointers' > >> .../addons/ofxLua/src/luabind/luabind/detail/call.hpp:295:48: No member > >> named 'apply' in 'luabind::detail::adopt_policy<2, > >> void>::only_accepts_nonconst_pointers' > >> > >> And when I try to use it with call_function: > >> > >> .../addons/ofxLua/src/luabind/luabind/detail/call_function.hpp:200:20: No > >> member named 'match' in 'luabind::detail::adopt_policy<0, > >> void>::only_accepts_nonconst_pointers' > > > > The struct name "only_accepts_nonconst_pointers" is meant as an error > > message: adopt only works with non-const pointers (meaning that the > > adopted type must be both a pointer and a non-const one). > > > > Hope I could help, > > Christian |