Boost logo

Boost :

From: Vladimir Pozdyayev (ardatur_at_[hidden])
Date: 2005-09-05 02:49:31


Hello David,

>> 2b. Now, suppose we have one more derived class:
>> class Base { ... };
>> class Derived: public Base { ... };
>> class HiddenImplementation: public Derived { ... };
>> with Derived being no more than an interface. Naturally, we don't want
>> to export HiddenImplementation to Python, but still want to retain
>> automatic conversions from Base* to Derived* whenever
>> pointer to HiddenImplementation object is passed to Python.
>> Any way to do this? Without hack-ins?

DA> Not without a hack, no. But the hack might be simple:
DA> #include <boost/python/object/inheritance.hpp> // non-public implementation details
DA> ...
DA> boost::python::object::register_conversion<Derived,HiddenImplementation>();
DA> boost::python::object::register_conversion<HiddenImplementation,Derived>();

Thanks again for the advice---though it didn't work as is, I was able
to track down the reason, and came up with a solution. Here it is, in
case you, or anyone else, is interested.

First, I should mention that such conversions for function args passed
from Python are always there---even if we don't mention implementation
class in the extension module at all. What disappears in the latter
case, is similar conversions for values being returned to Python.
The solution is actually to pretend that HiddenImp objects are some
sort of Derived ones:
  objects::copy_class_object(
    type_id< Derived >(),
    type_id< HiddenImplementation >()
  );
instead of calling "class_< HiddenImplementation >...".

As before, we don't even need access to actual HiddenImp declaration.
It is enough to have a fake
  class HiddenImplementation: public Derived { };
one. At least, it is enough for MSVC, and HiddenImplementation defined
in a separate DLL and not mentioned anywhere else in the extension
module.

-- 
Best regards,
 Vladimir                            mailto:ardatur_at_[hidden]

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