[C++-sig] Reference counted object return value policy

David Abrahams dave at boostpro.com
Tue Sep 23 18:04:19 CEST 2008


on Mon Sep 22 2008, Jean-Sébastien Guay <jean-sebastien.guay-AT-cm-labs.com> wrote:

> Hi all,
>
> Thanks to David for pointing out my mistake earlier, I am progressing again on
> my OSG boost.python wrappers.
>
> Now, you may or may not know that OSG uses its own reference counting
> mechanism. All ref counted objects inherit from an osg::Referenced
> base class which has the reference count, and a smart pointer template
> class ref_ptr<T> manages these counts. I've thus far been able to use
> pointee< ref_ptr<T> > as heldtype to get around the fact that
> destructors are protected, but now I want to expose the file loading
> method:
>
>   osg::Node* osgDB::readNodeFile(const std::string& filename, /*...*/)
>
> I've got this:
>
>     def("readNodeFile", readNodeFile1, /* some return value policy */);
>
> I'm stuck at the choice of return value policy. I guess the most
> logical would be return_value_policy<manage_new_object>(), but this
> gives a compilation error because the actual C++ return type of the
> function has a protected destructor.

As long as it's protected, you can break in by:

   struct Hole : osg::Node
   {
       struct deleter
       {
            void operator()(osg::Node* n) const { delete n; }
       };
   };

and then expose a thin wrapper around readNodeFile that returns

    boost::shared_ptr<Node*>(readNodeFile(...), Hole::deleter());

Or use your own smart pointer if that's what you have.

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com



More information about the Cplusplus-sig mailing list