Re: [Python] Wrapping Visitor Pattern and Object Slicing

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Re: [Python] Wrapping Visitor Pattern and Object Slicing

Dave Abrahams
The following message is a courtesy copy of an article
that has been posted to gmane.comp.lib.boost.user as well.

on Tue Dec 27 2011, Alex Olivas <> wrote:

> Hey All,
> I'm trying to wrap a third party lib that uses the visitor
> pattern and their own smart pointers ('ref_ptr' below), which
> manages objects that inherit from 'Referenced'.  I've put
> together a simple example that illustrates the problem.
> Code for python bindings :
> Simple test script :
> Output for two cases :
>    Case 1) 'apply' override is commented out in the python class
>    Case 2) 'apply' override exists
> The problem that I'm having is that the 'apply' method
> ( lines 83 - 86 ) slices everything to base unless I use
> boost::ref().  However this only works for wrapped classes
> and in this lib several classes use derived visitors that
> are not part of the public API ( which I modeled as HelperVisitor
> in the above example ).
> Is there anyway to prevent object slicing for both wrapped
> and unwrapped objects in this scenario?
> I'm building against boost 1.47 and if anyone's interested in
> digging deeper the 3rd party lib is Open Scene Graph 3.0.1.

Just a wild guess, but it looks like you're copying and slicing the
VisitorBase object here:

  VisitorBase_wrapper( PyObject* o , const VisitorBase& b) : self( o ), VisitorBase(b) {};

Also, you could try using & (address-of) instead of boost::ref.

Also, I suggest following this post up on the C++-sig instead of here;
you'll get better help.


Dave Abrahams
BoostPro Computing
Cplusplus-sig mailing list
[hidden email]