File indexing completed on 2025-01-19 09:25:00
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_BEAST_UNIT_TEST_DSTREAM_HPP
0011 #define BOOST_BEAST_UNIT_TEST_DSTREAM_HPP
0012
0013 #include <boost/config.hpp>
0014 #include <ios>
0015 #include <memory>
0016 #include <ostream>
0017 #include <sstream>
0018 #include <streambuf>
0019 #include <string>
0020
0021 #ifdef BOOST_WINDOWS
0022 #include <boost/winapi/basic_types.hpp>
0023 #include <boost/winapi/debugapi.hpp>
0024 #endif
0025
0026 namespace boost {
0027 namespace beast {
0028 namespace unit_test {
0029
0030 #ifdef BOOST_WINDOWS
0031
0032 namespace detail {
0033
0034 template<class CharT, class Traits, class Allocator>
0035 class dstream_buf
0036 : public std::basic_stringbuf<CharT, Traits, Allocator>
0037 {
0038 using ostream = std::basic_ostream<CharT, Traits>;
0039
0040 ostream& os_;
0041 bool dbg_;
0042
0043 template<class T>
0044 void write(T const*) = delete;
0045
0046 void write(char const* s)
0047 {
0048 if(dbg_)
0049 boost::winapi::OutputDebugStringA(s);
0050 os_ << s;
0051 }
0052
0053 void write(wchar_t const* s)
0054 {
0055 if(dbg_)
0056 boost::winapi::OutputDebugStringW(s);
0057 os_ << s;
0058 }
0059
0060 public:
0061 explicit
0062 dstream_buf(ostream& os)
0063 : os_(os)
0064 , dbg_(boost::winapi::IsDebuggerPresent() != 0)
0065 {
0066 }
0067
0068 ~dstream_buf()
0069 {
0070 sync();
0071 }
0072
0073 int
0074 sync() override
0075 {
0076 write(this->str().c_str());
0077 this->str("");
0078 return 0;
0079 }
0080 };
0081
0082 }
0083
0084
0085
0086
0087
0088
0089
0090
0091 template<
0092 class CharT,
0093 class Traits = std::char_traits<CharT>,
0094 class Allocator = std::allocator<CharT>
0095 >
0096 class basic_dstream
0097 : public std::basic_ostream<CharT, Traits>
0098 {
0099 detail::dstream_buf<
0100 CharT, Traits, Allocator> buf_;
0101
0102 public:
0103
0104
0105
0106
0107 explicit
0108 basic_dstream(std::ostream& os)
0109 : std::basic_ostream<CharT, Traits>(&buf_)
0110 , buf_(os)
0111 {
0112 if(os.flags() & std::ios::unitbuf)
0113 std::unitbuf(*this);
0114 }
0115 };
0116
0117 using dstream = basic_dstream<char>;
0118 using dwstream = basic_dstream<wchar_t>;
0119
0120 #else
0121
0122 using dstream = std::ostream&;
0123 using dwstream = std::wostream&;
0124
0125 #endif
0126
0127 }
0128 }
0129 }
0130
0131 #endif