From 31b291ba26db9403a9d9a3b593d0dc581e65ea74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Tue, 14 Mar 2023 16:28:30 +0100 Subject: [PATCH] Unify IMutableEnumRegistry and EnumValuesRegistry This time there was no need for compilation firewall, so we keep everything inlined. --- src/CMakeLists.txt | 1 - src/catch2/catch_registry_hub.cpp | 4 +- src/catch2/catch_tostring.hpp | 2 +- .../interfaces/catch_interfaces_all.hpp | 1 - .../catch_interfaces_enum_values_registry.hpp | 47 ----------------- .../catch_interfaces_registry_hub.hpp | 4 +- .../internal/catch_enum_values_registry.cpp | 52 +++++++++++-------- .../internal/catch_enum_values_registry.hpp | 45 ++++++++++++---- src/catch2/meson.build | 1 - 9 files changed, 70 insertions(+), 87 deletions(-) delete mode 100644 src/catch2/interfaces/catch_interfaces_enum_values_registry.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 719413d2..5a9c105f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -210,7 +210,6 @@ set(INTERFACE_HEADERS ${SOURCES_DIR}/interfaces/catch_interfaces_all.hpp ${SOURCES_DIR}/interfaces/catch_interfaces_capture.hpp ${SOURCES_DIR}/interfaces/catch_interfaces_config.hpp - ${SOURCES_DIR}/interfaces/catch_interfaces_enum_values_registry.hpp ${SOURCES_DIR}/interfaces/catch_interfaces_exception.hpp ${SOURCES_DIR}/interfaces/catch_interfaces_generatortracker.hpp ${SOURCES_DIR}/interfaces/catch_interfaces_registry_hub.hpp diff --git a/src/catch2/catch_registry_hub.cpp b/src/catch2/catch_registry_hub.cpp index 0e43db92..17390bc0 100644 --- a/src/catch2/catch_registry_hub.cpp +++ b/src/catch2/catch_registry_hub.cpp @@ -71,7 +71,7 @@ namespace Catch { CATCH_INTERNAL_ERROR("Attempted to register active exception under CATCH_CONFIG_DISABLE_EXCEPTIONS!"); #endif } - IMutableEnumValuesRegistry& getMutableEnumValuesRegistry() override { + EnumValuesRegistry& getMutableEnumValuesRegistry() override { return m_enumValuesRegistry; } @@ -81,7 +81,7 @@ namespace Catch { ExceptionTranslatorRegistry m_exceptionTranslatorRegistry; TagAliasRegistry m_tagAliasRegistry; StartupExceptionRegistry m_exceptionRegistry; - Detail::EnumValuesRegistry m_enumValuesRegistry; + EnumValuesRegistry m_enumValuesRegistry; }; } diff --git a/src/catch2/catch_tostring.hpp b/src/catch2/catch_tostring.hpp index 904caa7e..c95be44c 100644 --- a/src/catch2/catch_tostring.hpp +++ b/src/catch2/catch_tostring.hpp @@ -16,9 +16,9 @@ #include #include +#include #include #include -#include #ifdef CATCH_CONFIG_CPP17_STRING_VIEW #include diff --git a/src/catch2/interfaces/catch_interfaces_all.hpp b/src/catch2/interfaces/catch_interfaces_all.hpp index 7a0d0c29..df9e53e6 100644 --- a/src/catch2/interfaces/catch_interfaces_all.hpp +++ b/src/catch2/interfaces/catch_interfaces_all.hpp @@ -24,7 +24,6 @@ #include #include -#include #include #include #include diff --git a/src/catch2/interfaces/catch_interfaces_enum_values_registry.hpp b/src/catch2/interfaces/catch_interfaces_enum_values_registry.hpp deleted file mode 100644 index 38b052d9..00000000 --- a/src/catch2/interfaces/catch_interfaces_enum_values_registry.hpp +++ /dev/null @@ -1,47 +0,0 @@ - -// 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 -#ifndef CATCH_INTERFACES_ENUM_VALUES_REGISTRY_HPP_INCLUDED -#define CATCH_INTERFACES_ENUM_VALUES_REGISTRY_HPP_INCLUDED - -#include - -#include - -namespace Catch { - - namespace Detail { - struct EnumInfo { - StringRef m_name; - std::vector> m_values; - - ~EnumInfo(); - - StringRef lookup( int value ) const; - }; - } // namespace Detail - - class IMutableEnumValuesRegistry { - public: - virtual ~IMutableEnumValuesRegistry(); // = default; - - virtual Detail::EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::vector const& values ) = 0; - - template - Detail::EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::initializer_list values ) { - static_assert(sizeof(int) >= sizeof(E), "Cannot serialize enum to int"); - std::vector intValues; - intValues.reserve( values.size() ); - for( auto enumValue : values ) - intValues.push_back( static_cast( enumValue ) ); - return registerEnum( enumName, allEnums, intValues ); - } - }; - -} // Catch - -#endif // CATCH_INTERFACES_ENUM_VALUES_REGISTRY_HPP_INCLUDED diff --git a/src/catch2/interfaces/catch_interfaces_registry_hub.hpp b/src/catch2/interfaces/catch_interfaces_registry_hub.hpp index 113f223e..ebd5192d 100644 --- a/src/catch2/interfaces/catch_interfaces_registry_hub.hpp +++ b/src/catch2/interfaces/catch_interfaces_registry_hub.hpp @@ -23,7 +23,7 @@ namespace Catch { class IReporterFactory; class ITagAliasRegistry; class ITestInvoker; - class IMutableEnumValuesRegistry; + class EnumValuesRegistry; struct SourceLineInfo; class StartupExceptionRegistry; @@ -53,7 +53,7 @@ namespace Catch { virtual void registerTranslator( Detail::unique_ptr&& translator ) = 0; virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) = 0; virtual void registerStartupException() noexcept = 0; - virtual IMutableEnumValuesRegistry& getMutableEnumValuesRegistry() = 0; + virtual EnumValuesRegistry& getMutableEnumValuesRegistry() = 0; }; IRegistryHub const& getRegistryHub(); diff --git a/src/catch2/internal/catch_enum_values_registry.cpp b/src/catch2/internal/catch_enum_values_registry.cpp index 7e8bf5e5..5a9e7c53 100644 --- a/src/catch2/internal/catch_enum_values_registry.cpp +++ b/src/catch2/internal/catch_enum_values_registry.cpp @@ -12,44 +12,47 @@ namespace Catch { - IMutableEnumValuesRegistry::~IMutableEnumValuesRegistry() = default; - namespace Detail { namespace { // Extracts the actual name part of an enum instance - // In other words, it returns the Blue part of Bikeshed::Colour::Blue - StringRef extractInstanceName(StringRef enumInstance) { + // In other words, it returns the Blue part of + // Bikeshed::Colour::Blue + static StringRef extractInstanceName( StringRef enumInstance ) { // Find last occurrence of ":" size_t name_start = enumInstance.size(); - while (name_start > 0 && enumInstance[name_start - 1] != ':') { + while ( name_start > 0 && + enumInstance[name_start - 1] != ':' ) { --name_start; } - return enumInstance.substr(name_start, enumInstance.size() - name_start); + return enumInstance.substr( name_start, + enumInstance.size() - name_start ); } - } + } // end unnamed namespace std::vector parseEnums( StringRef enums ) { auto enumValues = splitStringRef( enums, ',' ); std::vector parsed; parsed.reserve( enumValues.size() ); - for( auto const& enumValue : enumValues ) { - parsed.push_back(trim(extractInstanceName(enumValue))); + for ( auto const& enumValue : enumValues ) { + parsed.push_back( trim( extractInstanceName( enumValue ) ) ); } return parsed; } - EnumInfo::~EnumInfo() {} + EnumInfo::~EnumInfo() = default; StringRef EnumInfo::lookup( int value ) const { - for( auto const& valueToName : m_values ) { - if( valueToName.first == value ) - return valueToName.second; + for ( auto const& valueToName : m_values ) { + if ( valueToName.first == value ) { return valueToName.second; } } return "{** unexpected enum value **}"_sr; } - Catch::Detail::unique_ptr makeEnumInfo( StringRef enumName, StringRef allValueNames, std::vector const& values ) { + Catch::Detail::unique_ptr + makeEnumInfo( StringRef enumName, + StringRef allValueNames, + std::vector const& values ) { auto enumInfo = Catch::Detail::make_unique(); enumInfo->m_name = enumName; enumInfo->m_values.reserve( values.size() ); @@ -57,17 +60,22 @@ namespace Catch { const auto valueNames = Catch::Detail::parseEnums( allValueNames ); assert( valueNames.size() == values.size() ); std::size_t i = 0; - for( auto value : values ) - enumInfo->m_values.emplace_back(value, valueNames[i++]); + for ( auto value : values ) { + enumInfo->m_values.emplace_back( value, valueNames[i++] ); + } return enumInfo; } - EnumInfo const& EnumValuesRegistry::registerEnum( StringRef enumName, StringRef allValueNames, std::vector const& values ) { - m_enumInfos.push_back(makeEnumInfo(enumName, allValueNames, values)); - return *m_enumInfos.back(); - } + } // namespace Detail - } // Detail -} // Catch + Detail::EnumInfo const& + EnumValuesRegistry::registerEnum( StringRef enumName, + StringRef allValueNames, + std::vector const& values ) { + m_enumInfos.push_back( + Detail::makeEnumInfo( enumName, allValueNames, values ) ); + return *m_enumInfos.back(); + } +} // namespace Catch diff --git a/src/catch2/internal/catch_enum_values_registry.hpp b/src/catch2/internal/catch_enum_values_registry.hpp index 999059ae..64776355 100644 --- a/src/catch2/internal/catch_enum_values_registry.hpp +++ b/src/catch2/internal/catch_enum_values_registry.hpp @@ -8,29 +8,54 @@ #ifndef CATCH_ENUM_VALUES_REGISTRY_HPP_INCLUDED #define CATCH_ENUM_VALUES_REGISTRY_HPP_INCLUDED -#include -#include #include +#include #include namespace Catch { namespace Detail { + struct EnumInfo { + StringRef m_name; + std::vector> m_values; - Catch::Detail::unique_ptr makeEnumInfo( StringRef enumName, StringRef allValueNames, std::vector const& values ); + ~EnumInfo(); - class EnumValuesRegistry : public IMutableEnumValuesRegistry { - - std::vector> m_enumInfos; - - EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::vector const& values) override; + StringRef lookup( int value ) const; }; + Detail::unique_ptr + makeEnumInfo( StringRef enumName, + StringRef allValueNames, + std::vector const& values ); std::vector parseEnums( StringRef enums ); - } // Detail + } // namespace Detail -} // Catch + class EnumValuesRegistry { + std::vector> m_enumInfos; + + public: + Detail::EnumInfo const& registerEnum( StringRef enumName, + StringRef allEnums, + std::vector const& values ); + template + Detail::EnumInfo const& + registerEnum( StringRef enumName, + StringRef allEnums, + std::initializer_list values ) { + static_assert( sizeof( int ) >= sizeof( E ), + "Cannot serialize enum to int" ); + std::vector intValues; + intValues.reserve( values.size() ); + for ( auto enumValue : values ) { + intValues.push_back( static_cast( enumValue ) ); + } + return registerEnum( enumName, allEnums, intValues ); + } + }; + +} // namespace Catch #endif // CATCH_ENUM_VALUES_REGISTRY_HPP_INCLUDED diff --git a/src/catch2/meson.build b/src/catch2/meson.build index fee3b4ed..879c6244 100644 --- a/src/catch2/meson.build +++ b/src/catch2/meson.build @@ -58,7 +58,6 @@ internal_headers = [ 'interfaces/catch_interfaces_all.hpp', 'interfaces/catch_interfaces_capture.hpp', 'interfaces/catch_interfaces_config.hpp', - 'interfaces/catch_interfaces_enum_values_registry.hpp', 'interfaces/catch_interfaces_exception.hpp', 'interfaces/catch_interfaces_generatortracker.hpp', 'interfaces/catch_interfaces_registry_hub.hpp',