Boost logo

Boost Users :

From: Sohail Somani (s.somani_at_[hidden])
Date: 2006-11-28 20:00:32


Hi,

Please see the following email about additional template parameters for
scoped_ptr. The quick hack I wrote for it is below (I've used this in
previous project as well). I think it would be very useful to many users
and wouldn't change any source using scoped_ptr.

Thanks,

Sohail
====
I've been thinking something like (just something I hacked together just
now). Not sure if there is a nicer way.

#include <string>
#include <cstdio>
#include <iostream>

template<typename T>
struct DeletingDeleter
{
        DeletingDeleter(T * p)
        {
            delete p;
            std::cout << "DeletingDeleter" << std::endl;
        }
};

struct FileCloser
{
    FileCloser(FILE * f)
    {
        std::cout << "Closing file" << std::endl;
        fclose(f);
    }
};

template<typename T, typename Deleter=DeletingDeleter<T> >
struct scoped_ptr
{
    scoped_ptr():p_(0){}
    scoped_ptr(T * p):p_(p){}
    ~scoped_ptr()
    {
        Deleter d(p_);
    }
    T * get() {return p_;}
    // regular stuff
private:
    T * p_;
};

typedef scoped_ptr<FILE,FileCloser> file_ptr;

int main()
{
          scoped_ptr<int> a; // maintain old interface
        scoped_ptr<int> b(new int(5));
        file_ptr p(fopen("myfile.txt","w"));
        fprintf(p.get(),"Hello world!\n");
}

> -----Original Message-----
> From: boost-users-bounces_at_[hidden]
> [mailto:boost-users-bounces_at_[hidden]] On Behalf Of David Klein
> Sent: Tuesday, November 28, 2006 4:55 PM
> To: boost-users_at_[hidden]
> Subject: Re: [Boost-users] Suggestion for boost`s smart pointers
>
> Sohail Somani wrote:
> >> -----Original Message-----
> >> From: boost-users-bounces_at_[hidden]
> >> [mailto:boost-users-bounces_at_[hidden]] On Behalf Of
> David Klein
> >>
> >> Sohail Somani wrote:
> >>
> >>>> -----Original Message-----
> >>>> From: boost-users-bounces_at_[hidden]
> >>>> [mailto:boost-users-bounces_at_[hidden]] On Behalf
> Of mark cox
> >>>>
> >>>> wow, this seems like a great idea, a scoped_ptr with deleter
> >>>> would be v.useful.
> >>>> mark
> >>>>
> >>>>
> >>> I've been thinking something like (just something I hacked
> >>>
> >> together just
> >>
> >>> now). Not sure if there is a nicer way.
> >>>
> >>>
> > [snip]
> >
> >> +1 vote for this idea :)
> >>
> >> i actually kind of did this for my current project,
> >> because i needed a custom deleter for scoped_ptr,
> >> when shared_ptr was to much overhead...
> >>
> >
> > As did I, but it was not for performance but for readability. Shared
> > pointers mean... Shared. Scoped don't.
> > _______________________________________________
> > Boost-users mailing list
> > Boost-users_at_[hidden]
> > http://lists.boost.org/mailman/listinfo.cgi/boost-users
> >
> >
>
> yep exactly,
> thatswhy i "ripped" scoped_ptr out of boost and added my
> custom_deleter
> stuff.
> (i only *thought* about shared_ptr to do the job)
>
> so are there any objections about adding this feature to scoped_ptr?
>
> --
> regards dave
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


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