29 template<
typename WriterF, std::
size_t bufferSize=256>
30 class StreamBufImpl :
public std::streambuf {
31 char data[bufferSize];
36 setp( data, data +
sizeof(data) );
39 ~StreamBufImpl()
noexcept {
40 StreamBufImpl::sync();
48 if( pbase() == epptr() )
49 m_writer( std::string( 1,
static_cast<char>( c ) ) );
51 sputc(
static_cast<char>( c ) );
57 if( pbase() != pptr() ) {
58 m_writer( std::string( pbase(),
static_cast<std::string::size_type
>( pptr() - pbase() ) ) );
59 setp( pbase(), epptr() );
67 struct OutputDebugWriter {
69 void operator()( std::string
const&str ) {
76 class FileStream :
public IStream {
77 mutable std::ofstream m_ofs;
80 m_ofs.open( filename.
c_str() );
81 CATCH_ENFORCE( !m_ofs.fail(),
"Unable to open file: '" << filename <<
"'" );
83 ~FileStream()
override =
default;
85 std::ostream& stream()
const override {
92 class CoutStream :
public IStream {
93 mutable std::ostream m_os;
98 ~CoutStream()
override =
default;
101 std::ostream& stream()
const override {
return m_os; }
106 class DebugOutStream :
public IStream {
107 std::unique_ptr<StreamBufImpl<OutputDebugWriter>> m_streamBuf;
108 mutable std::ostream m_os;
111 : m_streamBuf(
new StreamBufImpl<OutputDebugWriter>() ),
112 m_os( m_streamBuf.get() )
115 ~DebugOutStream()
override =
default;
118 std::ostream& stream()
const override {
return m_os; }
126 if( filename.empty() )
127 return new detail::CoutStream();
128 else if( filename[0] ==
'%' ) {
129 if( filename ==
"%debug" )
130 return new detail::DebugOutStream();
132 CATCH_ERROR(
"Unrecognised stream: '" << filename <<
"'" );
135 return new detail::FileStream( filename );
141 std::vector<std::unique_ptr<std::ostringstream>>
m_streams;
145 auto add() -> std::size_t {
147 m_streams.push_back( std::unique_ptr<std::ostringstream>(
new std::ostringstream ) );
169 static_cast<std::ostringstream*
>( m_oss )->
str(
"");
175 return static_cast<std::ostringstream*
>( m_oss )->
str();
182#ifndef CATCH_CONFIG_NOSTDOUT
183 std::ostream&
cout() {
return std::cout; }
184 std::ostream&
cerr() {
return std::cerr; }
185 std::ostream&
clog() {
return std::clog; }
#define CATCH_ENFORCE(condition, msg)
auto str() const -> std::string
static auto getMutable() -> MutableInterfaceT &
auto c_str() const -> char const *
void writeToDebugConsole(std::string const &text)
auto makeStream(StringRef const &filename) -> IStream const *
auto add() -> std::size_t
std::ostringstream m_referenceStream
std::vector< std::size_t > m_unused
void release(std::size_t index)
std::vector< std::unique_ptr< std::ostringstream > > m_streams
void setp(const Operand &op)