| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | namespace 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 Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame^] | 41 | class ExceptionTranslatorRegistrar |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 42 | { |
| Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame^] | 43 | 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 Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 73 | public: |
| Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame^] | 74 | template<typename T> |
| 75 | ExceptionTranslatorRegistrar |
| 76 | ( |
| 77 | std::string(*translateFunction)( T& ) |
| 78 | ) |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 79 | { |
| Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame^] | 80 | Catch::Hub::getExceptionTranslatorRegistry().registerTranslator |
| 81 | ( new ExceptionTranslator<T>( translateFunction ) ); |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 82 | } |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 83 | }; |
| 84 | } |
| 85 | |
| Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame^] | 86 | /////////////////////////////////////////////////////////////////////////////// |
| 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 Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 92 | #endif // TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED |