Added ExceptionPolicy enum.
git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@1082 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
parent
27f38492bd
commit
904bbee76d
1 changed files with 81 additions and 48 deletions
|
@ -17,6 +17,7 @@
|
|||
|
||||
// $Id$
|
||||
|
||||
#include <exception> // needed for calls to uncaught_exception.
|
||||
|
||||
#include <loki/RefToValue.h>
|
||||
|
||||
|
@ -42,6 +43,30 @@ namespace Loki
|
|||
|
||||
class ScopeGuardImplBase
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
enum ExceptionPolicy
|
||||
{
|
||||
AlwaysExecute = 0,
|
||||
CallIfNoException = 1,
|
||||
CallIfException = 2
|
||||
};
|
||||
|
||||
ScopeGuardImplBase() throw() : dismissed_(false), exceptionPolicy_( AlwaysExecute )
|
||||
{}
|
||||
|
||||
void Dismiss() const throw()
|
||||
{
|
||||
dismissed_ = true;
|
||||
}
|
||||
|
||||
void SetExceptionPolicy( ExceptionPolicy policy ) const throw()
|
||||
{
|
||||
exceptionPolicy_ = policy;
|
||||
}
|
||||
|
||||
private:
|
||||
/// Copy-assignment operator is not implemented and private.
|
||||
ScopeGuardImplBase& operator =(const ScopeGuardImplBase&);
|
||||
|
||||
|
@ -53,6 +78,7 @@ namespace Loki
|
|||
/// Copy-constructor takes over responsibility from other ScopeGuard.
|
||||
ScopeGuardImplBase(const ScopeGuardImplBase& other) throw()
|
||||
: dismissed_(other.dismissed_)
|
||||
, exceptionPolicy_( other.exceptionPolicy_ )
|
||||
{
|
||||
other.Dismiss();
|
||||
}
|
||||
|
@ -60,25 +86,32 @@ namespace Loki
|
|||
template <typename J>
|
||||
static void SafeExecute(J& j) throw()
|
||||
{
|
||||
if ( AlwaysExecute != j.exceptionPolicy_ )
|
||||
{
|
||||
const bool anyThrown = ::std::uncaught_exception();
|
||||
if ( anyThrown )
|
||||
{
|
||||
if ( CallIfNoException == j.exceptionPolicy_ )
|
||||
j.Dismiss();
|
||||
}
|
||||
else if ( CallIfException == j.exceptionPolicy_ )
|
||||
{
|
||||
j.Dismiss();
|
||||
}
|
||||
}
|
||||
if (!j.dismissed_)
|
||||
{
|
||||
try
|
||||
{
|
||||
j.Execute();
|
||||
}
|
||||
catch(...)
|
||||
{}
|
||||
}
|
||||
}
|
||||
|
||||
mutable bool dismissed_;
|
||||
|
||||
public:
|
||||
ScopeGuardImplBase() throw() : dismissed_(false)
|
||||
{}
|
||||
|
||||
void Dismiss() const throw()
|
||||
{
|
||||
dismissed_ = true;
|
||||
}
|
||||
mutable ExceptionPolicy exceptionPolicy_;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Add table
Reference in a new issue