[C++-sig] Exposing Abstract Classes and factories

Roman Yakovenko roman.yakovenko at gmail.com
Tue Apr 18 22:53:38 CEST 2006


On 4/18/06, Rod Cloutier <rodlist at hotmail.com> wrote:
> Hi,
>
> ==================== Hello.h  =============================
>
> #include <memory>
>
> class Abstract
> {
> public:
>  virtual void run() const = 0;
> };
>
> std::auto_ptr<Abstract> create();
>
>
> ==================== Hello.cpp  =============================
>
> #include <iostream>
> #include "hello.h"
>
> class Concrete : public Abstract
> {
>  virtual void run( ) const
>  {
>   std::cout << "Concrete ran!";
>  }
> };
>
> std::auto_ptr<Abstract> create()
> {
>  return std::auto_ptr<Abstract>( new Concrete() );
> }
>
>

You found bug in pyplusplus. Thanks.

Description:
I renamed your classes to "abstract" and "concrete", and put them into
"factory" namespace

namespace bp = boost::python;

struct abstract_wrapper : factory::abstract, bp::wrapper< factory::abstract > {

    abstract_wrapper()
    : factory::abstract()
      , bp::wrapper< factory::abstract >()
    {}

    virtual int run(  ) const {
        bp::override run = this->get_override( "run" );
        return run(  );
    }

};

BOOST_PYTHON_MODULE(factory){
    if( true ){
        typedef bp::class_< abstract_wrapper, boost::noncopyable >
abstract_exposer_t;
        abstract_exposer_t abstract_exposer = abstract_exposer_t( "abstract" );
        bp::scope abstract_scope( abstract_exposer );
        abstract_exposer.def( "run"
                //, bp::pure_virtual( &::abstract_wrapper::run )
                //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                , bp::pure_virtual( &::factory::abstract::run )
                , bp::default_call_policies() );
        bp::register_ptr_to_python< std::auto_ptr< factory::abstract > >();
    }

    bp::def( "create"
            , &factory::create
            , bp::default_call_policies() );
}

When generating "def" for pure virtual function "run", as argument I
provided address
to "run" function defined in wrapper class. This is just wrong. I need
to provide reference
to function defined in base class.

Fix. I don't have access to CVS right now, so I attach file to the mail.
Please put it in pyplusplus/code_creators directory.

Also, I added this use case to unittests.

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: calldef.py
Type: text/x-python
Size: 40092 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20060418/19218de6/attachment.py>


More information about the Cplusplus-sig mailing list