From 7134ad9913a1bc206726b9f1c057e1e625a08c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sat, 25 Jan 2020 14:07:29 +0100 Subject: [PATCH] Replace shared_ptr with unique_ptr for reporters --- src/catch2/catch_interfaces_registry_hub.h | 6 ++-- src/catch2/catch_interfaces_reporter.h | 2 +- src/catch2/catch_registry_hub.cpp | 8 ++--- src/catch2/catch_reporter_registrars.hpp | 4 +-- src/catch2/catch_reporter_registry.cpp | 34 ++++++++++++---------- src/catch2/catch_reporter_registry.h | 6 ++-- 6 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/catch2/catch_interfaces_registry_hub.h b/src/catch2/catch_interfaces_registry_hub.h index 91069cfd..fc63a0d0 100644 --- a/src/catch2/catch_interfaces_registry_hub.h +++ b/src/catch2/catch_interfaces_registry_hub.h @@ -28,7 +28,7 @@ namespace Catch { class StartupExceptionRegistry; - using IReporterFactoryPtr = std::shared_ptr; + using IReporterFactoryPtr = std::unique_ptr; struct IRegistryHub { virtual ~IRegistryHub(); @@ -44,8 +44,8 @@ namespace Catch { struct IMutableRegistryHub { virtual ~IMutableRegistryHub(); - virtual void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) = 0; - virtual void registerListener( IReporterFactoryPtr const& factory ) = 0; + virtual void registerReporter( std::string const& name, IReporterFactoryPtr factory ) = 0; + virtual void registerListener( IReporterFactoryPtr factory ) = 0; virtual void registerTest(std::unique_ptr&& testInfo, std::unique_ptr&& invoker) = 0; virtual void registerTranslator( const IExceptionTranslator* translator ) = 0; virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) = 0; diff --git a/src/catch2/catch_interfaces_reporter.h b/src/catch2/catch_interfaces_reporter.h index 36129741..97ce8ea0 100644 --- a/src/catch2/catch_interfaces_reporter.h +++ b/src/catch2/catch_interfaces_reporter.h @@ -261,7 +261,7 @@ namespace Catch { virtual IStreamingReporterPtr create( ReporterConfig const& config ) const = 0; virtual std::string getDescription() const = 0; }; - using IReporterFactoryPtr = std::shared_ptr; + using IReporterFactoryPtr = std::unique_ptr; struct IReporterRegistry { using FactoryMap = std::map; diff --git a/src/catch2/catch_registry_hub.cpp b/src/catch2/catch_registry_hub.cpp index f49506a2..8194c417 100644 --- a/src/catch2/catch_registry_hub.cpp +++ b/src/catch2/catch_registry_hub.cpp @@ -43,11 +43,11 @@ namespace Catch { } public: // IMutableRegistryHub - void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) override { - m_reporterRegistry.registerReporter( name, factory ); + void registerReporter( std::string const& name, IReporterFactoryPtr factory ) override { + m_reporterRegistry.registerReporter( name, std::move(factory) ); } - void registerListener( IReporterFactoryPtr const& factory ) override { - m_reporterRegistry.registerListener( factory ); + void registerListener( IReporterFactoryPtr factory ) override { + m_reporterRegistry.registerListener( std::move(factory) ); } void registerTest( std::unique_ptr&& testInfo, std::unique_ptr&& invoker ) override { m_testCaseRegistry.registerTest( std::move(testInfo), std::move(invoker) ); diff --git a/src/catch2/catch_reporter_registrars.hpp b/src/catch2/catch_reporter_registrars.hpp index 99be5577..cbc4fb73 100644 --- a/src/catch2/catch_reporter_registrars.hpp +++ b/src/catch2/catch_reporter_registrars.hpp @@ -17,7 +17,7 @@ namespace Catch { class ReporterFactory : public IReporterFactory { IStreamingReporterPtr create( ReporterConfig const& config ) const override { - return std::unique_ptr( new T( config ) ); + return std::make_unique( config ); } std::string getDescription() const override { @@ -50,7 +50,7 @@ namespace Catch { public: ListenerRegistrar() { - getMutableRegistryHub().registerListener( std::make_shared() ); + getMutableRegistryHub().registerListener( std::make_unique() ); } }; } diff --git a/src/catch2/catch_reporter_registry.cpp b/src/catch2/catch_reporter_registry.cpp index 92f0be89..beb8411e 100644 --- a/src/catch2/catch_reporter_registry.cpp +++ b/src/catch2/catch_reporter_registry.cpp @@ -18,17 +18,21 @@ namespace Catch { - ReporterRegistry::ReporterRegistry(): - m_factories({ - {"automake", std::make_shared>() }, - {"compact", std::make_shared>() }, - {"console", std::make_shared>() }, - {"junit", std::make_shared>() }, - {"sonarqube", std::make_shared>() }, - {"tap", std::make_shared>() }, - {"teamcity", std::make_shared>() }, - {"xml", std::make_shared>() }, - }) {} + ReporterRegistry::ReporterRegistry() { + // Because it is impossible to move out of initializer list, + // we have to add the elements manually + m_factories["automake"] = std::make_unique>(); + m_factories["compact"] = std::make_unique>(); + m_factories["console"] = std::make_unique>(); + m_factories["junit"] = std::make_unique>(); + m_factories["sonarqube"] = std::make_unique>(); + m_factories["tap"] = std::make_unique>(); + m_factories["teamcity"] = std::make_unique>(); + m_factories["xml"] = std::make_unique>(); + } + + ReporterRegistry::~ReporterRegistry() = default; + IStreamingReporterPtr ReporterRegistry::create( std::string const& name, IConfigPtr const& config ) const { auto it = m_factories.find( name ); @@ -37,11 +41,11 @@ namespace Catch { return it->second->create( ReporterConfig( config ) ); } - void ReporterRegistry::registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) { - m_factories.emplace(name, factory); + void ReporterRegistry::registerReporter( std::string const& name, IReporterFactoryPtr factory ) { + m_factories.emplace(name, std::move(factory)); } - void ReporterRegistry::registerListener( IReporterFactoryPtr const& factory ) { - m_listeners.push_back( factory ); + void ReporterRegistry::registerListener( IReporterFactoryPtr factory ) { + m_listeners.push_back( std::move(factory) ); } IReporterRegistry::FactoryMap const& ReporterRegistry::getFactories() const { diff --git a/src/catch2/catch_reporter_registry.h b/src/catch2/catch_reporter_registry.h index e6aa86f4..456b76f8 100644 --- a/src/catch2/catch_reporter_registry.h +++ b/src/catch2/catch_reporter_registry.h @@ -18,12 +18,12 @@ namespace Catch { public: ReporterRegistry(); - ~ReporterRegistry() override = default; + ~ReporterRegistry() override; // = default, out of line to allow fwd decl IStreamingReporterPtr create( std::string const& name, IConfigPtr const& config ) const override; - void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ); - void registerListener( IReporterFactoryPtr const& factory ); + void registerReporter( std::string const& name, IReporterFactoryPtr factory ); + void registerListener( IReporterFactoryPtr factory ); FactoryMap const& getFactories() const override; Listeners const& getListeners() const override;