Boost logo

Boost :

From: Tomasz Kowalczyk (tomek_at_[hidden])
Date: 2000-03-29 20:22:46


I have written a lightweight signal/slot mechanism and have been
considering making it available for everybody (if there is a need of
course) through boost. It's amazing how many applications signals can
have ... Here is another one: signals can be used to manage resources:

class guard {
public:
        // signal without arguments
        signal<> on_exit;
        // raise the signal when going out of scope
        ~guard() { on_exit(); }
};

void f( ResourceSource *s )
{
        // Guard, which raises g.on_exit signal when it goes
        // out of scope.
        guard g;

        HANDLE handle = s->GetResource();

 // The semantics of this connection is: prepend "h" to
        // the argument list and call method ReleaseResource
        // of the object "s".
        g.on_exit.connect(
                prepend( handle, closure(s, &s->ReleaseResource) ) );

        FILE *file = fopen("parrot.jpg");

        // Here, append "file" to the argument list and call
        // ordinary function "fclose" (g.on_exit is raised
        // without arguments, so there is really no difference here
        // between appending and prepending)
        g.on_exit.connect( append( file, function(fclose) ) );

        // ...
        // Now you can code without worrying that your resource
        // will not be freed.
}

Tomek

Kevin Atkinson wrote:
>
> On Wed, 29 Mar 2000, Ian Bruntlett wrote:
>
> > Here are my reasons for having two methods:-
> >
> > release() needs to know a safe "empty" value for the resource being handled.
> > For pointer types this is 0. For many OS handles this is 0. Some OS handles
> > have an "empty" value of -1. This kind of specific information is used by
> > release().
> >
> > The freeing up of the resource is handled by reset().
>
> So then pass in a class with 2 members one "free" and another
> "is_null". This will then extend better to other smart pointers types
> then writing a specific methods for auto pointer.
> >
> > > > > 3) Your module won't extend well to other smart pointers types which
> > are
> > > > > not as simple as auto_ptr such as boost's referencing counting
> > smart
> > > > > pointer.
> > > > I hadn't considered that. Could you recommend a smart pointer type that
> > does
> > > > extend well to other smart pointer types?
> > > What do you mean by that?
> >
> > You'd pointed out in (3) that auto_reference<> doesn't extend well to other
> > smart pointer types. I agree. They weren't considered.
> > I thought that (3) implied the existence of a smart pointer type that does
> > extend well to other smart pointer types.
>
> You are looking for an interface. I am looking for a general method to
> extend all smart pointers. That general method in my view is to pass in a
> class with the necessary information so that the smart pointer can do its
> job. For most pointers this means one to free the resource and another to
> tell if it is null. For some resources there is no null and
> auto_resource should then insists that the resource is always pointing to
> something valid.
>
> Also, you are also repeatedly ignoring the fact that you specializations
> won't work one the compiler can't tell one resource from another because
> there are both represented by an integer!
>
> ---
> Kevin Atkinson
> kevinatk_at_[hidden]
> http://metalab.unc.edu/kevina/
>
> ------------------------------------------------------------------------
> Special Offer-Earn 300 Points from MyPoints.com for trying @Backup
> Get automatic protection and access to your important computer files.
> Install today:
> http://click.egroups.com/1/2344/2/_/9351/_/954343444/
>
> -- Talk to your group with your own voice!
> -- http://www.egroups.com/VoiceChatPage?listName=boost&m=1


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk