Boost logo

Boost :

From: Aleksey Chernoraenko (achernoraenko_at_[hidden])
Date: 2003-09-24 23:12:39


We use very similar implementation but we name it 'scope_action', which
reflects the purpose more correctly, IMO.
In fact, this *is* some action, which will be invoked by exiting scope,
right? "Guard" is simply a name for one of the possible use cases (our name
for it is 'on_exit').

<code>
unsigned long LocalFileSize( std::string const& aFile )
        {
        int handle = _open( aFile.c_str(), _O_BINARY | _O_RDONLY | _O_RANDOM
);
        mtn::scoped_action a = mtn::on_exit( boost::bind( _close, handle )
);
        return handle != -1 ? _lseek( handle, 0, SEEK_END ) : -1;
        }
</code>

BTW, we also have the interesting and useful classes 'scoped_value' and
'deferred_value'.
They together cover a known patterns:

<code>
        if ( ... )
                {
                mtn::scoped_action a = mtn::scoped_value( fHandlingResize,
true );
                TPoint windowExtent( windowPos->cx, windowPos->cy );
                TRectangle rootViewLocation(
                          Location().TopLeft()
                        , ViewModel().TranslateToLogical( windowExtent -
fWindowEdge )
                        );
                }
</code>

http://www.meta-comm.com/engineering/resources/mtn/deferred_value.h
http://www.meta-comm.com/engineering/resources/mtn/scoped_value.h
http://www.meta-comm.com/engineering/resources/mtn/scoped_action.h
http://www.meta-comm.com/engineering/resources/mtn/on_exit.h
http://www.meta-comm.com/engineering/resources/mtn/aux_/assign.h

--
Aleksey Chernoraenko
MetaCommunications Engineering
At 22:46 2003-09-24, Peter Dimov wrote:
>Daniel Wallin wrote:
> >
> > FWIW, here's the scopeguard implementation that I use:
> >
> > <snip>
> >
> >    typedef const detail::guard_base& scope_guard;
>
>You are assuming that
>
>     scope_guard g = make_guard(f);
>
>will only create a single instance of guard_impl<F> (no copies), but this
is
>not guaranteed.
Right, my mistake.
    guard_base(const guard_base& rhs)
       : m_dismissed(rhs.m_dismissed)
    { rhs.dismiss(); }
When I said "that I use" I actually meant "that I once wrote with the intent
to use, but has never actually used". ;)
---
Daniel Wallin

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