Boost logo

Boost :

Subject: Re: [boost] Proposed new RAII Library
From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2012-09-14 14:37:48


Sohail Somani wrote:
> On 14/09/2012 11:43 AM, Lorenzo Caminiti wrote:
> >
> >UNARY_SCOPE_EXIT(CloseHandle, handle);
> >
> >That way the code for this one specialized use case of Scope Exit
> >becomes closer to what your expressed preference is. You can of course
> >put this macro #definition into your own little lib
> >scope_exit_arity.hpp and re-use it across your different projects:)
>
> This was pretty clever and I exclaimed "mother of god" but honestly
> I need the scope guard's "dismiss/release" function as does 99.93%
> of people who want the scope guard (I just did a poll.)

#define UNARY_SCOPE_EXIT(dismiss, unary_func, arg) \
    BOOST_SCOPE_EXIT( (&dismiss)(&arg) ) { \
        if (!dismiss) unary_func(arg); \
    } BOOST_SCOPE_EXIT_END

And then:

bool dismiss = false;

...

UNARY_SCOPE_EXIT(dismiss, CloseHandle, handle);

...

dismiss = true;

Alex


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