[C++-sig] inheritance and virtual callbacks

Roman Yakovenko roman.yakovenko at gmail.com
Wed Jun 4 06:32:51 CEST 2008


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/



More information about the Cplusplus-sig mailing list