From 4f09f1120ba0a28f218a00821a8347a443042ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Tue, 12 Apr 2022 20:59:01 +0200 Subject: [PATCH] Make IStream::stream non-const This way it makes much more sense from logically-const point of view, and also means that concrete implementations don't have to always have a `mutable` keyword on the stream member. --- src/catch2/catch_config.cpp | 4 ++-- src/catch2/catch_config.hpp | 4 ++-- .../interfaces/catch_interfaces_reporter.cpp | 4 ++-- .../interfaces/catch_interfaces_reporter.hpp | 6 +++--- src/catch2/internal/catch_console_colour.cpp | 8 ++++---- src/catch2/internal/catch_console_colour.hpp | 6 +++--- src/catch2/internal/catch_stream.cpp | 18 +++++++++--------- src/catch2/internal/catch_stream.hpp | 4 ++-- .../reporters/catch_reporter_common_base.hpp | 2 +- .../IntrospectiveTests/ColourImpl.tests.cpp | 4 ++-- .../IntrospectiveTests/Reporters.tests.cpp | 4 ++-- 11 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/catch2/catch_config.cpp b/src/catch2/catch_config.cpp index 394ad8fe..9f112bd8 100644 --- a/src/catch2/catch_config.cpp +++ b/src/catch2/catch_config.cpp @@ -108,8 +108,8 @@ namespace Catch { return m_data.reporterSpecifications; } - IStream const* Config::getReporterOutputStream(std::size_t reporterIdx) const { - return m_reporterStreams.at(reporterIdx).get(); + IStream* Config::getReporterOutputStream(std::size_t reporterIdx) const { + return const_cast(m_reporterStreams.at(reporterIdx).get()); } TestSpec const& Config::testSpec() const { return m_testSpec; } diff --git a/src/catch2/catch_config.hpp b/src/catch2/catch_config.hpp index 582eb3e6..bed9b088 100644 --- a/src/catch2/catch_config.hpp +++ b/src/catch2/catch_config.hpp @@ -81,7 +81,7 @@ namespace Catch { bool listReporters() const; std::vector const& getReporterSpecs() const; - IStream const* getReporterOutputStream(std::size_t reporterIdx) const; + IStream* getReporterOutputStream(std::size_t reporterIdx) const; std::vector const& getTestsOrTags() const override; std::vector const& getSectionsToRun() const override; @@ -118,7 +118,7 @@ namespace Catch { private: ConfigData m_data; - std::vector> m_reporterStreams; + std::vector> m_reporterStreams; TestSpec m_testSpec; bool m_hasTestFilters = false; }; diff --git a/src/catch2/interfaces/catch_interfaces_reporter.cpp b/src/catch2/interfaces/catch_interfaces_reporter.cpp index 585d4365..926331e0 100644 --- a/src/catch2/interfaces/catch_interfaces_reporter.cpp +++ b/src/catch2/interfaces/catch_interfaces_reporter.cpp @@ -23,7 +23,7 @@ namespace Catch { ReporterConfig::ReporterConfig( IConfig const* _fullConfig, - IStream const* _stream, + IStream* _stream, ColourMode colourMode, std::map customOptions ): m_stream( _stream ), @@ -31,7 +31,7 @@ namespace Catch { m_colourMode( colourMode ), m_customOptions( CATCH_MOVE( customOptions ) ) {} - IStream const* ReporterConfig::stream() const { return m_stream; } + IStream* ReporterConfig::stream() const { return m_stream; } IConfig const * ReporterConfig::fullConfig() const { return m_fullConfig; } ColourMode ReporterConfig::colourMode() const { return m_colourMode; } diff --git a/src/catch2/interfaces/catch_interfaces_reporter.hpp b/src/catch2/interfaces/catch_interfaces_reporter.hpp index 4cf98ba1..21a16cde 100644 --- a/src/catch2/interfaces/catch_interfaces_reporter.hpp +++ b/src/catch2/interfaces/catch_interfaces_reporter.hpp @@ -36,17 +36,17 @@ namespace Catch { struct ReporterConfig { ReporterConfig( IConfig const* _fullConfig, - IStream const* _stream, + IStream* _stream, ColourMode colourMode, std::map customOptions ); - IStream const* stream() const; + IStream* stream() const; IConfig const* fullConfig() const; ColourMode colourMode() const; std::map const& customOptions() const; private: - IStream const* m_stream; + IStream* m_stream; IConfig const* m_fullConfig; ColourMode m_colourMode; std::map m_customOptions; diff --git a/src/catch2/internal/catch_console_colour.cpp b/src/catch2/internal/catch_console_colour.cpp index 67f0d98a..e9fa55f7 100644 --- a/src/catch2/internal/catch_console_colour.cpp +++ b/src/catch2/internal/catch_console_colour.cpp @@ -84,7 +84,7 @@ namespace Catch { //! platforms, and when the user asks to deactivate all colours. class NoColourImpl : public ColourImpl { public: - NoColourImpl( IStream const* stream ): ColourImpl( stream ) {} + NoColourImpl( IStream* stream ): ColourImpl( stream ) {} static bool useColourOnPlatform() { return true; } private: @@ -103,7 +103,7 @@ namespace { class Win32ColourImpl : public ColourImpl { public: - Win32ColourImpl(IStream const* stream): + Win32ColourImpl(IStream* stream): ColourImpl(stream) { CONSOLE_SCREEN_BUFFER_INFO csbiInfo; GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), @@ -169,7 +169,7 @@ namespace { class ANSIColourImpl : public ColourImpl { public: - ANSIColourImpl( IStream const* stream ): ColourImpl( stream ) {} + ANSIColourImpl( IStream* stream ): ColourImpl( stream ) {} static bool useColourOnPlatform(IStream const& stream) { // This is kinda messy due to trying to support a bunch of @@ -229,7 +229,7 @@ namespace { namespace Catch { Detail::unique_ptr makeColourImpl( ColourMode implSelection, - IStream const* stream ) { + IStream* stream ) { if ( implSelection == ColourMode::None ) { return Detail::make_unique( stream ); } diff --git a/src/catch2/internal/catch_console_colour.hpp b/src/catch2/internal/catch_console_colour.hpp index bf7cb893..661078af 100644 --- a/src/catch2/internal/catch_console_colour.hpp +++ b/src/catch2/internal/catch_console_colour.hpp @@ -60,9 +60,9 @@ namespace Catch { class ColourImpl { protected: //! The associated stream of this ColourImpl instance - IStream const* m_stream; + IStream* m_stream; public: - ColourImpl( IStream const* stream ): m_stream( stream ) {} + ColourImpl( IStream* stream ): m_stream( stream ) {} //! RAII wrapper around writing specific colour of text using specific //! colour impl into a stream. @@ -131,7 +131,7 @@ namespace Catch { //! Provides ColourImpl based on global config and target compilation platform Detail::unique_ptr makeColourImpl( ColourMode colourSelection, - IStream const* stream ); + IStream* stream ); //! Checks if specific colour impl has been compiled into the binary bool isColourImplAvailable( ColourMode colourSelection ); diff --git a/src/catch2/internal/catch_stream.cpp b/src/catch2/internal/catch_stream.cpp index dfa322c7..089a7583 100644 --- a/src/catch2/internal/catch_stream.cpp +++ b/src/catch2/internal/catch_stream.cpp @@ -74,7 +74,7 @@ namespace Detail { /////////////////////////////////////////////////////////////////////////// class FileStream : public IStream { - mutable std::ofstream m_ofs; + std::ofstream m_ofs; public: FileStream( std::string const& filename ) { m_ofs.open( filename.c_str() ); @@ -82,7 +82,7 @@ namespace Detail { } ~FileStream() override = default; public: // IStream - std::ostream& stream() const override { + std::ostream& stream() override { return m_ofs; } }; @@ -90,7 +90,7 @@ namespace Detail { /////////////////////////////////////////////////////////////////////////// class CoutStream : public IStream { - mutable std::ostream m_os; + std::ostream m_os; public: // Store the streambuf from cout up-front because // cout may get redirected when running tests @@ -98,12 +98,12 @@ namespace Detail { ~CoutStream() override = default; public: // IStream - std::ostream& stream() const override { return m_os; } + std::ostream& stream() override { return m_os; } bool isConsole() const override { return true; } }; class CerrStream : public IStream { - mutable std::ostream m_os; + std::ostream m_os; public: // Store the streambuf from cerr up-front because @@ -112,7 +112,7 @@ namespace Detail { ~CerrStream() override = default; public: // IStream - std::ostream& stream() const override { return m_os; } + std::ostream& stream() override { return m_os; } bool isConsole() const override { return true; } }; @@ -120,7 +120,7 @@ namespace Detail { class DebugOutStream : public IStream { Detail::unique_ptr> m_streamBuf; - mutable std::ostream m_os; + std::ostream m_os; public: DebugOutStream() : m_streamBuf( Detail::make_unique>() ), @@ -130,7 +130,7 @@ namespace Detail { ~DebugOutStream() override = default; public: // IStream - std::ostream& stream() const override { return m_os; } + std::ostream& stream() override { return m_os; } }; } // unnamed namespace @@ -138,7 +138,7 @@ namespace Detail { /////////////////////////////////////////////////////////////////////////// - auto makeStream( std::string const& filename ) -> Detail::unique_ptr { + auto makeStream( std::string const& filename ) -> Detail::unique_ptr { if ( filename.empty() || filename == "-" ) { return Detail::make_unique(); } diff --git a/src/catch2/internal/catch_stream.hpp b/src/catch2/internal/catch_stream.hpp index 41c1fef0..eab7161c 100644 --- a/src/catch2/internal/catch_stream.hpp +++ b/src/catch2/internal/catch_stream.hpp @@ -25,7 +25,7 @@ namespace Catch { class IStream { public: virtual ~IStream(); // = default - virtual std::ostream& stream() const = 0; + virtual std::ostream& stream() = 0; /** * Best guess on whether the instance is writing to a console (e.g. via stdout/stderr) * @@ -51,7 +51,7 @@ namespace Catch { * * \throws if passed an unrecognized %-prefixed stream */ - auto makeStream( std::string const& filename ) -> Detail::unique_ptr; + auto makeStream( std::string const& filename ) -> Detail::unique_ptr; class ReusableStringStream : Detail::NonCopyable { std::size_t m_index; diff --git a/src/catch2/reporters/catch_reporter_common_base.hpp b/src/catch2/reporters/catch_reporter_common_base.hpp index e110763b..8b2e6c08 100644 --- a/src/catch2/reporters/catch_reporter_common_base.hpp +++ b/src/catch2/reporters/catch_reporter_common_base.hpp @@ -29,7 +29,7 @@ namespace Catch { class ReporterBase : public IEventListener { protected: //! The stream wrapper as passed to us by outside code - IStream const* m_wrapped_stream; + IStream* m_wrapped_stream; //! Cached output stream from `m_wrapped_stream` to reduce //! number of indirect calls needed to write output. std::ostream& m_stream; diff --git a/tests/SelfTest/IntrospectiveTests/ColourImpl.tests.cpp b/tests/SelfTest/IntrospectiveTests/ColourImpl.tests.cpp index 4da7c4b3..716d6033 100644 --- a/tests/SelfTest/IntrospectiveTests/ColourImpl.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/ColourImpl.tests.cpp @@ -21,9 +21,9 @@ namespace { }; class TestStringStream : public Catch::IStream { - mutable std::stringstream m_stream; + std::stringstream m_stream; public: - std::ostream& stream() const override { + std::ostream& stream() override { return m_stream; } diff --git a/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp b/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp index 708f0903..84f2b689 100644 --- a/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp @@ -29,10 +29,10 @@ namespace { class StringIStream : public Catch::IStream { public: - std::ostream& stream() const override { return sstr; } + std::ostream& stream() override { return sstr; } std::string str() const { return sstr.str(); } private: - mutable std::stringstream sstr; + std::stringstream sstr; }; }