From 218d0bdcc75cccc0871fdc0c46a229a88d39f034 Mon Sep 17 00:00:00 2001 From: Eirik Byrkjeflot Anonsen Date: Mon, 4 Apr 2016 18:51:33 +0200 Subject: [PATCH] Eliminate dynamic cast by promoting interface to parent class This eliminates one use of RTTI in Catch. --- include/internal/catch_interfaces_reporter.h | 7 +++++++ include/reporters/catch_reporter_multi.hpp | 16 +++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/internal/catch_interfaces_reporter.h b/include/internal/catch_interfaces_reporter.h index d1c6e704..26d20e24 100644 --- a/include/internal/catch_interfaces_reporter.h +++ b/include/internal/catch_interfaces_reporter.h @@ -226,6 +226,13 @@ namespace Catch // Implementing class must also provide the following static method: // static std::string getDescription(); + virtual bool supportsChainedReporters() const { + return false; + } + + virtual void addChainedReporter( Ptr const& reporter ) { + } + virtual ReporterPreferences getPreferences() const = 0; virtual void noMatchingTestCases( std::string const& spec ) = 0; diff --git a/include/reporters/catch_reporter_multi.hpp b/include/reporters/catch_reporter_multi.hpp index d3282666..9023a3f1 100644 --- a/include/reporters/catch_reporter_multi.hpp +++ b/include/reporters/catch_reporter_multi.hpp @@ -22,6 +22,13 @@ public: } public: // IStreamingReporter + virtual bool supportsChainedReporters() const CATCH_OVERRIDE { + return true; + } + + virtual void addChainedReporter( Ptr const& reporter ) CATCH_OVERRIDE { + add( reporter ); + } virtual ReporterPreferences getPreferences() const CATCH_OVERRIDE { return m_reporters[0]->getPreferences(); @@ -124,16 +131,15 @@ Ptr addReporter( Ptr const& existingRepo Ptr resultingReporter; if( existingReporter ) { - MultipleReporters* multi = dynamic_cast( existingReporter.get() ); - if( !multi ) { - multi = new MultipleReporters; + if( !existingReporter->supportsChainedReporters() ) { + MultipleReporters* multi = new MultipleReporters; resultingReporter = Ptr( multi ); if( existingReporter ) - multi->add( existingReporter ); + multi->addChainedReporter( existingReporter ); } else resultingReporter = existingReporter; - multi->add( additionalReporter ); + resultingReporter->addChainedReporter( additionalReporter ); } else resultingReporter = additionalReporter;