[C++-sig] Re: Making Boost.Python understand DerivedSafe

David Abrahams dave at boost-consulting.com
Sat Jun 19 13:00:41 CEST 2004


Chad Austin <caustin at gmail.com> writes:

> (I am not yet subscribed to the list...  it seems mailman is having
> trouble sending me the authorization.  Please CC me on replies.. 
> Thanks.)
>
> Hi all,
>
> I just started using Boost.Python to embed Python in a project of
> mine.  I spent a couple days evaluating, testing, and studying, but it
> seems to have paid off!  Great job on making such a time-saving
> library!
>
> I am wrapping an internally refcounted class called CutScene. 
> CutScene objects are referenced by RefPtr<CutScene> smart pointers,
> typedef'd as CutScenePtr.  I export the class with:
>
> class_<CutScene, CutScenePtr>("CutScene").def /*etc*/;
>
> So far, so good.  The problem comes about when I want to pass a
> CutScenePtr into object::operator() to call a function.  If I pass a
> CutScenePtr object in, it says something about reject_raw_object_ptr:
>
> d:\Projects\empyrean\empyrean\third-party-vc7\include\boost\python\converter\arg_to_python.hpp(244)
> : error C2784: 'void
> boost::python::converter::detail::reject_raw_object_ptr(T *)' : could
> not deduce template argument for 'T1 *' from 'pyr::RefPtr<T>'
>         with
>         [
>             T=pyr::CutScene
>         ]

1. Show us more detail: more of the error messages, some sample code,
   etc.

2. As far as I can tell you have used boost::ptr around one of your
   RefPtr<CutScene>s, call it 'x':

         ptr(x)

   boost::ptr is reserved for real pointers.

> If I pass the result of CutScenePtr::get() into operator() I get a
> runtime error about Boost.Python not knowing how to convert
> RefDerivedSafe<T>* to a Python object.  This makes sense, because
> RefPtr's implicit conversion operator, operator->, and get() all
> return RefDerivedSafe<T>*. (RefDerivedSafe<T> is a special class
> derived from T that prevents C++ clients from accidentally calling
> delete, ref(), or unref() on a smart pointer

Bah!

        T* p = x.get();
        delete p;

> . This techique is used in both ATL and nsCOMPtr in Mozilla).

That doesn't make it safe ;->

> Here's my question:  Is there a way to tell Boost.Python that
> ptr(someCutScenePtr) is valid 

No!  Why do you want to use ptr?

> and should be equivalent to
> ptr(someCutScenePtr.get()) and/or that RefDerivedSafe<T>* can be
> implicitly converted to T*?  I've tried several different things, and
> nothing seems to work...

Maybe this helps?

http://www.boost.org/libs/python/doc/v2/register_ptr_to_python.html

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com





More information about the Cplusplus-sig mailing list