|
Boost Users : |
From: Andrew Holden (aholden_at_[hidden])
Date: 2006-11-29 12:50:25
-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Sohail Somani
Sent: Tuesday, November 28, 2006 4:44 PM
>
> > -----Original Message-----
> > From: boost-users-bounces_at_[hidden]
> > [mailto:boost-users-bounces_at_[hidden]] On Behalf Of
> > Sohail Somani
> > > The deleter feature in shared_ptr is very useful for raii
> > > with any kind of
> > > resource (variables, functions, objects, etc.). Having it in
> > > scoped_ptr will
> > > offer the same advantage for noncopyable pointers.
> > >
> > > Thank you.
> >
> > +1 from me too but I think the reason is that people wanted
> > sizeof(scoped_ptr<T>) to be sizeof(T *) (or they don't want it to
> > change)
>
> A part solution could be a second template parameter but of course
there
> can be no state.
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. The default
deleter adds no state, and simply deletes the pointer, just like
scoped_ptr always has. On most compilers, it will not change the size
of the shared_ptr, and any apparent overhead should be optimized away.
I have also supplied an assignable deleter, which will act much like
shared_ptr's custom deleters. If you select this deleter, it will
increase the size of the scoped_ptr to store the pointer to the custom
deleter. This also adds member functions to set and clear the deleter,
and even enables new versions of the constructer and reset function that
take the deleter as a second parameter.
It is also somewhat straightforward to add new deleter policies. This
could be useful if you wanted to use a custom deleter without increasing
the size of the scoped_ptr. Here is a skeleton for a new deleter
policy:
template<class T> class default_deleter
{
public:
/*Any public functions you declare will be added to the
scoped_ptr's interface*/
protected:
typedef int delete_parm_type; /*This will become the second
parameter for the two-argument constructor and reset function*/
default_deleter() {} /*Protected constructor because this
class to worthless on its own*/
~default_deleter() {} /*Protected destructor to ensure
scoped_ptr must be properly destroyed*/
void policy_delete (T* ptr)
{
/*Your delete code here*/
}
void swap(default_deleter & b) // never throws
{
/*Any code needed to swap two deleters here*/
}
private:
default_deleter (delete_parm_type); /*Make this protected if
you want to use the two-argument constructor and reset function*/
//Declare these private to prevent copying
default_deleter(default_deleter const &);
default_deleter & operator=(default_deleter const &);
};
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