Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:05

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
0008 #ifndef CATCH_CONSOLE_COLOUR_HPP_INCLUDED
0009 #define CATCH_CONSOLE_COLOUR_HPP_INCLUDED
0010 
0011 #include <catch2/internal/catch_unique_ptr.hpp>
0012 
0013 #include <iosfwd>
0014 #include <cstdint>
0015 
0016 namespace Catch {
0017 
0018     enum class ColourMode : std::uint8_t;
0019     class IStream;
0020 
0021     struct Colour {
0022         enum Code {
0023             None = 0,
0024 
0025             White,
0026             Red,
0027             Green,
0028             Blue,
0029             Cyan,
0030             Yellow,
0031             Grey,
0032 
0033             Bright = 0x10,
0034 
0035             BrightRed = Bright | Red,
0036             BrightGreen = Bright | Green,
0037             LightGrey = Bright | Grey,
0038             BrightWhite = Bright | White,
0039             BrightYellow = Bright | Yellow,
0040 
0041             // By intention
0042             FileName = LightGrey,
0043             Warning = BrightYellow,
0044             ResultError = BrightRed,
0045             ResultSuccess = BrightGreen,
0046             ResultExpectedFailure = Warning,
0047 
0048             Error = BrightRed,
0049             Success = Green,
0050             Skip = LightGrey,
0051 
0052             OriginalExpression = Cyan,
0053             ReconstructedExpression = BrightYellow,
0054 
0055             SecondaryText = LightGrey,
0056             Headers = White
0057         };
0058     };
0059 
0060     class ColourImpl {
0061     protected:
0062         //! The associated stream of this ColourImpl instance
0063         IStream* m_stream;
0064     public:
0065         ColourImpl( IStream* stream ): m_stream( stream ) {}
0066 
0067         //! RAII wrapper around writing specific colour of text using specific
0068         //! colour impl into a stream.
0069         class ColourGuard {
0070             ColourImpl const* m_colourImpl;
0071             Colour::Code m_code;
0072             bool m_engaged = false;
0073 
0074         public:
0075             //! Does **not** engage the guard/start the colour
0076             ColourGuard( Colour::Code code,
0077                          ColourImpl const* colour );
0078 
0079             ColourGuard( ColourGuard const& rhs ) = delete;
0080             ColourGuard& operator=( ColourGuard const& rhs ) = delete;
0081 
0082             ColourGuard( ColourGuard&& rhs ) noexcept;
0083             ColourGuard& operator=( ColourGuard&& rhs ) noexcept;
0084 
0085             //! Removes colour _if_ the guard was engaged
0086             ~ColourGuard();
0087 
0088             /**
0089              * Explicitly engages colour for given stream.
0090              *
0091              * The API based on operator<< should be preferred.
0092              */
0093             ColourGuard& engage( std::ostream& stream ) &;
0094             /**
0095              * Explicitly engages colour for given stream.
0096              *
0097              * The API based on operator<< should be preferred.
0098              */
0099             ColourGuard&& engage( std::ostream& stream ) &&;
0100 
0101         private:
0102             //! Engages the guard and starts using colour
0103             friend std::ostream& operator<<( std::ostream& lhs,
0104                                              ColourGuard& guard ) {
0105                 guard.engageImpl( lhs );
0106                 return lhs;
0107             }
0108             //! Engages the guard and starts using colour
0109             friend std::ostream& operator<<( std::ostream& lhs,
0110                                             ColourGuard&& guard) {
0111                 guard.engageImpl( lhs );
0112                 return lhs;
0113             }
0114 
0115             void engageImpl( std::ostream& stream );
0116 
0117         };
0118 
0119         virtual ~ColourImpl(); // = default
0120         /**
0121          * Creates a guard object for given colour and this colour impl
0122          *
0123          * **Important:**
0124          * the guard starts disengaged, and has to be engaged explicitly.
0125          */
0126         ColourGuard guardColour( Colour::Code colourCode );
0127 
0128     private:
0129         virtual void use( Colour::Code colourCode ) const = 0;
0130     };
0131 
0132     //! Provides ColourImpl based on global config and target compilation platform
0133     Detail::unique_ptr<ColourImpl> makeColourImpl( ColourMode colourSelection,
0134                                                    IStream* stream );
0135 
0136     //! Checks if specific colour impl has been compiled into the binary
0137     bool isColourImplAvailable( ColourMode colourSelection );
0138 
0139 } // end namespace Catch
0140 
0141 #endif // CATCH_CONSOLE_COLOUR_HPP_INCLUDED