diff options
| author | Martin Hořeňovský | 2022-03-21 23:20:08 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2022-03-24 16:02:54 +0100 |
| commit | 081a1e9abaf32a759fd08a01fcf7a65860f91a63 (patch) | |
| tree | 2bbc0f40950d524596e4aa3b79b665ca3eb89adb /src/catch2/internal | |
| parent | 4d8acafecb973d9b03178346c3f1566fe7485fb1 (diff) | |
| download | catch3-081a1e9abaf32a759fd08a01fcf7a65860f91a63.tar.gz | |
ColourGuard is no longer constructed engaged
Forcing it to be engaged explicitly, either via `op<<`, or by
`ColourGuard::engage`, fixes an issue with multiple `ColourGuard`s
being constructed in a single expression. Because the construction
of the `ColourGuard` instances can happen in arbitrary order,
colours would be applied in arbitrary order too. However, a chain
of `op<<`s has strict call orders, fixing this issue.
Diffstat (limited to 'src/catch2/internal')
| -rw-r--r-- | src/catch2/internal/catch_console_colour.cpp | 48 | ||||
| -rw-r--r-- | src/catch2/internal/catch_console_colour.hpp | 57 |
2 files changed, 86 insertions, 19 deletions
diff --git a/src/catch2/internal/catch_console_colour.cpp b/src/catch2/internal/catch_console_colour.cpp index 7f294abc..b7b4b4fb 100644 --- a/src/catch2/internal/catch_console_colour.cpp +++ b/src/catch2/internal/catch_console_colour.cpp @@ -27,7 +27,7 @@ namespace Catch { ColourImpl::~ColourImpl() = default; - ColourImpl::ColourGuard ColourImpl::startColour( Colour::Code colourCode ) { + ColourImpl::ColourGuard ColourImpl::guardColour( Colour::Code colourCode ) { return ColourGuard(colourCode, this ); } @@ -45,27 +45,53 @@ namespace Catch { } // namespace + void ColourImpl::ColourGuard::engageImpl( std::ostream& stream ) { + assert( &stream == &m_colourImpl->m_stream->stream() && + "Engaging colour guard for different stream than used by the " + "parent colour implementation" ); + static_cast<void>( stream ); + + m_engaged = true; + m_colourImpl->use( m_code ); + } + ColourImpl::ColourGuard::ColourGuard( Colour::Code code, - ColourImpl const* colour ): - m_colourImpl( colour ) { - m_colourImpl->use( code ); + ColourImpl const* colour ): + m_colourImpl( colour ), m_code( code ) { } - ColourImpl::ColourGuard::ColourGuard( ColourGuard&& rhs ): - m_colourImpl( rhs.m_colourImpl ) { - rhs.m_moved = true; + ColourImpl::ColourGuard::ColourGuard( ColourGuard&& rhs ) noexcept: + m_colourImpl( rhs.m_colourImpl ), + m_code( rhs.m_code ), + m_engaged( rhs.m_engaged ) { + rhs.m_engaged = false; } ColourImpl::ColourGuard& - ColourImpl::ColourGuard::operator=( ColourGuard&& rhs ) { - m_colourImpl = rhs.m_colourImpl; - rhs.m_moved = true; + ColourImpl::ColourGuard::operator=( ColourGuard&& rhs ) noexcept { + using std::swap; + swap( m_colourImpl, rhs.m_colourImpl ); + swap( m_code, rhs.m_code ); + swap( m_engaged, rhs.m_engaged ); + return *this; } ColourImpl::ColourGuard::~ColourGuard() { - if ( !m_moved ) { + if ( m_engaged ) { m_colourImpl->use( Colour::None ); } } + ColourImpl::ColourGuard& + ColourImpl::ColourGuard::engage( std::ostream& stream ) & { + engageImpl( stream ); + return *this; + } + + ColourImpl::ColourGuard&& + ColourImpl::ColourGuard::engage( std::ostream& stream ) && { + engageImpl( stream ); + return CATCH_MOVE(*this); + } + } // namespace Catch #if !defined( CATCH_CONFIG_COLOUR_NONE ) && !defined( CATCH_CONFIG_COLOUR_WINDOWS ) && !defined( CATCH_CONFIG_COLOUR_ANSI ) diff --git a/src/catch2/internal/catch_console_colour.hpp b/src/catch2/internal/catch_console_colour.hpp index e6a37967..7b8f5b7a 100644 --- a/src/catch2/internal/catch_console_colour.hpp +++ b/src/catch2/internal/catch_console_colour.hpp @@ -62,25 +62,66 @@ namespace Catch { public: ColourImpl( IStream const* stream ): m_stream( stream ) {} + //! RAII wrapper around writing specific colour of text using specific + //! colour impl into a stream. class ColourGuard { ColourImpl const* m_colourImpl; - bool m_moved = false; - friend std::ostream& operator<<(std::ostream& lhs, - ColourGuard const&) { - return lhs; - } + Colour::Code m_code; + bool m_engaged = false; + public: + //! Does **not** engage the guard/start the colour ColourGuard( Colour::Code code, ColourImpl const* colour ); + ColourGuard( ColourGuard const& rhs ) = delete; ColourGuard& operator=( ColourGuard const& rhs ) = delete; - ColourGuard( ColourGuard&& rhs ); - ColourGuard& operator=( ColourGuard&& rhs ); + + ColourGuard( ColourGuard&& rhs ) noexcept; + ColourGuard& operator=( ColourGuard&& rhs ) noexcept; + + //! Removes colour _if_ the guard was engaged ~ColourGuard(); + + /** + * Explicitly engages colour for given stream. + * + * The API based on operator<< should be preferred. + */ + ColourGuard& engage( std::ostream& stream ) &; + /** + * Explicitly engages colour for given stream. + * + * The API based on operator<< should be preferred. + */ + ColourGuard&& engage( std::ostream& stream ) &&; + + private: + //! Engages the guard and starts using colour + friend std::ostream& operator<<( std::ostream& lhs, + ColourGuard& guard ) { + guard.engageImpl( lhs ); + return lhs; + } + //! Engages the guard and starts using colour + friend std::ostream& operator<<( std::ostream& lhs, + ColourGuard&& guard) { + guard.engageImpl( lhs ); + return lhs; + } + + void engageImpl( std::ostream& stream ); + }; virtual ~ColourImpl(); // = default - ColourGuard startColour( Colour::Code colourCode ); + /** + * Creates a guard object for given colour and this colour impl + * + * **Important:** + * the guard starts disengaged, and has to be engaged explicitly. + */ + ColourGuard guardColour( Colour::Code colourCode ); private: virtual void use( Colour::Code colourCode ) const = 0; |