File indexing completed on 2025-01-18 09:54:08
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef CATCH_TRANSLATE_EXCEPTION_HPP_INCLUDED
0009 #define CATCH_TRANSLATE_EXCEPTION_HPP_INCLUDED
0010
0011 #include <catch2/interfaces/catch_interfaces_exception.hpp>
0012 #include <catch2/internal/catch_compiler_capabilities.hpp>
0013 #include <catch2/internal/catch_unique_name.hpp>
0014
0015 #include <exception>
0016
0017 namespace Catch {
0018 namespace Detail {
0019 void registerTranslatorImpl(
0020 Detail::unique_ptr<IExceptionTranslator>&& translator );
0021 }
0022
0023 class ExceptionTranslatorRegistrar {
0024 template<typename T>
0025 class ExceptionTranslator : public IExceptionTranslator {
0026 public:
0027
0028 ExceptionTranslator( std::string(*translateFunction)( T const& ) )
0029 : m_translateFunction( translateFunction )
0030 {}
0031
0032 std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const override {
0033 #if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
0034 try {
0035 if( it == itEnd )
0036 std::rethrow_exception(std::current_exception());
0037 else
0038 return (*it)->translate( it+1, itEnd );
0039 }
0040 catch( T const& ex ) {
0041 return m_translateFunction( ex );
0042 }
0043 #else
0044 return "You should never get here!";
0045 #endif
0046 }
0047
0048 protected:
0049 std::string(*m_translateFunction)( T const& );
0050 };
0051
0052 public:
0053 template<typename T>
0054 ExceptionTranslatorRegistrar( std::string(*translateFunction)( T const& ) ) {
0055 Detail::registerTranslatorImpl(
0056 Detail::make_unique<ExceptionTranslator<T>>(
0057 translateFunction ) );
0058 }
0059 };
0060
0061 }
0062
0063
0064 #define INTERNAL_CATCH_TRANSLATE_EXCEPTION2( translatorName, signature ) \
0065 static std::string translatorName( signature ); \
0066 CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \
0067 CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \
0068 namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); } \
0069 CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \
0070 static std::string translatorName( signature )
0071
0072 #define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature )
0073
0074 #if defined(CATCH_CONFIG_DISABLE)
0075 #define INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( translatorName, signature) \
0076 static std::string translatorName( signature )
0077 #endif
0078
0079
0080
0081 #if !defined(CATCH_CONFIG_DISABLE)
0082 #define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature )
0083 #else
0084 #define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature )
0085 #endif
0086
0087
0088 #endif