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)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED
|
|
|
|
|
|
|
|
#include <string>
|
2012-08-06 21:16:53 +02:00
|
|
|
#include "catch_interfaces_static_registries.h"
|
2011-04-20 16:40:40 +02:00
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
2011-04-20 16:40:40 +02:00
|
|
|
typedef std::string(*exceptionTranslateFunction)();
|
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
struct IExceptionTranslator {
|
2011-04-20 16:40:40 +02:00
|
|
|
virtual ~IExceptionTranslator(){}
|
|
|
|
virtual std::string translate() const = 0;
|
|
|
|
};
|
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
struct IExceptionTranslatorRegistry {
|
|
|
|
virtual ~IExceptionTranslatorRegistry(){}
|
2011-04-20 16:40:40 +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:
|
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
ExceptionTranslator( std::string(*translateFunction)( T& ) )
|
2011-04-20 20:09:41 +02:00
|
|
|
: m_translateFunction( translateFunction )
|
|
|
|
{}
|
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
virtual std::string translate() const {
|
|
|
|
try {
|
2011-04-20 20:09:41 +02:00
|
|
|
throw;
|
|
|
|
}
|
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
|
|
|
}
|
2011-04-20 20:09:41 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
std::string(*m_translateFunction)( T& );
|
|
|
|
};
|
2011-04-20 16:40:40 +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-06 21:16:53 +02:00
|
|
|
getStaticRegistries().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
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) \
|
|
|
|
static std::string INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator )( signature ); \
|
|
|
|
namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ) ); }\
|
|
|
|
static std::string INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator )( signature )
|
|
|
|
|
2011-04-20 16:40:40 +02:00
|
|
|
#endif // TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED
|