diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2002-07-16 21:59:22 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2002-07-16 21:59:22 +0000 |
| commit | 2a31073734be6e44e477079699578820282b7345 (patch) | |
| tree | 70b18c3fc851173fe5fd69a7d844640ab6b780d2 /src/cppunit/ProtectorChain.cpp | |
| parent | 251c1ff8aecaa608ef9e6041c2691d369430bf7b (diff) | |
| download | cppunit-2a31073734be6e44e477079699578820282b7345.tar.gz | |
Include/cppunit/Protector.
include/cppunit/Protector.h:
* src/cppunit/Protector.cpp: added. Base class for protectors.
* src/cppunit/DefaultProtector.h:
* src/cppunit/DefaultProtector.cpp: added. Implementation of the default
protector used to catch std::exception and any other exception.
* src/cppunit/ProtectorChain.h:
* src/cppunit/ProtectorChain.cpp: added. Implementation of a chain of
protector, allowing catching custom exception and implementation of
expected exception.
* src/cppunit/TestCase.cpp:
* src/cppunit/TestResult.cpp: updated to use protector.
Diffstat (limited to 'src/cppunit/ProtectorChain.cpp')
| -rw-r--r-- | src/cppunit/ProtectorChain.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/cppunit/ProtectorChain.cpp b/src/cppunit/ProtectorChain.cpp new file mode 100644 index 0000000..087b938 --- /dev/null +++ b/src/cppunit/ProtectorChain.cpp @@ -0,0 +1,89 @@ +#include "ProtectorChain.h" + +CPPUNIT_NS_BEGIN + + +class ProtectorChain::ProtectFunctor : public Functor +{ +public: + ProtectFunctor( Protector *protector, + const Functor &functor, + const ProtectorContext &context ) + : m_protector( protector ) + , m_functor( functor ) + , m_context( context ) + { + } + + bool operator()() const + { + return m_protector->protect( m_functor, m_context ); + } + +private: + Protector *m_protector; + const Functor &m_functor; + const ProtectorContext &m_context; +}; + + +ProtectorChain::~ProtectorChain() +{ + while ( count() > 0 ) + pop(); +} + + +void +ProtectorChain::push( Protector *protector ) +{ + m_protectors.push_back( protector ); +} + + +void +ProtectorChain::pop() +{ + delete m_protectors.back(); + m_protectors.pop_back(); +} + +int +ProtectorChain::count() const +{ + return m_protectors.size(); +} + + +bool +ProtectorChain::protect( const Functor &functor, + const ProtectorContext &context ) +{ + if ( m_protectors.empty() ) + { + functor(); + return true; + } + + Functors functors; + for ( int index = 0; index < m_protectors.size(); ++index ) + { + const Functor &protectedFunctor = + functors.empty() ? functor : *functors.back(); + + functors.push_back( new ProtectFunctor( m_protectors[index], + protectedFunctor, + context ) ); + } + + const Functor &outermostFunctor = *functors.back(); + bool succeed = outermostFunctor(); + + for ( int deletingIndex = 0; deletingIndex < m_protectors.size(); ++deletingIndex ) + delete functors[deletingIndex]; + + return succeed; +} + + +CPPUNIT_NS_END |
