CppUnit project page CppUnit home page

exceptionguard.h

Go to the documentation of this file.
00001 #ifndef CPPUT_TESTFAILUREGUARD_H_INCLUDED
00002 # define CPPUT_TESTFAILUREGUARD_H_INCLUDED
00003 
00004 # include <cpput/forwards.h>
00005 # include <cpptl/intrusiveptr.h>
00006 # include <deque>
00007 
00008 /* Tests setUp(), tearDown(), run() call are protected by a ExceptionGuard.
00009  *
00010  * This guard chain ensures that any exceptions thrown by the call is caught and properly handled.
00011  * The default guard chain handles AssertionException (thrown when an assertion fails),
00012  * std::exception, and any other exception.
00013  * 
00014  * A ExceptionGuardElement can be added to the guard chain to handle exception type unknown
00015  * to the test framework which are not derived from std::exception (MFC CException * for example).
00016  */
00017 
00018 namespace CppUT {
00019 
00023 class CPPUT_API ExceptionGuardElement : public CppTL::IntrusiveCount
00024 {
00025 public:
00026    struct Context;
00027 
00028    virtual ~ExceptionGuardElement()
00029    {
00030    }
00031 
00032    void setNextInChain( const ExceptionGuardElementPtr &deleguate );
00033 
00035    virtual bool protect( Context &context ) = 0;
00036 
00037 protected:
00038    bool callNextInChain( Context &context );
00039 
00040 private:
00041    ExceptionGuardElementPtr deleguate_;
00042 };
00043 
00044 
00050 template<class ExceptionType
00051         ,class Translator>
00052 class ExceptionTranslatorGuard : public ExceptionGuardElement
00053 {
00054 public:
00055    ExceptionTranslatorGuard( Translator translator )
00056       : translator_( translator )
00057    {
00058    }
00059 
00060    // overridden from ExceptionGuardElement
00061    bool protect( Context &context )
00062    {
00063       try
00064       {
00065          return callNextInChain( context );
00066       }
00067       catch ( const ExceptionType &e )
00068       {
00069          translator_( e );
00070          return false;
00071       }
00072    }
00073 private:
00074    Translator translator_;
00075 };
00076 
00077 
00082 class CPPUT_API ExceptionGuard
00083 {
00084 public:
00085    ExceptionGuard();
00086 
00087    void append( const ExceptionGuardElementPtr &guard );
00088 
00089    void removeLast();
00090 
00092    bool protect( CppTL::Functor0 test ) const;
00093 
00094 private:
00095    typedef std::deque<ExceptionGuardElementPtr> Guards;
00096    Guards guards_;
00097 };
00098 
00099 
00100 
00120 template<class Translator
00121         ,class ExceptionType>
00122 void registerExceptionTranslation( ExceptionGuard &guard,
00123                                    Translator translator,
00124                                    CppTL::Type<ExceptionType> )
00125 {
00126    typedef ExceptionTranslatorGuard<ExceptionType,Translator> GuardType;
00127    guard.append( ExceptionGuardElementPtr( new GuardType( translator ) ) );
00128 }
00129 
00130 
00131 } // namespace CppUT
00132 
00133 #endif // CPPUT_TESTFAILUREGUARD_H_INCLUDED

SourceForge Logo hosts this site. Send comments to:
CppUnit Developers