[C++-sig] intrusive pointer comparison in python
Alex Mohr
amohr at pixar.com
Wed Oct 4 22:10:28 CEST 2006
> Here's a better example:
>
> class foo;
> typedef boost::intrusive_ptr<foo> fooHandle;
>
> // personal refcounting implementation
> class foo: public reference_counted {};
> };
>
> class foocontainer {
> public:
> foocontainer():fooobj(foo::getfoo()) {}
> fooHandle getfoo() {return fooobj;}
> private:
> fooHandle fooobj;
> };
>
> BOOST_PYTHON_MODULE(test_intrusive) {
> class_<foo,fooHandle>("foo");
> class_<foocontainer>("foocontainer")
> .def("getfoo",&foocontainer::getfoo);
> }
>
> Now if I go into python shell:
>>> x=foocontainer()
>>> test1=x.getfoo()
>>> test2=x.getfoo()
>>> test1==test2
> False
This is due to the fact that boost::python doesn't really handle what I
call the "object identity" problem. In this case, you expect test1 and
test2 to be the same python object because they "represent" the same C++
object. They're not the same python object though -- boost python
creates a new python wrapper each time a fooHandle is converted to-python.
There's no simple way to solve this problem in boost::python as-is. It
would be great if boost::python could have some support for doing this,
because it's obviously an important thing.
BTW: I should mention that boost::python does handle this correctly for
objects that are exposed to python polymorphically (deriving from
wrapper<T>). But then only for polymorphic wrapper objects
(boost::python holds the python object in wrapper<T>).
Incidentally, you can make your 'test1 == test2' line work by adding the
equality operator to your wrapper, and have it compare the pointers.
But you can't make 'test1 is test2' work.
Alex
More information about the Cplusplus-sig
mailing list