blob: 0e17fa36722d4e08443c6b2b5c41601b151a4b92 [file] [log] [blame]
Phil Nash9430a2c2011-04-20 15:40:40 +01001/*
2 * catch_exception_interfaces.h
3 * Catch
4 *
5 * Created by Phil on 20/04/2011.
6 * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
7 *
8 * Distributed under the Boost Software License, Version 1.0. (See accompanying
9 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 *
11 */
12#ifndef TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED
13#define TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED
14
15#include <string>
16
17namespace Catch
18{
19 typedef std::string(*exceptionTranslateFunction)();
20
21 struct IExceptionTranslator
22 {
23 virtual ~IExceptionTranslator(){}
24 virtual std::string translate() const = 0;
25 };
26
27 struct IExceptionTranslatorRegistry
28 {
29 virtual ~IExceptionTranslatorRegistry
30 ()
31 {}
32
33 virtual void registerTranslator
34 ( IExceptionTranslator* translator
35 ) = 0;
36 virtual std::string translateActiveException
37 () const = 0;
38
39 };
40
Phil Nash5d7b0542011-04-20 19:09:41 +010041 class ExceptionTranslatorRegistrar
Phil Nash9430a2c2011-04-20 15:40:40 +010042 {
Phil Nash5d7b0542011-04-20 19:09:41 +010043 template<typename T>
44 class ExceptionTranslator : public IExceptionTranslator
45 {
46 public:
47
48 ExceptionTranslator
49 (
50 std::string(*translateFunction)( T& )
51 )
52 : m_translateFunction( translateFunction )
53 {}
54
55 virtual std::string translate
56 ()
57 const
58 {
59 try
60 {
61 throw;
62 }
63 catch( T& ex )
64 {
65 return m_translateFunction( ex );
66 }
67 }
68
69 protected:
70 std::string(*m_translateFunction)( T& );
71 };
72
Phil Nash9430a2c2011-04-20 15:40:40 +010073 public:
Phil Nash5d7b0542011-04-20 19:09:41 +010074 template<typename T>
75 ExceptionTranslatorRegistrar
76 (
77 std::string(*translateFunction)( T& )
78 )
Phil Nash9430a2c2011-04-20 15:40:40 +010079 {
Phil Nash5d7b0542011-04-20 19:09:41 +010080 Catch::Hub::getExceptionTranslatorRegistry().registerTranslator
81 ( new ExceptionTranslator<T>( translateFunction ) );
Phil Nash9430a2c2011-04-20 15:40:40 +010082 }
Phil Nash9430a2c2011-04-20 15:40:40 +010083 };
84}
85
Phil Nash5d7b0542011-04-20 19:09:41 +010086///////////////////////////////////////////////////////////////////////////////
87#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) \
88 static std::string INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator )( signature ); \
89 namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ) ); }\
90 static std::string INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator )( signature )
91
Phil Nash9430a2c2011-04-20 15:40:40 +010092#endif // TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED