[C++-sig] boost::python - how to pass by reference/pointer to python overriden class functions
Jay Riley
super24bitsound at hotmail.com
Thu May 26 01:33:27 CEST 2011
Your reply actually helped alot. I hadn't considered that the call_method might have been being used incorrectly. Turns out it was. After looking up the documentation for call_method, by changing
void StatusEffectWrapper::TickCharacter(Game::Character::BaseCharacter* character, Game::Battles::BattleField *field, int ticks)
{
call_method<void>(self, "TickCharacter", character, field, ticks);
}
to this
void StatusEffectWrapper::TickCharacter(Game::Character::BaseCharacter* character, Game::Battles::BattleField *field, int ticks)
{
call_method<void>(self, "TickCharacter", ptr(character), ptr(field), ticks);
}
I get the behave I expected. Thanks!
> Date: Wed, 25 May 2011 15:35:31 -0700
> From: talljimbo at gmail.com
> To: cplusplus-sig at python.org
> Subject: Re: [C++-sig] boost::python - how to pass by reference/pointer to python overriden class functions
>
> On 05/25/2011 02:26 PM, Jay Riley wrote:
> > I have some class functions that takes in parameters by pointer.
> > However, when I expose and override this class/these functions in
> > boost::python, any changes I make to the parameters are not reflected
> > back to the original, meaning it isn't being passed by pointer as I
> > expect. I previously had them as pass-by-reference, but the same problem
> > still occurred. For an example of what I mean, here's some of my code
> > with unimportant functions and details omitted.
> >
>
> I note two spots that bear further investigation.
>
> The most likely source is here:
>
> > //In the actual scripting code
> > void StatusEffectWrapper::TickCharacter(Game::Character::BaseCharacter*
> > character, Game::Battles::BattleField *field, int ticks)
> > {
> > call_method<void>(self, "TickCharacter", character, field, ticks);
> > }
>
> I think there's a very good chance that call_method is copying your
> character argument when it converts it to Python. Boost.Python is
> extra-careful about making sure the C++ objects it puts inside Python
> objects don't turn into dangling references, and it has no way to
> control the lifetime of a raw pointer or reference. I think you could
> probably fix this by using shared_ptr<BaseCharacter> here, because
> Boost.Python does know how to convert that safely to Python without
> copying the pointee.
>
> If that fails, look here:
>
> > .def("TickCharacter", &StatusEffect::TickCharacter,
> > &StatusEffectWrapper::TickCharacterDefault)
>
> I've personally never seen this invocation of .def, with two member
> function pointer arguments. That doesn't necessarily mean it's
> incorrect, because overloading virtual methods in Python is feature I
> almost never use, and it seems like there are a half-dozen ways to do it
> and I never know which one is considered best (perhaps someone else on
> this list does?). But you might want to make sure this does what you
> expect.
>
> HTH
>
> Jim Bosch
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20110525/af64be44/attachment-0001.html>
More information about the Cplusplus-sig
mailing list