diff options
| author | Martin Hořeňovský | 2020-05-19 21:16:33 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2020-05-20 08:15:11 +0200 |
| commit | 4b2f1da02a37d111b572551f4bd8ed765b06ca28 (patch) | |
| tree | 96be40b8dd699fddb8ec1def8fc344aaa9cea7df | |
| parent | 0c6fda6e7dfbd1a46ad02b692eb90668575df9dc (diff) | |
| download | catch3-4b2f1da02a37d111b572551f4bd8ed765b06ca28.tar.gz | |
Split CATCH_TRANSLATE_EXCEPTION into its own header
As far as I know, only a few users actually use it, but these changes
allow us to avoid including a surprising amount of code in the main
compilation path.
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/catch2/catch_all.hpp | 1 | ||||
| -rw-r--r-- | src/catch2/catch_test_macros.hpp | 8 | ||||
| -rw-r--r-- | src/catch2/catch_translate_exception.hpp | 74 | ||||
| -rw-r--r-- | src/catch2/interfaces/catch_interfaces_exception.hpp | 55 | ||||
| -rw-r--r-- | tests/SelfTest/UsageTests/Exception.tests.cpp | 1 |
6 files changed, 78 insertions, 62 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bc7a6753..4f81c000 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -123,6 +123,7 @@ set(INTERNAL_HEADERS ${SOURCES_DIR}/internal/catch_to_string.hpp ${SOURCES_DIR}/catch_tostring.hpp ${SOURCES_DIR}/catch_totals.hpp + ${SOURCES_DIR}/catch_translate_exception.hpp ${SOURCES_DIR}/internal/catch_uncaught_exceptions.hpp ${SOURCES_DIR}/catch_version.hpp ${SOURCES_DIR}/catch_version_macros.hpp diff --git a/src/catch2/catch_all.hpp b/src/catch2/catch_all.hpp index 019c1bfe..b0f31196 100644 --- a/src/catch2/catch_all.hpp +++ b/src/catch2/catch_all.hpp @@ -33,6 +33,7 @@ #include <catch2/catch_timer.hpp> #include <catch2/catch_tostring.hpp> #include <catch2/catch_totals.hpp> +#include <catch2/catch_translate_exception.hpp> #include <catch2/catch_version.hpp> #include <catch2/catch_version_macros.hpp> #include <catch2/generators/catch_generators_all.hpp> diff --git a/src/catch2/catch_test_macros.hpp b/src/catch2/catch_test_macros.hpp index 2f1c637f..2514d200 100644 --- a/src/catch2/catch_test_macros.hpp +++ b/src/catch2/catch_test_macros.hpp @@ -8,7 +8,6 @@ #include <catch2/internal/catch_test_macro_impl.hpp> #include <catch2/catch_message.hpp> -#include <catch2/interfaces/catch_interfaces_exception.hpp> #include <catch2/internal/catch_preprocessor.hpp> #include <catch2/internal/catch_section.hpp> #include <catch2/internal/catch_test_registry.hpp> @@ -202,13 +201,6 @@ #endif // ^^ unprefixed, disabled -// This macro is always prefixed -#if !defined(CATCH_CONFIG_DISABLE) - #define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) -#else - #define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) -#endif - // end of user facing macros #endif // TWOBLUECUBES_CATCH_TEST_MACROS_HPP_INCLUDED diff --git a/src/catch2/catch_translate_exception.hpp b/src/catch2/catch_translate_exception.hpp new file mode 100644 index 00000000..0b5a3747 --- /dev/null +++ b/src/catch2/catch_translate_exception.hpp @@ -0,0 +1,74 @@ +#ifndef CATCH_TRANSLATE_EXCEPTION_HPP_INCLUDED +#define CATCH_TRANSLATE_EXCEPTION_HPP_INCLUDED + +#include <catch2/interfaces/catch_interfaces_exception.hpp> + +#include <exception> + +namespace Catch { + + class ExceptionTranslatorRegistrar { + template<typename T> + class ExceptionTranslator : public IExceptionTranslator { + public: + + ExceptionTranslator( std::string(*translateFunction)( T& ) ) + : m_translateFunction( translateFunction ) + {} + + std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const override { +#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) + try { + if( it == itEnd ) + std::rethrow_exception(std::current_exception()); + else + return (*it)->translate( it+1, itEnd ); + } + catch( T& ex ) { + return m_translateFunction( ex ); + } +#else + return "You should never get here!"; +#endif + } + + protected: + std::string(*m_translateFunction)( T& ); + }; + + public: + template<typename T> + ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) { + getMutableRegistryHub().registerTranslator + ( new ExceptionTranslator<T>( translateFunction ) ); + } + }; + +} // namespace Catch + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_TRANSLATE_EXCEPTION2( translatorName, signature ) \ + static std::string translatorName( signature ); \ + CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); } \ + CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \ + static std::string translatorName( signature ) + +#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) + +#if defined(CATCH_CONFIG_DISABLE) + #define INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( translatorName, signature) \ + static std::string translatorName( signature ) +#endif + + +// This macro is always prefixed +#if !defined(CATCH_CONFIG_DISABLE) +#define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) +#else +#define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) +#endif + + +#endif // CATCH_TRANSLATE_EXCEPTION_HPP_INCLUDED
\ No newline at end of file diff --git a/src/catch2/interfaces/catch_interfaces_exception.hpp b/src/catch2/interfaces/catch_interfaces_exception.hpp index 3104ef14..e9fb9ef7 100644 --- a/src/catch2/interfaces/catch_interfaces_exception.hpp +++ b/src/catch2/interfaces/catch_interfaces_exception.hpp @@ -11,12 +11,6 @@ #include <catch2/interfaces/catch_interfaces_registry_hub.hpp> #include <catch2/internal/catch_compiler_capabilities.hpp> -#if defined(CATCH_CONFIG_DISABLE) - #define INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( translatorName, signature) \ - static std::string translatorName( signature ) -#endif - -#include <exception> #include <string> #include <vector> @@ -37,53 +31,6 @@ namespace Catch { virtual std::string translateActiveException() const = 0; }; - class ExceptionTranslatorRegistrar { - template<typename T> - class ExceptionTranslator : public IExceptionTranslator { - public: - - ExceptionTranslator( std::string(*translateFunction)( T& ) ) - : m_translateFunction( translateFunction ) - {} - - std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const override { -#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) - try { - if( it == itEnd ) - std::rethrow_exception(std::current_exception()); - else - return (*it)->translate( it+1, itEnd ); - } - catch( T& ex ) { - return m_translateFunction( ex ); - } -#else - return "You should never get here!"; -#endif - } - - protected: - std::string(*m_translateFunction)( T& ); - }; - - public: - template<typename T> - ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) { - getMutableRegistryHub().registerTranslator - ( new ExceptionTranslator<T>( translateFunction ) ); - } - }; -} - -/////////////////////////////////////////////////////////////////////////////// -#define INTERNAL_CATCH_TRANSLATE_EXCEPTION2( translatorName, signature ) \ - static std::string translatorName( signature ); \ - CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ - CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ - namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); } \ - CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \ - static std::string translatorName( signature ) - -#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) +} // namespace Catch #endif // TWOBLUECUBES_CATCH_INTERFACES_EXCEPTION_H_INCLUDED diff --git a/tests/SelfTest/UsageTests/Exception.tests.cpp b/tests/SelfTest/UsageTests/Exception.tests.cpp index 8fb3e2f0..f8c628b9 100644 --- a/tests/SelfTest/UsageTests/Exception.tests.cpp +++ b/tests/SelfTest/UsageTests/Exception.tests.cpp @@ -4,6 +4,7 @@ */ #include <catch2/catch_test_macros.hpp> +#include <catch2/catch_translate_exception.hpp> #include <catch2/matchers/catch_matchers_string.hpp> #include <string> |