From cfe859e0f330eecf92b606b0062b7fa8e9624458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Tue, 14 Mar 2023 19:56:59 +0100 Subject: [PATCH] Unify IExceptionTranslatorRegistry and ExceptionTranslatorRegistry --- src/CMakeLists.txt | 1 + src/catch2/catch_registry_hub.cpp | 2 +- src/catch2/catch_translate_exception.cpp | 20 +++++++++ src/catch2/catch_translate_exception.hpp | 9 ++-- .../interfaces/catch_interfaces_exception.cpp | 1 - .../interfaces/catch_interfaces_exception.hpp | 9 ---- .../catch_interfaces_registry_hub.hpp | 4 +- .../catch_exception_translator_registry.cpp | 41 ++++++++++++------- .../catch_exception_translator_registry.hpp | 17 ++++---- src/catch2/meson.build | 1 + 10 files changed, 65 insertions(+), 40 deletions(-) create mode 100644 src/catch2/catch_translate_exception.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5a9c105f..9f750031 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -153,6 +153,7 @@ set(IMPL_SOURCES ${SOURCES_DIR}/catch_timer.cpp ${SOURCES_DIR}/catch_tostring.cpp ${SOURCES_DIR}/catch_totals.cpp + ${SOURCES_DIR}/catch_translate_exception.cpp ${SOURCES_DIR}/catch_version.cpp ${SOURCES_DIR}/internal/catch_assertion_handler.cpp ${SOURCES_DIR}/internal/catch_case_insensitive_comparisons.cpp diff --git a/src/catch2/catch_registry_hub.cpp b/src/catch2/catch_registry_hub.cpp index 17390bc0..d12c7ba7 100644 --- a/src/catch2/catch_registry_hub.cpp +++ b/src/catch2/catch_registry_hub.cpp @@ -38,7 +38,7 @@ namespace Catch { ITestCaseRegistry const& getTestCaseRegistry() const override { return m_testCaseRegistry; } - IExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const override { + ExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const override { return m_exceptionTranslatorRegistry; } ITagAliasRegistry const& getTagAliasRegistry() const override { diff --git a/src/catch2/catch_translate_exception.cpp b/src/catch2/catch_translate_exception.cpp new file mode 100644 index 00000000..c4b28944 --- /dev/null +++ b/src/catch2/catch_translate_exception.cpp @@ -0,0 +1,20 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +#include +#include + +namespace Catch { + namespace Detail { + void registerTranslatorImpl( + Detail::unique_ptr&& translator ) { + getMutableRegistryHub().registerTranslator( + CATCH_MOVE( translator ) ); + } + } // namespace Detail +} // namespace Catch diff --git a/src/catch2/catch_translate_exception.hpp b/src/catch2/catch_translate_exception.hpp index 2dbeb17e..95628d50 100644 --- a/src/catch2/catch_translate_exception.hpp +++ b/src/catch2/catch_translate_exception.hpp @@ -15,6 +15,10 @@ #include namespace Catch { + namespace Detail { + void registerTranslatorImpl( + Detail::unique_ptr&& translator ); + } class ExceptionTranslatorRegistrar { template @@ -48,9 +52,8 @@ namespace Catch { public: template ExceptionTranslatorRegistrar( std::string(*translateFunction)( T const& ) ) { - getMutableRegistryHub().registerTranslator( - Detail::make_unique>(translateFunction) - ); + Detail::registerTranslatorImpl( Detail::make_unique>( + translateFunction ) ); } }; diff --git a/src/catch2/interfaces/catch_interfaces_exception.cpp b/src/catch2/interfaces/catch_interfaces_exception.cpp index 44c272d2..4e85047c 100644 --- a/src/catch2/interfaces/catch_interfaces_exception.cpp +++ b/src/catch2/interfaces/catch_interfaces_exception.cpp @@ -10,5 +10,4 @@ namespace Catch { IExceptionTranslator::~IExceptionTranslator() = default; - IExceptionTranslatorRegistry::~IExceptionTranslatorRegistry() = default; } diff --git a/src/catch2/interfaces/catch_interfaces_exception.hpp b/src/catch2/interfaces/catch_interfaces_exception.hpp index 9177666a..50d4ff11 100644 --- a/src/catch2/interfaces/catch_interfaces_exception.hpp +++ b/src/catch2/interfaces/catch_interfaces_exception.hpp @@ -8,15 +8,12 @@ #ifndef CATCH_INTERFACES_EXCEPTION_HPP_INCLUDED #define CATCH_INTERFACES_EXCEPTION_HPP_INCLUDED -#include #include #include #include namespace Catch { - using exceptionTranslateFunction = std::string(*)(); - class IExceptionTranslator; using ExceptionTranslators = std::vector>; @@ -26,12 +23,6 @@ namespace Catch { virtual std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const = 0; }; - class IExceptionTranslatorRegistry { - public: - virtual ~IExceptionTranslatorRegistry(); // = default - virtual std::string translateActiveException() const = 0; - }; - } // namespace Catch #endif // CATCH_INTERFACES_EXCEPTION_HPP_INCLUDED diff --git a/src/catch2/interfaces/catch_interfaces_registry_hub.hpp b/src/catch2/interfaces/catch_interfaces_registry_hub.hpp index ebd5192d..d225e48a 100644 --- a/src/catch2/interfaces/catch_interfaces_registry_hub.hpp +++ b/src/catch2/interfaces/catch_interfaces_registry_hub.hpp @@ -17,7 +17,7 @@ namespace Catch { class TestCaseHandle; struct TestCaseInfo; class ITestCaseRegistry; - class IExceptionTranslatorRegistry; + class ExceptionTranslatorRegistry; class IExceptionTranslator; class ReporterRegistry; class IReporterFactory; @@ -38,7 +38,7 @@ namespace Catch { virtual ReporterRegistry const& getReporterRegistry() const = 0; virtual ITestCaseRegistry const& getTestCaseRegistry() const = 0; virtual ITagAliasRegistry const& getTagAliasRegistry() const = 0; - virtual IExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const = 0; + virtual ExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const = 0; virtual StartupExceptionRegistry const& getStartupExceptionRegistry() const = 0; diff --git a/src/catch2/internal/catch_exception_translator_registry.cpp b/src/catch2/internal/catch_exception_translator_registry.cpp index 0645c6ce..2f173563 100644 --- a/src/catch2/internal/catch_exception_translator_registry.cpp +++ b/src/catch2/internal/catch_exception_translator_registry.cpp @@ -6,18 +6,41 @@ // SPDX-License-Identifier: BSL-1.0 #include +#include #include #include #include #include +#include + namespace Catch { - ExceptionTranslatorRegistry::~ExceptionTranslatorRegistry() { + namespace { + static std::string tryTranslators( + std::vector> const& + translators ) { + if ( translators.empty() ) { + std::rethrow_exception( std::current_exception() ); + } else { + return translators[0]->translate( translators.begin() + 1, + translators.end() ); + } + } } + + struct ExceptionTranslatorRegistry::ExceptionTranslatorRegistryImpl { + std::vector> + translators; + }; + + ExceptionTranslatorRegistry::ExceptionTranslatorRegistry(): + m_impl( Detail::make_unique() ) {} + ExceptionTranslatorRegistry::~ExceptionTranslatorRegistry() = default; + void ExceptionTranslatorRegistry::registerTranslator( Detail::unique_ptr&& translator ) { - m_translators.push_back( CATCH_MOVE( translator ) ); + m_impl->translators.push_back( CATCH_MOVE( translator ) ); } #if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) @@ -37,7 +60,7 @@ namespace Catch { // First we try user-registered translators. If none of them can // handle the exception, it will be rethrown handled by our defaults. try { - return tryTranslators(); + return tryTranslators(m_impl->translators); } // To avoid having to handle TFE explicitly everywhere, we just // rethrow it so that it goes back up the caller. @@ -61,22 +84,10 @@ namespace Catch { } } - std::string ExceptionTranslatorRegistry::tryTranslators() const { - if (m_translators.empty()) { - std::rethrow_exception(std::current_exception()); - } else { - return m_translators[0]->translate(m_translators.begin() + 1, m_translators.end()); - } - } - #else // ^^ Exceptions are enabled // Exceptions are disabled vv std::string ExceptionTranslatorRegistry::translateActiveException() const { CATCH_INTERNAL_ERROR("Attempted to translate active exception under CATCH_CONFIG_DISABLE_EXCEPTIONS!"); } - - std::string ExceptionTranslatorRegistry::tryTranslators() const { - CATCH_INTERNAL_ERROR("Attempted to use exception translators under CATCH_CONFIG_DISABLE_EXCEPTIONS!"); - } #endif diff --git a/src/catch2/internal/catch_exception_translator_registry.hpp b/src/catch2/internal/catch_exception_translator_registry.hpp index 2aafa684..c0180e9e 100644 --- a/src/catch2/internal/catch_exception_translator_registry.hpp +++ b/src/catch2/internal/catch_exception_translator_registry.hpp @@ -8,23 +8,22 @@ #ifndef CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED #define CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED -#include #include -#include #include namespace Catch { + class IExceptionTranslator; + + class ExceptionTranslatorRegistry { + struct ExceptionTranslatorRegistryImpl; + Detail::unique_ptr m_impl; - class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry { public: - ~ExceptionTranslatorRegistry() override; + ExceptionTranslatorRegistry(); + ~ExceptionTranslatorRegistry(); void registerTranslator( Detail::unique_ptr&& translator ); - std::string translateActiveException() const override; - std::string tryTranslators() const; - - private: - ExceptionTranslators m_translators; + std::string translateActiveException() const; }; } diff --git a/src/catch2/meson.build b/src/catch2/meson.build index 879c6244..123c6717 100644 --- a/src/catch2/meson.build +++ b/src/catch2/meson.build @@ -259,6 +259,7 @@ internal_sources = files( 'catch_timer.cpp', 'catch_tostring.cpp', 'catch_totals.cpp', + 'catch_translate_exception.cpp', 'catch_version.cpp', )