catch_registry_hub.cpp
1 2 // Copyright Catch2 Authors 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE.txt or copy at 5 // https://www.boost.org/LICENSE_1_0.txt) 6 7 // SPDX-License-Identifier: BSL-1.0 8 #include <catch2/interfaces/catch_interfaces_registry_hub.hpp> 9 10 #include <catch2/internal/catch_context.hpp> 11 #include <catch2/internal/catch_enforce.hpp> 12 #include <catch2/internal/catch_test_case_registry_impl.hpp> 13 #include <catch2/internal/catch_reporter_registry.hpp> 14 #include <catch2/internal/catch_exception_translator_registry.hpp> 15 #include <catch2/internal/catch_tag_alias_registry.hpp> 16 #include <catch2/internal/catch_startup_exception_registry.hpp> 17 #include <catch2/internal/catch_singletons.hpp> 18 #include <catch2/internal/catch_enum_values_registry.hpp> 19 #include <catch2/catch_test_case_info.hpp> 20 #include <catch2/internal/catch_noncopyable.hpp> 21 #include <catch2/interfaces/catch_interfaces_reporter_factory.hpp> 22 #include <catch2/internal/catch_move_and_forward.hpp> 23 #include <catch2/internal/catch_reporter_registry.hpp> 24 25 #include <exception> 26 27 namespace Catch { 28 29 namespace { 30 31 class RegistryHub : public IRegistryHub, 32 public IMutableRegistryHub, 33 private Detail::NonCopyable { 34 35 public: // IRegistryHub 36 RegistryHub() = default; 37 ReporterRegistry const& getReporterRegistry() const override { 38 return m_reporterRegistry; 39 } 40 ITestCaseRegistry const& getTestCaseRegistry() const override { 41 return m_testCaseRegistry; 42 } 43 IExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const override { 44 return m_exceptionTranslatorRegistry; 45 } 46 ITagAliasRegistry const& getTagAliasRegistry() const override { 47 return m_tagAliasRegistry; 48 } 49 StartupExceptionRegistry const& getStartupExceptionRegistry() const override { 50 return m_exceptionRegistry; 51 } 52 53 public: // IMutableRegistryHub 54 void registerReporter( std::string const& name, IReporterFactoryPtr factory ) override { 55 m_reporterRegistry.registerReporter( name, CATCH_MOVE(factory) ); 56 } 57 void registerListener( Detail::unique_ptr<EventListenerFactory> factory ) override { 58 m_reporterRegistry.registerListener( CATCH_MOVE(factory) ); 59 } 60 void registerTest( Detail::unique_ptr<TestCaseInfo>&& testInfo, Detail::unique_ptr<ITestInvoker>&& invoker ) override { 61 m_testCaseRegistry.registerTest( CATCH_MOVE(testInfo), CATCH_MOVE(invoker) ); 62 } 63 void registerTranslator( Detail::unique_ptr<IExceptionTranslator>&& translator ) override { 64 m_exceptionTranslatorRegistry.registerTranslator( CATCH_MOVE(translator) ); 65 } 66 void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) override { 67 m_tagAliasRegistry.add( alias, tag, lineInfo ); 68 } 69 void registerStartupException() noexcept override { 70 #if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) 71 m_exceptionRegistry.add(std::current_exception()); 72 #else 73 CATCH_INTERNAL_ERROR("Attempted to register active exception under CATCH_CONFIG_DISABLE_EXCEPTIONS!"); 74 #endif 75 } 76 IMutableEnumValuesRegistry& getMutableEnumValuesRegistry() override { 77 return m_enumValuesRegistry; 78 } 79 80 private: 81 TestRegistry m_testCaseRegistry; 82 ReporterRegistry m_reporterRegistry; 83 ExceptionTranslatorRegistry m_exceptionTranslatorRegistry; 84 TagAliasRegistry m_tagAliasRegistry; 85 StartupExceptionRegistry m_exceptionRegistry; 86 Detail::EnumValuesRegistry m_enumValuesRegistry; 87 }; 88 } 89 90 using RegistryHubSingleton = Singleton<RegistryHub, IRegistryHub, IMutableRegistryHub>; 91 92 IRegistryHub const& getRegistryHub() { 93 return RegistryHubSingleton::get(); 94 } 95 IMutableRegistryHub& getMutableRegistryHub() { 96 return RegistryHubSingleton::getMutable(); 97 } 98 void cleanUp() { 99 cleanupSingletons(); 100 cleanUpContext(); 101 } 102 std::string translateActiveException() { 103 return getRegistryHub().getExceptionTranslatorRegistry().translateActiveException(); 104 } 105 106 107 } // end namespace Catch