| 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 | |
| 41 | template<typename T> |
| 42 | class ExceptionTranslator : public IExceptionTranslator |
| 43 | { |
| 44 | public: |
| 45 | ExceptionTranslator() |
| 46 | { |
| 47 | Catch::Hub::getExceptionTranslatorRegistry().registerTranslator( this ); |
| 48 | } |
| 49 | |
| 50 | virtual std::string translate() const |
| 51 | { |
| 52 | try |
| 53 | { |
| 54 | throw; |
| 55 | } |
| 56 | catch( T& ex ) |
| 57 | { |
| 58 | return translate( ex ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | std::string translate( T& ex ) const; |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | #endif // TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED |