Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/catch2/internal/catch_output_redirect.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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_OUTPUT_REDIRECT_HPP_INCLUDED
0009 #define CATCH_OUTPUT_REDIRECT_HPP_INCLUDED
0010 
0011 #include <catch2/internal/catch_unique_ptr.hpp>
0012 
0013 #include <cassert>
0014 #include <string>
0015 
0016 namespace Catch {
0017 
0018     class OutputRedirect {
0019         bool m_redirectActive = false;
0020         virtual void activateImpl() = 0;
0021         virtual void deactivateImpl() = 0;
0022     public:
0023         enum Kind {
0024             //! No redirect (noop implementation)
0025             None,
0026             //! Redirect std::cout/std::cerr/std::clog streams internally
0027             Streams,
0028             //! Redirect the stdout/stderr file descriptors into files
0029             FileDescriptors,
0030         };
0031 
0032         virtual ~OutputRedirect(); // = default;
0033 
0034         // TODO: Do we want to check that redirect is not active before retrieving the output?
0035         virtual std::string getStdout() = 0;
0036         virtual std::string getStderr() = 0;
0037         virtual void clearBuffers() = 0;
0038         bool isActive() const { return m_redirectActive; }
0039         void activate() {
0040             assert( !m_redirectActive && "redirect is already active" );
0041             activateImpl();
0042             m_redirectActive = true;
0043         }
0044         void deactivate() {
0045             assert( m_redirectActive && "redirect is not active" );
0046             deactivateImpl();
0047             m_redirectActive = false;
0048         }
0049     };
0050 
0051     bool isRedirectAvailable( OutputRedirect::Kind kind);
0052     Detail::unique_ptr<OutputRedirect> makeOutputRedirect( bool actual );
0053 
0054     class RedirectGuard {
0055         OutputRedirect* m_redirect;
0056         bool m_activate;
0057         bool m_previouslyActive;
0058         bool m_moved = false;
0059 
0060     public:
0061         RedirectGuard( bool activate, OutputRedirect& redirectImpl );
0062         ~RedirectGuard() noexcept( false );
0063 
0064         RedirectGuard( RedirectGuard const& ) = delete;
0065         RedirectGuard& operator=( RedirectGuard const& ) = delete;
0066 
0067         // C++14 needs move-able guards to return them from functions
0068         RedirectGuard( RedirectGuard&& rhs ) noexcept;
0069         RedirectGuard& operator=( RedirectGuard&& rhs ) noexcept;
0070     };
0071 
0072     RedirectGuard scopedActivate( OutputRedirect& redirectImpl );
0073     RedirectGuard scopedDeactivate( OutputRedirect& redirectImpl );
0074 
0075 } // end namespace Catch
0076 
0077 #endif // CATCH_OUTPUT_REDIRECT_HPP_INCLUDED