]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-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 | ||
8 | #ifndef BEAST_UNIT_TEST_DSTREAM_HPP | |
9 | #define BEAST_UNIT_TEST_DSTREAM_HPP | |
10 | ||
11 | #include <ios> | |
12 | #include <memory> | |
13 | #include <ostream> | |
14 | #include <streambuf> | |
15 | #include <string> | |
16 | ||
17 | #ifdef _MSC_VER | |
18 | # ifndef NOMINMAX | |
19 | # define NOMINMAX 1 | |
20 | # endif | |
21 | # ifndef WIN32_LEAN_AND_MEAN | |
22 | # define WIN32_LEAN_AND_MEAN | |
23 | # endif | |
24 | # include <windows.h> | |
25 | # undef WIN32_LEAN_AND_MEAN | |
26 | # undef NOMINMAX | |
27 | #endif | |
28 | ||
29 | namespace beast { | |
30 | namespace unit_test { | |
31 | ||
32 | #ifdef _MSC_VER | |
33 | ||
34 | namespace detail { | |
35 | ||
36 | template<class CharT, class Traits, class Allocator> | |
37 | class dstream_buf | |
38 | : public std::basic_stringbuf<CharT, Traits, Allocator> | |
39 | { | |
40 | using ostream = std::basic_ostream<CharT, Traits>; | |
41 | ||
42 | bool dbg_; | |
43 | ostream& os_; | |
44 | ||
45 | template<class T> | |
46 | void write(T const*) = delete; | |
47 | ||
48 | void write(char const* s) | |
49 | { | |
50 | if(dbg_) | |
51 | OutputDebugStringA(s); | |
52 | os_ << s; | |
53 | } | |
54 | ||
55 | void write(wchar_t const* s) | |
56 | { | |
57 | if(dbg_) | |
58 | OutputDebugStringW(s); | |
59 | os_ << s; | |
60 | } | |
61 | ||
62 | public: | |
63 | explicit | |
64 | dstream_buf(ostream& os) | |
65 | : os_(os) | |
66 | , dbg_(IsDebuggerPresent() != FALSE) | |
67 | { | |
68 | } | |
69 | ||
70 | ~dstream_buf() | |
71 | { | |
72 | sync(); | |
73 | } | |
74 | ||
75 | int | |
76 | sync() override | |
77 | { | |
78 | write(this->str().c_str()); | |
79 | this->str(""); | |
80 | return 0; | |
81 | } | |
82 | }; | |
83 | ||
84 | } // detail | |
85 | ||
86 | /** std::ostream with Visual Studio IDE redirection. | |
87 | ||
88 | Instances of this stream wrap a specified `std::ostream` | |
89 | (such as `std::cout` or `std::cerr`). If the IDE debugger | |
90 | is attached when the stream is created, output will be | |
91 | additionally copied to the Visual Studio Output window. | |
92 | */ | |
93 | template< | |
94 | class CharT, | |
95 | class Traits = std::char_traits<CharT>, | |
96 | class Allocator = std::allocator<CharT> | |
97 | > | |
98 | class basic_dstream | |
99 | : public std::basic_ostream<CharT, Traits> | |
100 | { | |
101 | detail::dstream_buf< | |
102 | CharT, Traits, Allocator> buf_; | |
103 | ||
104 | public: | |
105 | /** Construct a stream. | |
106 | ||
107 | @param os The output stream to wrap. | |
108 | */ | |
109 | explicit | |
110 | basic_dstream(std::ostream& os) | |
111 | : std::basic_ostream<CharT, Traits>(&buf_) | |
112 | , buf_(os) | |
113 | { | |
114 | if(os.flags() & std::ios::unitbuf) | |
115 | std::unitbuf(*this); | |
116 | } | |
117 | }; | |
118 | ||
119 | using dstream = basic_dstream<char>; | |
120 | using dwstream = basic_dstream<wchar_t>; | |
121 | ||
122 | #else | |
123 | ||
124 | using dstream = std::ostream&; | |
125 | using dwstream = std::wostream&; | |
126 | ||
127 | #endif | |
128 | ||
129 | } // unit_test | |
130 | } // beast | |
131 | ||
132 | #endif |