Files
boost_beast/extras/beast/unit_test/dstream.hpp
T

133 lines
2.5 KiB
C++
Raw Normal View History

2016-05-09 20:13:20 -04:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2016-05-09 20:13:20 -04:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_UNIT_TEST_DSTREAM_HPP
#define BEAST_UNIT_TEST_DSTREAM_HPP
2016-09-02 13:54:27 -04:00
#include <ios>
2016-05-09 20:13:20 -04:00
#include <memory>
#include <ostream>
2016-09-02 13:54:27 -04:00
#include <streambuf>
2016-05-09 20:13:20 -04:00
#include <string>
#ifdef _MSC_VER
# ifndef NOMINMAX
# define NOMINMAX 1
# endif
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# undef WIN32_LEAN_AND_MEAN
# undef NOMINMAX
#endif
namespace beast {
namespace unit_test {
#ifdef _MSC_VER
2016-09-02 13:54:27 -04:00
namespace detail {
2016-05-09 20:13:20 -04:00
template<class CharT, class Traits, class Allocator>
class dstream_buf
: public std::basic_stringbuf<CharT, Traits, Allocator>
{
2016-09-02 13:54:27 -04:00
using ostream = std::basic_ostream<CharT, Traits>;
2016-05-09 20:13:20 -04:00
bool dbg_;
2016-09-02 13:54:27 -04:00
ostream& os_;
2016-05-09 20:13:20 -04:00
template<class T>
void write(T const*) = delete;
void write(char const* s)
{
if(dbg_)
OutputDebugStringA(s);
2016-09-02 13:54:27 -04:00
os_ << s;
2016-05-09 20:13:20 -04:00
}
void write(wchar_t const* s)
{
if(dbg_)
OutputDebugStringW(s);
2016-09-02 13:54:27 -04:00
os_ << s;
2016-05-09 20:13:20 -04:00
}
public:
2016-09-02 13:54:27 -04:00
explicit
dstream_buf(ostream& os)
: os_(os)
, dbg_(IsDebuggerPresent() != FALSE)
2016-05-09 20:13:20 -04:00
{
}
~dstream_buf()
{
sync();
}
int
sync() override
{
2016-05-10 07:09:57 -04:00
write(this->str().c_str());
this->str("");
2016-05-09 20:13:20 -04:00
return 0;
}
};
} // detail
2016-09-02 13:54:27 -04:00
/** std::ostream with Visual Studio IDE redirection.
Instances of this stream wrap a specified `std::ostream`
(such as `std::cout` or `std::cerr`). If the IDE debugger
is attached when the stream is created, output will be
additionally copied to the Visual Studio Output window.
*/
2016-05-09 20:13:20 -04:00
template<
class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
>
class basic_dstream
2016-09-02 13:54:27 -04:00
: public std::basic_ostream<CharT, Traits>
2016-05-09 20:13:20 -04:00
{
2016-09-02 13:54:27 -04:00
detail::dstream_buf<
CharT, Traits, Allocator> buf_;
2016-05-09 20:13:20 -04:00
public:
2016-09-02 13:54:27 -04:00
/** Construct a stream.
@param os The output stream to wrap.
*/
explicit
basic_dstream(std::ostream& os)
: std::basic_ostream<CharT, Traits>(&buf_)
, buf_(os)
2016-05-09 20:13:20 -04:00
{
2017-02-05 14:42:58 -08:00
if(os.flags() & std::ios::unitbuf)
2016-09-02 13:54:27 -04:00
std::unitbuf(*this);
2016-05-09 20:13:20 -04:00
}
};
using dstream = basic_dstream<char>;
using dwstream = basic_dstream<wchar_t>;
2016-09-02 13:54:27 -04:00
#else
using dstream = std::ostream&;
using dwstream = std::wostream&;
#endif
} // unit_test
2016-05-09 20:13:20 -04:00
} // beast
#endif