Boost logo

Boost Users :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2006-11-29 13:52:24


On Nov 29, 2006, at 12:50 PM, Andrew Holden wrote:

> I have put together an option based on Andrei Alexandrescu's policy
> based design. It may take a little time to read, as I modified a copy
> of scoped_ptr.hpp. What I did was add a second template parameter
> (called deleter) and inherited scoped_ptr from this.

If instead of inheriting from D, you contain a compressed_pair<T*,
D>, you'll get the same size savings and be able to support function
pointers as deleters. With a little more work (in your constructors
mainly) you can support D& types as a deleter.

D& types are really handy for transferring pointer ownership from one
object to another when there is a custom deleter of unknown type. Of
course since you're working with scoped_ptr, perhaps you're not
interested in this use case. But just in case you are:

template<class T, class D>
class A
{
     ...
     void foo(T* p);
private:
     T* p_;
     D deleter_;
     ...
};

// A::foo establishes ownership of p, but
// must acquire other resources to do so. A local
// unique_ptr is used as an aid to hold on to p while
// those other resources are acquired.
template<class T, class D>
void
A<T, D>::foo(T* p)
{
     // Establish preliminary ownership without requiring
     // a copy of the deleter D
     std::unique_ptr<T, D&> hold(p, deleter_); // no throw
     // acquire resources // if throws,
     // ... // deleter_(p)
executed
     // transfer ownership to A
     p_ = hold.release(); // no throw
}

-Howard


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net