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:
rich_sposato 2011-06-21 01:07:22 +00:00
parent 27f38492bd
commit 904bbee76d

View file

@ -17,6 +17,7 @@
// $Id$ // $Id$
#include <exception> // needed for calls to uncaught_exception.
#include <loki/RefToValue.h> #include <loki/RefToValue.h>
@ -42,6 +43,30 @@ namespace Loki
class ScopeGuardImplBase 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. /// Copy-assignment operator is not implemented and private.
ScopeGuardImplBase& operator =(const ScopeGuardImplBase&); ScopeGuardImplBase& operator =(const ScopeGuardImplBase&);
@ -53,6 +78,7 @@ namespace Loki
/// Copy-constructor takes over responsibility from other ScopeGuard. /// Copy-constructor takes over responsibility from other ScopeGuard.
ScopeGuardImplBase(const ScopeGuardImplBase& other) throw() ScopeGuardImplBase(const ScopeGuardImplBase& other) throw()
: dismissed_(other.dismissed_) : dismissed_(other.dismissed_)
, exceptionPolicy_( other.exceptionPolicy_ )
{ {
other.Dismiss(); other.Dismiss();
} }
@ -60,7 +86,21 @@ namespace Loki
template <typename J> template <typename J>
static void SafeExecute(J& j) throw() 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_) if (!j.dismissed_)
{
try try
{ {
j.Execute(); j.Execute();
@ -68,17 +108,10 @@ namespace Loki
catch(...) catch(...)
{} {}
} }
}
mutable bool dismissed_; mutable bool dismissed_;
mutable ExceptionPolicy exceptionPolicy_;
public:
ScopeGuardImplBase() throw() : dismissed_(false)
{}
void Dismiss() const throw()
{
dismissed_ = true;
}
}; };
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////