[C++-sig] Re: call_method

David Abrahams dave at boost-consulting.com
Sat Nov 16 07:00:40 CET 2002


"Brett Calcott" <brett.calcott at paradise.net.nz> writes:

>> > Are there some other advantages to using shared_ptr?
>>
>> Oh, yes: many, many. For example, shared_ptr<U> -> shared_ptr<T> is a
>> legitimate conversion, even though you haven't intruded on U. And
>> probably more-importantly, shared_ptr<> interoperates seamlessly in
>> many contexts and serves as a "one-stop shop" for nearly all smart
>> pointer needs. Just see the enclosed document for reasons why.
>>
>>
>
> Wow - mind-expanding material. But I still cannot see a way to using
> shared_ptr. The customer release function passed in the constructor doesn't
> give you enough control to do the "crazy" counting stuff.

You don't need to do the "crazy" counting stuff! (read on...)

> How did you want to progress this? Is it worth trying to put something like
> this back into the library -- or should it just go into the documentation as
> a "technique"?

I'm thinking we should do this:

1. Set up Boost.Python so that shared_ptr<T> can be extracted from any
Python object from which we can extract T*... no matter how it's
held! You can do this by using a custom deleter that holds the Python
object and keeps it alive. See the section titled:

   "Using a shared_ptr to Hold Another Shared Ownership Smart Pointer"

I find this very exciting, and can't believe I didn't think of it
earlier!!

2. Prepare a special "self_base" class that can be used as a base of
   "callback" classes like BaseWrap from the examples. This base would
   hold the "self" pointer. It would also provide a family of
   convenience functions which implement call_method<> on the held
   self pointer without having to explicitly mention it (hmm, I'm not
   sure I can make this work out on msvc6. But that's another
   issue). Most-importantly, when references and (smart) pointers to
   polymorphic classes are converted to python, we will attempt to
   downcast to "self_base", and if successful, we'll return the
   contained self pointer instead of a new object. 

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list