boost: return pointer to base class

Jacek Generowicz jacek.generowicz at cern.ch
Thu Jul 25 03:21:34 EDT 2002


Hi,

This question has been unfielded on the Boost users list for a couple
of days, so I thought I'd try my luck here.

My requirements:

  1) Expose a C++ class hierarchy to python (using Boost)
  2) Allow all the exposed classes to be subclassed, with correct
     overriding of virtual fuctions
  3) Expose a C++ function which returns a pointer to the base class.


The problem:

  Trying 3) makes the compiler complain about lack of an appropriate
  (boost-generated) conversion funcion. My attempts at providing one
  by hand have failed (though I could do it before I added the
  callback classes required by point 2 above).


Minimal example code:

  #include <boost/python/class_builder.hpp>
  namespace python = boost::python;
  
  // A class hierarchy in C++
  struct abstract { virtual ~abstract(); };
  class concrete : public abstract {};
  
  // Ensure correct python subclassing of the above.
  struct abstract_callback : public abstract {
    PyObject * self;
    abstract_callback(PyObject * Self) : abstract(), self(Self) {}
  };
  
  struct concrete_callback : public concrete {
    PyObject * self;
    concrete_callback(PyObject * Self) : concrete(), self(Self) {}
  };
  
  // A C++ function which returns a pointer to the base of the C++
  // hierarchy
  abstract* rcbpta() {
    return new concrete;
  }
  
  BOOST_PYTHON_MODULE_INIT(abstract) {
  
    python::module_builder this_module("abstract");
  
    python::class_builder<abstract, abstract_callback> 
      abs_class(this_module, "abstract");
    abs_class.def(python::constructor<>());
  
    python::class_builder<concrete,concrete_callback> 
      con_class(this_module, "concrete");
    con_class.declare_base(abs_class);
    con_class.def(python::constructor<>());
  
    // Exposing this function causes problems !
    this_module.def(rcbpta, "problem");
  }


gcc 2.95.2 error message when compiling the above:

  <boost-path>/python/caller.hpp:430: instantiated from
  `boost::python::caller<abstract *>::call(abstract * (*)(), PyObject *,
  PyObject *)'
  
  <boost-path>/python/detail/functions.hpp:73: instantiated from
  `boost::python::detail::wrapped_function_pointer<abstract *,abstract *
  (*)()>::do_call(PyObject *, PyObject *) const'
  
  <boost-path>/function/function_base.hpp:155:   instantiated from here
  
  <boost-path>/python/detail/extension_class.hpp:388: no matching
  function for call to `py_extension_class_converters
  (boost::python::type<abstract *>)'
  
  <boost-path>/python/detail/extension_class.hpp:215: candidates are:
  class python_extension_class_converters<abstract,abstract_callback>
  py_extension_class_converters(boost::python::type<abstract>)
  
  <boost-path>/python/detail/extension_class.hpp:215: 
  class python_extension_class_converters<concrete,concrete_callback>
  py_extension_class_converters(boost::python::type<concrete>)


Any prods in the right direction would be appreciated.

Thanks,



More information about the Python-list mailing list