[C++-sig] shared_ptr object identity

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Mon Aug 16 06:11:48 CEST 2010


Hi Earl,

> So I would wrap member functions returning the shared_ptr, and store a

> persistent reference to the python object, returning its shared_ptr
> instead?

Yes.

> > - Add all data attributes to the C++ object (and use .def_readonly() or
> > .def_readwrite()).
> >  Then the state of the object is preserved naturally.
> >
> > - Inject additional Python methods as described in the Boost.Python 
tutorial:
> >
> > 
>http://www.boost.org/doc/libs/1_43_0/libs/python/doc/tutorial/doc/html/python> 
>/techniques.html#python.extending_wrapped_objects_in_python
> 
> None of these methods would allow me to extend the individual
> instances dynamically with attributes in python, correct?

Yes.
Do I understand correctly that you want to use the object essentially like
a dictionary, just with obj.key instead of obj["key"]?
I consider this bad practice, since it isn't obvious to readers of your
code.
Did you consider adding a boost::python::object member to your C++
object? That would solve all your problems I think and it would be
obvious.
If you need to keep your C++ code independent of Python, you could
work with boost::any.
 
> but isn't there some way to write a custom
> to_python_converter to allow me to inject that code whenever a
> shared_ptr is to be converted to python?

I think you could make this work, but you'd have to maintain a registry.
I'd look for a more conventional solution first.

Ralf


More information about the Cplusplus-sig mailing list