[C++-sig] Best way to register shared_ptr<const T>
amohr at pixar.com
Thu Jan 24 23:00:02 CET 2008
> It looks you know a lot about it... would you be able to help me on the other issue I posted recently:
> somehow boost::python is able to tell when 2 different instances of a boost::shared_ptr<> point to
> the same C++ object.
> This is not the case for custom smart pointers.
> I'm happy to investigate, but I could not find where to start. Do you know how it works?
Boost.python has special-case code for shared_ptr. When boost.python
creates a shared_ptr for a class, it supplies a special custom deleter
that tracks the python object identified with the c++ object.
When a shared_ptr is converted to_python, boost.python checks to see if
it has its special deleter, and if so, it can get the existing python
object from it. That's how "object identity" is maintained for shared_ptr.
It is possible to make object identity work for custom smart pointers,
but it is hard. What we're doing is totally gross. After a class_<> is
registered with boost.python, I manually replace the to_python
converter that boost.python registered with my own that wraps it but
also does custom object-identity mapping. This requires digging into
the conversion registry using private api, and doing nasty stuff, but
you *can* make it work.
More information about the Cplusplus-sig