]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/subtree/unit_test/include/boost/beast/unit_test/dstream.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / beast / subtree / unit_test / include / boost / beast / unit_test / dstream.hpp
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 #ifndef BOOST_BEAST_UNIT_TEST_DSTREAM_HPP
11 #define BOOST_BEAST_UNIT_TEST_DSTREAM_HPP
12
13 #include <boost/config.hpp>
14 #include <ios>
15 #include <memory>
16 #include <ostream>
17 #include <streambuf>
18 #include <string>
19
20 #ifdef BOOST_WINDOWS
21 #include <boost/winapi/basic_types.hpp>
22 #include <boost/winapi/debugapi.hpp>
23 #endif
24
25 namespace boost {
26 namespace beast {
27 namespace unit_test {
28
29 #ifdef BOOST_WINDOWS
30
31 namespace detail {
32
33 template<class CharT, class Traits, class Allocator>
34 class dstream_buf
35 : public std::basic_stringbuf<CharT, Traits, Allocator>
36 {
37 using ostream = std::basic_ostream<CharT, Traits>;
38
39 ostream& os_;
40 bool dbg_;
41
42 template<class T>
43 void write(T const*) = delete;
44
45 void write(char const* s)
46 {
47 if(dbg_)
48 boost::winapi::OutputDebugStringA(s);
49 os_ << s;
50 }
51
52 void write(wchar_t const* s)
53 {
54 if(dbg_)
55 boost::winapi::OutputDebugStringW(s);
56 os_ << s;
57 }
58
59 public:
60 explicit
61 dstream_buf(ostream& os)
62 : os_(os)
63 , dbg_(boost::winapi::IsDebuggerPresent() != 0)
64 {
65 }
66
67 ~dstream_buf()
68 {
69 sync();
70 }
71
72 int
73 sync() override
74 {
75 write(this->str().c_str());
76 this->str("");
77 return 0;
78 }
79 };
80
81 } // detail
82
83 /** std::ostream with Visual Studio IDE redirection.
84
85 Instances of this stream wrap a specified `std::ostream`
86 (such as `std::cout` or `std::cerr`). If the IDE debugger
87 is attached when the stream is created, output will be
88 additionally copied to the Visual Studio Output window.
89 */
90 template<
91 class CharT,
92 class Traits = std::char_traits<CharT>,
93 class Allocator = std::allocator<CharT>
94 >
95 class basic_dstream
96 : public std::basic_ostream<CharT, Traits>
97 {
98 detail::dstream_buf<
99 CharT, Traits, Allocator> buf_;
100
101 public:
102 /** Construct a stream.
103
104 @param os The output stream to wrap.
105 */
106 explicit
107 basic_dstream(std::ostream& os)
108 : std::basic_ostream<CharT, Traits>(&buf_)
109 , buf_(os)
110 {
111 if(os.flags() & std::ios::unitbuf)
112 std::unitbuf(*this);
113 }
114 };
115
116 using dstream = basic_dstream<char>;
117 using dwstream = basic_dstream<wchar_t>;
118
119 #else
120
121 using dstream = std::ostream&;
122 using dwstream = std::wostream&;
123
124 #endif
125
126 } // unit_test
127 } // beast
128 } // boost
129
130 #endif