|
Boost : |
From: Jason Hise (chaos_at_[hidden])
Date: 2005-05-01 15:56:06
Does a utility like the following already exist in boost? If so, what
is it called? If not, is there interest in adding it?
template < typename Function >
struct free_on_error
{
private:
Function function_;
bool reset_;
public:
free_on_error ( Function function ) :
function_ ( function ),
reset_ ( true )
{
}
~ free_on_error ( )
{
if ( reset_ )
{
function_ ( );
}
}
void discard ( )
{
reset_ = false;
}
};
This would be used to make certain operations exception safe. For example:
A * foo ( )
{
A * a = new A ( );
free_on_error < BOOST_TYPEOF ( lambda::bind ( delete _1, a ) ) >
release ( lambda::bind ( delete _1, a ) );
a->DoSomethingThatMightThrow ( );
release.discard ( );
return a;
}
(my experience with lambda is minimal, so I'm not sure that delete _1
creates a valid lambda functor, but you get the idea)
-Jason
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk