[C++-sig] inheritance and virtual callbacks

Matthew Scouten matthew.scouten at gmail.com
Wed Jun 4 17:37:44 CEST 2008


Ah, I see that is going on.

The important thing is not the registration. Registration is irrelevant
unless things are getting called from python. My callbacks are only called
from c++, and are overridden in python.

The important difference to note here is that in the py++ code, FooCallback
is separately wrapped in both foo and bar. That is exactly what I had hoped
to avoid. I was afraid that would be the case. Thank you.


On Tue, Jun 3, 2008 at 11:32 PM, Roman Yakovenko <roman.yakovenko at gmail.com>
wrote:

> 2008/6/3 Matthew Scouten <matthew.scouten at gmail.com>:
> > all right, yes. I forgot that. mea culpa.
> > but it did not fix the problem :
> > See?  still calls  the wrong function.
>
> I missed that you didn't registered the functions.
>
> This is the code that was generated by Py++:
> #include "boost/python.hpp"
>
> #include "yyy.h"
>
> namespace bp = boost::python;
>
> struct foo_wrapper : foo, bp::wrapper< foo > {
>
>    foo_wrapper(foo const & arg )
>    : foo( arg )
>      , bp::wrapper< foo >(){
>        // copy constructor
>
>    }
>
>    foo_wrapper( )
>    : foo( )
>      , bp::wrapper< foo >(){
>        // null constructor
>
>    }
>
>    foo_wrapper(int init_x, int init_y )
>    : foo( init_x, init_y )
>       , bp::wrapper< foo >(){
>        // constructor
>
>    }
>
>    virtual void FooCallback(  ) {
>        if( bp::override func_FooCallback = this->get_override(
> "FooCallback" ) )
>            func_FooCallback(  );
>        else
>            this->foo::FooCallback(  );
>    }
>
>
>    void default_FooCallback(  ) {
>        foo::FooCallback( );
>    }
>
> };
>
> struct bar_wrapper : bar, bp::wrapper< bar > {
>
>    bar_wrapper(bar const & arg )
>    : bar( arg )
>      , bp::wrapper< bar >(){
>        // copy constructor
>
>    }
>
>    bar_wrapper( )
>    : bar( )
>      , bp::wrapper< bar >(){
>        // null constructor
>
>    }
>
>    bar_wrapper(int init_x, int init_y, int init_z )
>    : bar( init_x, init_y, init_z )
>       , bp::wrapper< bar >(){
>        // constructor
>
>    }
>
>    virtual void BarCallback(  ) {
>        if( bp::override func_BarCallback = this->get_override(
> "BarCallback" ) )
>            func_BarCallback(  );
>        else
>            this->bar::BarCallback(  );
>    }
>
>
>    void default_BarCallback(  ) {
>        bar::BarCallback( );
>    }
>
>    virtual void FooCallback(  ) {
>        if( bp::override func_FooCallback = this->get_override(
> "FooCallback" ) )
>            func_FooCallback(  );
>        else
>            this->foo::FooCallback(  );
>    }
>
>
>    void default_FooCallback(  ) {
>        foo::FooCallback( );
>    }
>
> };
>
> BOOST_PYTHON_MODULE(pyplusplus){
>    bp::class_< foo_wrapper >( "foo" )
>        .def( bp::init< >() )
>        .def( bp::init< int, int >(( bp::arg("init_x"),
> bp::arg("init_y") )) )
>        .def(
>            "FooCallback"
>            , (void ( ::foo::* )(  ) )(&::foo::FooCallback)
>            , (void ( foo_wrapper::* )(  )
> )(&foo_wrapper::default_FooCallback) )
>         .def_readwrite( "x", &foo::x )
>         .def_readwrite( "y", &foo::y );
>
>    bp::class_< bar_wrapper, bp::bases< foo > >( "bar" )
>        .def( bp::init< >() )
>        .def( bp::init< int, int, int >(( bp::arg("init_x"),
> bp::arg("init_y"), bp::arg("init_z") )) )
>        .def(
>            "BarCallback"
>            , (void ( ::bar::* )(  ) )(&::bar::BarCallback)
>            , (void ( bar_wrapper::* )(  )
> )(&bar_wrapper::default_BarCallback) )
>         .def_readwrite( "z", &bar::z )
>        .def(
>             "FooCallback"
>            , (void ( ::foo::* )(  ) )(&::foo::FooCallback)
>            , (void ( bar_wrapper::* )(  )
> )(&bar_wrapper::default_FooCallback) );
>
>    { //::backcaller
>
>        typedef void ( *backcaller_function_type )( ::bar & );
>
>        bp::def(
>            "backcaller"
>            , backcaller_function_type( &::backcaller )
>            , ( bp::arg("b") ) );
>
>    }
> }
>
>
>
> --
> Roman Yakovenko
> C++ Python language binding
> http://www.language-binding.net/
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org <C%2B%2B-sig at python.org>
> http://mail.python.org/mailman/listinfo/c++-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080604/152deae3/attachment.htm>


More information about the Cplusplus-sig mailing list