converted IStreamingReporter to unique_ptr

This commit is contained in:
Phil Nash 2017-04-29 19:38:34 +01:00
parent ef8b72c949
commit 687437fcd1
5 changed files with 46 additions and 46 deletions

View File

@ -23,30 +23,28 @@
namespace Catch { namespace Catch {
IStreamingReporterPtr createReporter( std::string const& reporterName, IConfigPtr const& config ) { IStreamingReporterPtr createReporter( std::string const& reporterName, IConfigPtr const& config ) {
IStreamingReporterPtr reporter = getRegistryHub().getReporterRegistry().create( reporterName, config ); if( auto reporter = getRegistryHub().getReporterRegistry().create( reporterName, config ) )
if( !reporter ) { return reporter;
std::ostringstream oss;
oss << "No reporter registered with name: '" << reporterName << "'"; std::ostringstream oss;
throw std::domain_error( oss.str() ); oss << "No reporter registered with name: '" << reporterName << "'";
} throw std::domain_error( oss.str() );
return reporter;
} }
IStreamingReporterPtr makeReporter( std::shared_ptr<Config> const& config ) { IStreamingReporterPtr makeReporter( std::shared_ptr<Config> const& config ) {
std::vector<std::string> reporters = config->getReporterNames(); auto const& reporterNames = config->getReporterNames();
if( reporters.empty() ) if( reporterNames.empty() )
reporters.push_back( "console" ); return createReporter( "console", config );
IStreamingReporterPtr reporter; IStreamingReporterPtr reporter;
for( auto const& name : reporters ) for( auto const& name : reporterNames )
reporter = addReporter( reporter, createReporter( name, config ) ); addReporter( reporter, createReporter( name, config ) );
return reporter; return reporter;
} }
IStreamingReporterPtr addListeners( IConfigPtr const& config, IStreamingReporterPtr reporters ) { void addListeners( IStreamingReporterPtr& reporters, IConfigPtr const& config ) {
auto const& listeners = getRegistryHub().getReporterRegistry().getListeners(); auto const& listeners = getRegistryHub().getReporterRegistry().getListeners();
for( auto const& listener : listeners ) for( auto const& listener : listeners )
reporters = addReporter(reporters, listener->create( ReporterConfig( config ) ) ); addReporter(reporters, listener->create( ReporterConfig( config ) ) );
return reporters;
} }
@ -55,9 +53,9 @@ namespace Catch {
IConfigPtr iconfig = config; IConfigPtr iconfig = config;
IStreamingReporterPtr reporter = makeReporter( config ); IStreamingReporterPtr reporter = makeReporter( config );
reporter = addListeners( iconfig, reporter ); addListeners( reporter, iconfig );
RunContext context( iconfig, reporter ); RunContext context( iconfig, std::move( reporter ) );
Totals totals; Totals totals;
@ -72,7 +70,7 @@ namespace Catch {
if( !context.aborting() && matchTest( testCase, testSpec, *iconfig ) ) if( !context.aborting() && matchTest( testCase, testSpec, *iconfig ) )
totals += context.runTest( testCase ); totals += context.runTest( testCase );
else else
reporter->skipTest( testCase ); context.reporter().skipTest( testCase );
} }
context.testGroupEnded( iconfig->name(), totals, 1, 1 ); context.testGroupEnded( iconfig->name(), totals, 1, 1 );

View File

@ -232,9 +232,9 @@ namespace Catch
virtual void skipTest( TestCaseInfo const& testInfo ) = 0; virtual void skipTest( TestCaseInfo const& testInfo ) = 0;
virtual MultipleReporters* tryAsMulti() { return nullptr; } virtual bool isMulti() const { return false; }
}; };
using IStreamingReporterPtr = std::shared_ptr<IStreamingReporter>; using IStreamingReporterPtr = std::unique_ptr<IStreamingReporter>;
struct IReporterFactory { struct IReporterFactory {
virtual ~IReporterFactory(); virtual ~IReporterFactory();
@ -253,8 +253,7 @@ namespace Catch
virtual Listeners const& getListeners() const = 0; virtual Listeners const& getListeners() const = 0;
}; };
IStreamingReporterPtr addReporter( IStreamingReporterPtr const& existingReporter, IStreamingReporterPtr const& additionalReporter ); void addReporter( IStreamingReporterPtr& existingReporter, IStreamingReporterPtr&& additionalReporter );
} }
#endif // TWOBLUECUBES_CATCH_INTERFACES_REPORTER_H_INCLUDED #endif // TWOBLUECUBES_CATCH_INTERFACES_REPORTER_H_INCLUDED

View File

@ -18,7 +18,7 @@ namespace Catch {
class ReporterFactory : public IReporterFactory { class ReporterFactory : public IReporterFactory {
virtual IStreamingReporterPtr create( ReporterConfig const& config ) const { virtual IStreamingReporterPtr create( ReporterConfig const& config ) const {
return std::make_shared<T>( config ); return std::unique_ptr<T>( new T( config ) );
} }
virtual std::string getDescription() const { virtual std::string getDescription() const {

View File

@ -59,11 +59,11 @@ namespace Catch {
public: public:
explicit RunContext( IConfigPtr const& _config, IStreamingReporterPtr const& reporter ) explicit RunContext( IConfigPtr const& _config, IStreamingReporterPtr&& reporter )
: m_runInfo( _config->name() ), : m_runInfo( _config->name() ),
m_context( getCurrentMutableContext() ), m_context( getCurrentMutableContext() ),
m_config( _config ), m_config( _config ),
m_reporter( reporter ) m_reporter( std::move( reporter ) )
{ {
m_context.setRunner( this ); m_context.setRunner( this );
m_context.setConfig( m_config ); m_context.setConfig( m_config );
@ -131,6 +131,9 @@ namespace Catch {
IConfigPtr config() const { IConfigPtr config() const {
return m_config; return m_config;
} }
IStreamingReporter& reporter() const {
return *m_reporter;
}
private: // IResultCapture private: // IResultCapture

View File

@ -13,12 +13,12 @@
namespace Catch { namespace Catch {
class MultipleReporters : public IStreamingReporter { class MultipleReporters : public IStreamingReporter {
typedef std::vector<IStreamingReporterPtr > Reporters; typedef std::vector<IStreamingReporterPtr> Reporters;
Reporters m_reporters; Reporters m_reporters;
public: public:
void add( IStreamingReporterPtr const& reporter ) { void add( IStreamingReporterPtr&& reporter ) {
m_reporters.push_back( reporter ); m_reporters.push_back( std::move( reporter ) );
} }
public: // IStreamingReporter public: // IStreamingReporter
@ -95,31 +95,31 @@ public: // IStreamingReporter
reporter->skipTest( testInfo ); reporter->skipTest( testInfo );
} }
virtual MultipleReporters* tryAsMulti() override { virtual bool isMulti() const override {
return this; return true;
} }
}; };
IStreamingReporterPtr addReporter( IStreamingReporterPtr const& existingReporter, IStreamingReporterPtr const& additionalReporter ) { void addReporter( IStreamingReporterPtr& existingReporter, IStreamingReporterPtr&& additionalReporter ) {
IStreamingReporterPtr resultingReporter;
if( existingReporter ) { if( !existingReporter ) {
MultipleReporters* multi = existingReporter->tryAsMulti(); existingReporter = std::move( additionalReporter );
if( !multi ) { return;
multi = new MultipleReporters;
resultingReporter = IStreamingReporterPtr( multi );
if( existingReporter )
multi->add( existingReporter );
}
else
resultingReporter = existingReporter;
multi->add( additionalReporter );
} }
else
resultingReporter = additionalReporter;
return resultingReporter; MultipleReporters* multi = nullptr;
if( existingReporter->isMulti() ) {
multi = static_cast<MultipleReporters*>( existingReporter.get() );
}
else {
auto newMulti = std::unique_ptr<MultipleReporters>( new MultipleReporters );
newMulti->add( std::move( existingReporter ) );
multi = newMulti.get();
existingReporter = std::move( newMulti );
}
multi->add( std::move( additionalReporter ) );
} }