Replace shared_ptr with unique_ptr for reporters

This commit is contained in:
Martin Hořeňovský 2020-01-25 14:07:29 +01:00
parent 827733fe81
commit 7134ad9913
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
6 changed files with 32 additions and 28 deletions

View File

@ -28,7 +28,7 @@ namespace Catch {
class StartupExceptionRegistry; class StartupExceptionRegistry;
using IReporterFactoryPtr = std::shared_ptr<IReporterFactory>; using IReporterFactoryPtr = std::unique_ptr<IReporterFactory>;
struct IRegistryHub { struct IRegistryHub {
virtual ~IRegistryHub(); virtual ~IRegistryHub();
@ -44,8 +44,8 @@ namespace Catch {
struct IMutableRegistryHub { struct IMutableRegistryHub {
virtual ~IMutableRegistryHub(); virtual ~IMutableRegistryHub();
virtual void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) = 0; virtual void registerReporter( std::string const& name, IReporterFactoryPtr factory ) = 0;
virtual void registerListener( IReporterFactoryPtr const& factory ) = 0; virtual void registerListener( IReporterFactoryPtr factory ) = 0;
virtual void registerTest(std::unique_ptr<TestCaseInfo>&& testInfo, std::unique_ptr<ITestInvoker>&& invoker) = 0; virtual void registerTest(std::unique_ptr<TestCaseInfo>&& testInfo, std::unique_ptr<ITestInvoker>&& invoker) = 0;
virtual void registerTranslator( const IExceptionTranslator* translator ) = 0; virtual void registerTranslator( const IExceptionTranslator* translator ) = 0;
virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) = 0; virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) = 0;

View File

@ -261,7 +261,7 @@ namespace Catch {
virtual IStreamingReporterPtr create( ReporterConfig const& config ) const = 0; virtual IStreamingReporterPtr create( ReporterConfig const& config ) const = 0;
virtual std::string getDescription() const = 0; virtual std::string getDescription() const = 0;
}; };
using IReporterFactoryPtr = std::shared_ptr<IReporterFactory>; using IReporterFactoryPtr = std::unique_ptr<IReporterFactory>;
struct IReporterRegistry { struct IReporterRegistry {
using FactoryMap = std::map<std::string, IReporterFactoryPtr>; using FactoryMap = std::map<std::string, IReporterFactoryPtr>;

View File

@ -43,11 +43,11 @@ namespace Catch {
} }
public: // IMutableRegistryHub public: // IMutableRegistryHub
void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) override { void registerReporter( std::string const& name, IReporterFactoryPtr factory ) override {
m_reporterRegistry.registerReporter( name, factory ); m_reporterRegistry.registerReporter( name, std::move(factory) );
} }
void registerListener( IReporterFactoryPtr const& factory ) override { void registerListener( IReporterFactoryPtr factory ) override {
m_reporterRegistry.registerListener( factory ); m_reporterRegistry.registerListener( std::move(factory) );
} }
void registerTest( std::unique_ptr<TestCaseInfo>&& testInfo, std::unique_ptr<ITestInvoker>&& invoker ) override { void registerTest( std::unique_ptr<TestCaseInfo>&& testInfo, std::unique_ptr<ITestInvoker>&& invoker ) override {
m_testCaseRegistry.registerTest( std::move(testInfo), std::move(invoker) ); m_testCaseRegistry.registerTest( std::move(testInfo), std::move(invoker) );

View File

@ -17,7 +17,7 @@ namespace Catch {
class ReporterFactory : public IReporterFactory { class ReporterFactory : public IReporterFactory {
IStreamingReporterPtr create( ReporterConfig const& config ) const override { IStreamingReporterPtr create( ReporterConfig const& config ) const override {
return std::unique_ptr<T>( new T( config ) ); return std::make_unique<T>( config );
} }
std::string getDescription() const override { std::string getDescription() const override {
@ -50,7 +50,7 @@ namespace Catch {
public: public:
ListenerRegistrar() { ListenerRegistrar() {
getMutableRegistryHub().registerListener( std::make_shared<ListenerFactory>() ); getMutableRegistryHub().registerListener( std::make_unique<ListenerFactory>() );
} }
}; };
} }

View File

@ -18,17 +18,21 @@
namespace Catch { namespace Catch {
ReporterRegistry::ReporterRegistry(): ReporterRegistry::ReporterRegistry() {
m_factories({ // Because it is impossible to move out of initializer list,
{"automake", std::make_shared<ReporterFactory<AutomakeReporter>>() }, // we have to add the elements manually
{"compact", std::make_shared<ReporterFactory<CompactReporter>>() }, m_factories["automake"] = std::make_unique<ReporterFactory<AutomakeReporter>>();
{"console", std::make_shared<ReporterFactory<ConsoleReporter>>() }, m_factories["compact"] = std::make_unique<ReporterFactory<CompactReporter>>();
{"junit", std::make_shared<ReporterFactory<JunitReporter>>() }, m_factories["console"] = std::make_unique<ReporterFactory<ConsoleReporter>>();
{"sonarqube", std::make_shared<ReporterFactory<SonarQubeReporter>>() }, m_factories["junit"] = std::make_unique<ReporterFactory<JunitReporter>>();
{"tap", std::make_shared<ReporterFactory<TAPReporter>>() }, m_factories["sonarqube"] = std::make_unique<ReporterFactory<SonarQubeReporter>>();
{"teamcity", std::make_shared<ReporterFactory<TeamCityReporter>>() }, m_factories["tap"] = std::make_unique<ReporterFactory<TAPReporter>>();
{"xml", std::make_shared<ReporterFactory<XmlReporter>>() }, m_factories["teamcity"] = std::make_unique<ReporterFactory<TeamCityReporter>>();
}) {} m_factories["xml"] = std::make_unique<ReporterFactory<XmlReporter>>();
}
ReporterRegistry::~ReporterRegistry() = default;
IStreamingReporterPtr ReporterRegistry::create( std::string const& name, IConfigPtr const& config ) const { IStreamingReporterPtr ReporterRegistry::create( std::string const& name, IConfigPtr const& config ) const {
auto it = m_factories.find( name ); auto it = m_factories.find( name );
@ -37,11 +41,11 @@ namespace Catch {
return it->second->create( ReporterConfig( config ) ); return it->second->create( ReporterConfig( config ) );
} }
void ReporterRegistry::registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) { void ReporterRegistry::registerReporter( std::string const& name, IReporterFactoryPtr factory ) {
m_factories.emplace(name, factory); m_factories.emplace(name, std::move(factory));
} }
void ReporterRegistry::registerListener( IReporterFactoryPtr const& factory ) { void ReporterRegistry::registerListener( IReporterFactoryPtr factory ) {
m_listeners.push_back( factory ); m_listeners.push_back( std::move(factory) );
} }
IReporterRegistry::FactoryMap const& ReporterRegistry::getFactories() const { IReporterRegistry::FactoryMap const& ReporterRegistry::getFactories() const {

View File

@ -18,12 +18,12 @@ namespace Catch {
public: public:
ReporterRegistry(); 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; IStreamingReporterPtr create( std::string const& name, IConfigPtr const& config ) const override;
void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ); void registerReporter( std::string const& name, IReporterFactoryPtr factory );
void registerListener( IReporterFactoryPtr const& factory ); void registerListener( IReporterFactoryPtr factory );
FactoryMap const& getFactories() const override; FactoryMap const& getFactories() const override;
Listeners const& getListeners() const override; Listeners const& getListeners() const override;