blob: 7614612982d502059392e640ec9c101ae260955f [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>
Phil Nashda0ae952012-08-07 07:58:34 +010012#include "catch_interfaces_registry_hub.h"
Phil Nash9430a2c2011-04-20 15:40:40 +010013
Phil Nashd0be9ed2012-05-15 08:02:36 +010014namespace Catch {
15
Phil Nash9430a2c2011-04-20 15:40:40 +010016 typedef std::string(*exceptionTranslateFunction)();
17
Phil Nashd0be9ed2012-05-15 08:02:36 +010018 struct IExceptionTranslator {
Phil Nasha695eb92012-08-13 07:46:10 +010019 virtual ~IExceptionTranslator();
Phil Nash9430a2c2011-04-20 15:40:40 +010020 virtual std::string translate() const = 0;
21 };
22
Phil Nashd0be9ed2012-05-15 08:02:36 +010023 struct IExceptionTranslatorRegistry {
Phil Nasha695eb92012-08-13 07:46:10 +010024 virtual ~IExceptionTranslatorRegistry();
Phil Nash9430a2c2011-04-20 15:40:40 +010025
Phil Nashd0be9ed2012-05-15 08:02:36 +010026 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 Nashda0ae952012-08-07 07:58:34 +010054 getMutableRegistryHub().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