2011-04-20 16:40:40 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil on 20/04/2011.
|
|
|
|
* Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
2012-09-17 07:42:29 +02:00
|
|
|
#ifndef TWOBLUECUBES_CATCH_INTERFACES_EXCEPTION_H_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_INTERFACES_EXCEPTION_H_INCLUDED
|
2011-04-20 16:40:40 +02:00
|
|
|
|
2017-08-29 09:52:25 +02:00
|
|
|
#include "catch_interfaces_registry_hub.h"
|
|
|
|
|
|
|
|
#if defined(CATCH_CONFIG_DISABLE)
|
|
|
|
#define INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( translatorName, signature) \
|
|
|
|
static std::string translatorName( signature )
|
|
|
|
#endif
|
|
|
|
|
2017-09-28 12:47:32 +02:00
|
|
|
#include <exception>
|
2011-04-20 16:40:40 +02:00
|
|
|
#include <string>
|
2015-11-18 09:39:21 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
namespace Catch {
|
2017-07-27 12:24:21 +02:00
|
|
|
using exceptionTranslateFunction = std::string(*)();
|
2011-04-20 16:40:40 +02:00
|
|
|
|
2015-11-18 09:39:21 +01:00
|
|
|
struct IExceptionTranslator;
|
2017-07-27 12:24:21 +02:00
|
|
|
using ExceptionTranslators = std::vector<std::unique_ptr<IExceptionTranslator const>>;
|
2015-12-04 11:20:33 +01:00
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
struct IExceptionTranslator {
|
2012-08-13 08:46:10 +02:00
|
|
|
virtual ~IExceptionTranslator();
|
2015-11-18 09:39:21 +01:00
|
|
|
virtual std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const = 0;
|
2011-04-20 16:40:40 +02:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
struct IExceptionTranslatorRegistry {
|
2012-08-13 08:46:10 +02:00
|
|
|
virtual ~IExceptionTranslatorRegistry();
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
virtual std::string translateActiveException() const = 0;
|
2011-04-20 16:40:40 +02:00
|
|
|
};
|
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
class ExceptionTranslatorRegistrar {
|
2011-04-20 20:09:41 +02:00
|
|
|
template<typename T>
|
2012-05-15 09:02:36 +02:00
|
|
|
class ExceptionTranslator : public IExceptionTranslator {
|
2011-04-20 20:09:41 +02:00
|
|
|
public:
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
ExceptionTranslator( std::string(*translateFunction)( T& ) )
|
2011-04-20 20:09:41 +02:00
|
|
|
: m_translateFunction( translateFunction )
|
|
|
|
{}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-07-21 00:50:07 +02:00
|
|
|
std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const override {
|
2012-05-15 09:02:36 +02:00
|
|
|
try {
|
2015-11-18 09:39:21 +01:00
|
|
|
if( it == itEnd )
|
2017-09-28 12:47:32 +02:00
|
|
|
std::rethrow_exception(std::current_exception());
|
2015-11-18 09:39:21 +01:00
|
|
|
else
|
|
|
|
return (*it)->translate( it+1, itEnd );
|
2011-04-20 20:09:41 +02:00
|
|
|
}
|
2012-05-15 09:02:36 +02:00
|
|
|
catch( T& ex ) {
|
2011-04-20 20:09:41 +02:00
|
|
|
return m_translateFunction( ex );
|
|
|
|
}
|
2011-04-20 16:40:40 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2011-04-20 20:09:41 +02:00
|
|
|
protected:
|
|
|
|
std::string(*m_translateFunction)( T& );
|
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2011-04-20 20:09:41 +02:00
|
|
|
public:
|
|
|
|
template<typename T>
|
2012-05-15 09:02:36 +02:00
|
|
|
ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) {
|
2012-08-07 08:58:34 +02:00
|
|
|
getMutableRegistryHub().registerTranslator
|
2011-04-20 20:09:41 +02:00
|
|
|
( new ExceptionTranslator<T>( translateFunction ) );
|
|
|
|
}
|
2011-04-20 16:40:40 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-04-20 20:09:41 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-12-15 08:50:51 +01:00
|
|
|
#define INTERNAL_CATCH_TRANSLATE_EXCEPTION2( translatorName, signature ) \
|
|
|
|
static std::string translatorName( signature ); \
|
2017-11-29 17:14:33 +01:00
|
|
|
CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \
|
|
|
|
namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); } \
|
|
|
|
CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \
|
2015-12-15 08:50:51 +01:00
|
|
|
static std::string translatorName( signature )
|
|
|
|
|
|
|
|
#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature )
|
2011-04-20 20:09:41 +02:00
|
|
|
|
2012-09-17 07:42:29 +02:00
|
|
|
#endif // TWOBLUECUBES_CATCH_INTERFACES_EXCEPTION_H_INCLUDED
|