| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 1 | /* |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 2 | * Created by Phil on 20/04/2011. |
| 3 | * Copyright 2011 Two Blue Cubes Ltd. All rights reserved. |
| 4 | * |
| 5 | * Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 7 | */ |
| 8 | #ifndef TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED |
| 9 | #define TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED |
| 10 | |
| 11 | #include <string> |
| Phil Nash | da0ae95 | 2012-08-07 07:58:34 +0100 | [diff] [blame] | 12 | #include "catch_interfaces_registry_hub.h" |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 13 | |
| Phil Nash | d0be9ed | 2012-05-15 08:02:36 +0100 | [diff] [blame] | 14 | namespace Catch { |
| 15 | |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 16 | typedef std::string(*exceptionTranslateFunction)(); |
| 17 | |
| Phil Nash | d0be9ed | 2012-05-15 08:02:36 +0100 | [diff] [blame] | 18 | struct IExceptionTranslator { |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 19 | virtual ~IExceptionTranslator(){} |
| 20 | virtual std::string translate() const = 0; |
| 21 | }; |
| 22 | |
| Phil Nash | d0be9ed | 2012-05-15 08:02:36 +0100 | [diff] [blame] | 23 | struct IExceptionTranslatorRegistry { |
| 24 | virtual ~IExceptionTranslatorRegistry(){} |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 25 | |
| Phil Nash | d0be9ed | 2012-05-15 08:02:36 +0100 | [diff] [blame] | 26 | virtual std::string translateActiveException() const = 0; |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 27 | }; |
| 28 | |
| Phil Nash | d0be9ed | 2012-05-15 08:02:36 +0100 | [diff] [blame] | 29 | class ExceptionTranslatorRegistrar { |
| Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 30 | template<typename T> |
| Phil Nash | d0be9ed | 2012-05-15 08:02:36 +0100 | [diff] [blame] | 31 | class ExceptionTranslator : public IExceptionTranslator { |
| Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 32 | public: |
| 33 | |
| Phil Nash | d0be9ed | 2012-05-15 08:02:36 +0100 | [diff] [blame] | 34 | ExceptionTranslator( std::string(*translateFunction)( T& ) ) |
| Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 35 | : m_translateFunction( translateFunction ) |
| 36 | {} |
| 37 | |
| Phil Nash | d0be9ed | 2012-05-15 08:02:36 +0100 | [diff] [blame] | 38 | virtual std::string translate() const { |
| 39 | try { |
| Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 40 | throw; |
| 41 | } |
| Phil Nash | d0be9ed | 2012-05-15 08:02:36 +0100 | [diff] [blame] | 42 | catch( T& ex ) { |
| Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 43 | return m_translateFunction( ex ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | protected: |
| 48 | std::string(*m_translateFunction)( T& ); |
| 49 | }; |
| 50 | |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 51 | public: |
| Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 52 | template<typename T> |
| Phil Nash | d0be9ed | 2012-05-15 08:02:36 +0100 | [diff] [blame] | 53 | ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) { |
| Phil Nash | da0ae95 | 2012-08-07 07:58:34 +0100 | [diff] [blame] | 54 | getMutableRegistryHub().registerTranslator |
| Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 55 | ( new ExceptionTranslator<T>( translateFunction ) ); |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 56 | } |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 57 | }; |
| 58 | } |
| 59 | |
| Phil Nash | 5d7b054 | 2011-04-20 19:09:41 +0100 | [diff] [blame] | 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | #define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) \ |
| 62 | static std::string INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator )( signature ); \ |
| 63 | namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ) ); }\ |
| 64 | static std::string INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator )( signature ) |
| 65 | |
| Phil Nash | 9430a2c | 2011-04-20 15:40:40 +0100 | [diff] [blame] | 66 | #endif // TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED |