blob: 9c69958311b0a7e343e6aae87778ddfa57e17773 [file] [log] [blame]
Phil Nash9430a2c2011-04-20 15:40:40 +01001/*
Phil Nash9430a2c2011-04-20 15:40:40 +01002 * 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 Nash9430a2c2011-04-20 15:40:40 +01007 */
8#ifndef TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED
9#define TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED
10
11#include <string>
12
Phil Nashd0be9ed2012-05-15 08:02:36 +010013namespace Catch {
14
Phil Nash9430a2c2011-04-20 15:40:40 +010015 typedef std::string(*exceptionTranslateFunction)();
16
Phil Nashd0be9ed2012-05-15 08:02:36 +010017 struct IExceptionTranslator {
Phil Nash9430a2c2011-04-20 15:40:40 +010018 virtual ~IExceptionTranslator(){}
19 virtual std::string translate() const = 0;
20 };
21
Phil Nashd0be9ed2012-05-15 08:02:36 +010022 struct IExceptionTranslatorRegistry {
23 virtual ~IExceptionTranslatorRegistry(){}
Phil Nash9430a2c2011-04-20 15:40:40 +010024
Phil Nashd0be9ed2012-05-15 08:02:36 +010025 virtual void registerTranslator( IExceptionTranslator* translator ) = 0;
26 virtual std::string translateActiveException() const = 0;
Phil Nash9430a2c2011-04-20 15:40:40 +010027 };
28
Phil Nashd0be9ed2012-05-15 08:02:36 +010029 class ExceptionTranslatorRegistrar {
Phil Nash5d7b0542011-04-20 19:09:41 +010030 template<typename T>
Phil Nashd0be9ed2012-05-15 08:02:36 +010031 class ExceptionTranslator : public IExceptionTranslator {
Phil Nash5d7b0542011-04-20 19:09:41 +010032 public:
33
Phil Nashd0be9ed2012-05-15 08:02:36 +010034 ExceptionTranslator( std::string(*translateFunction)( T& ) )
Phil Nash5d7b0542011-04-20 19:09:41 +010035 : m_translateFunction( translateFunction )
36 {}
37
Phil Nashd0be9ed2012-05-15 08:02:36 +010038 virtual std::string translate() const {
39 try {
Phil Nash5d7b0542011-04-20 19:09:41 +010040 throw;
41 }
Phil Nashd0be9ed2012-05-15 08:02:36 +010042 catch( T& ex ) {
Phil Nash5d7b0542011-04-20 19:09:41 +010043 return m_translateFunction( ex );
44 }
45 }
46
47 protected:
48 std::string(*m_translateFunction)( T& );
49 };
50
Phil Nash9430a2c2011-04-20 15:40:40 +010051 public:
Phil Nash5d7b0542011-04-20 19:09:41 +010052 template<typename T>
Phil Nashd0be9ed2012-05-15 08:02:36 +010053 ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) {
Phil Nash5ec53b22012-05-10 07:58:48 +010054 Catch::Context::getExceptionTranslatorRegistry().registerTranslator
Phil Nash5d7b0542011-04-20 19:09:41 +010055 ( new ExceptionTranslator<T>( translateFunction ) );
Phil Nash9430a2c2011-04-20 15:40:40 +010056 }
Phil Nash9430a2c2011-04-20 15:40:40 +010057 };
58}
59
Phil Nash5d7b0542011-04-20 19:09:41 +010060///////////////////////////////////////////////////////////////////////////////
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 Nash9430a2c2011-04-20 15:40:40 +010066#endif // TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED