From 2a31073734be6e44e477079699578820282b7345 Mon Sep 17 00:00:00 2001 From: Baptiste Lepilleur Date: Tue, 16 Jul 2002 21:59:22 +0000 Subject: 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. --- src/cppunit/ProtectorChain.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/cppunit/ProtectorChain.cpp (limited to 'src/cppunit/ProtectorChain.cpp') 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 -- cgit v1.2.1