Boost logo

Boost :

From: Mike Owen (mikeowen_at_[hidden])
Date: 2001-06-26 19:19:30


I'm writing an open source tool for astrophysical simulations, and I've
recently been trying to switch to using Boost.Python to provide the
wrapping interface from C++ to Python (up until now I've been using a home
grown tool for this purpose written at Lawrence Livermore Lab). I've run
into two problems using BPL, though, and I wanted to see if I'm just
thinking about this wrong, or if anyone can suggest how to solve this.
These problems are both concerned with cross module support.

1. I cannot do cross module inheritance (i.e., have a base class defined
in one module, and then try to wrap a derived class in another module where
I need to be able to reflect this inheritance to python with
"declare_base"). The code and its interface are far too large to to make
it feasible to wrap in a single module. I've looked at the boost mailing
list and I found a few messages related to this problem from April, but
there didn't really seem to be a final answer. The obvious thing I've
tried is to export the converters for a class in one module, and then in
another import the converters and try to declare_base with the imported
type. The compiler (I'm mostly using g++) always barfs at this. Here's a
snippet that demonstrates the pattern:

In one module declare:
      python::class_builder<DefiniteBase> DefiniteBaseBoost(basemodule,
"DefiniteBase");
      python::export_converters(DefiniteBaseBoost);

And then in a second module try:
      python::import_converters<DefiniteBase> DefiniteBaseConverters("Base",
"DefiniteBase");
      python::class_builder<AnotherDerivedClass>
AnotherDerivedClassBoost(secondmodule, "AnotherDerivedClass");
      AnotherDerivedClassBoost.declare_base(DefiniteBaseConverters); // This
doesn't compile!

The error message I get back when I try to compile the second module is:
Secondmodule.cc: In function `void {anonymous}::initSecond(...)':
Secondmodule.cc:27: no matching function for call to
`boost::python::class_builder<AnotherDerivedClass,boost::python::detail::held_instance<AnotherDerivedClass>
>::declare_base (boost::python::import_converters<DefiniteBase> &)'

2. I make fairly heavy use of abstract base classes (with pure virtual
functions), and have other objects which expect to be able to take
instances of these abstract classes as arguments to methods. I can wrap
the abstract types and use them in a given module with no problem, but I
cannot seem to export the abstract types for use in other modules. As an
example, if I have a class "AbstractBase" that has pure virtual methods,
the following export statement always fails:

      python::class_builder<AbstractBase> AbstractBaseBoost(basemodule,
"AbstractBase");
      python::export_converters(AbstractBaseBoost); // Won't compile!

This generates the following error:
/home/owen/Spheral++/boost/boost/python/detail/extension_class.hpp: In
instantiation of
`boost::python::detail::instance_value_holder<AbstractBase,boost::python::detail::held_instance<AbstractBase>
>':
/home/owen/Spheral++/boost/boost/python/detail/extension_class.hpp:212:
instantiated from
`python_extension_class_converters<AbstractBase,boost::python::detail::held_instance<AbstractBase>
>::to_python(const AbstractBase &) const'
/home/owen/Spheral++/boost/boost/python/cross_module.hpp:228: instantiated
from `boost::python::export_converter_object<AbstractBase>::to_python(const
AbstractBase &)'
/home/owen/Spheral++/boost/boost/python/detail/init_function.hpp:130:
instantiated from here
/home/owen/Spheral++/boost/boost/python/detail/extension_class.hpp:778:
cannot declare field
`boost::python::detail::instance_value_holder<AbstractBase,boost::python::detail::held_instance<AbstractBase>
>::m_held' to be of type `boost::python::detail::held_instance<AbstractBase>'
/home/owen/Spheral++/boost/boost/python/detail/extension_class.hpp:778:
since
the following virtual functions are abstract:
ClassDefs.hh:10: void AbstractBase::pureMethod() const
  

I am attaching a small self-contained set of files that demonstrates these
patterns. They are "ClassDefs.hh", "Basemodule.cc", and "Secondmodule.cc".
If you try to compile Basemodule and Secondmodule you'll see what's
happening here. If anyone can help me out I'd really appreciate it!
Thanks.

Mike Owen.

-- 
 "Hey...where are the sunflower seeds?" |       J. Michael Owen         
        o_o /                           |       Phone:  925-423-7160     
        (")                             |       Email:  mikeowen_at_[hidden]
       \/'\/                            |
____(__(,_,)_______________________________________________________________







Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk