-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathodstream.hpp
282 lines (257 loc) · 8.9 KB
/
odstream.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/***********************************************************************/
/* */
/* odstream.hpp: Header file for stream class using OutputDebugString */
/* */
/* Copyright (C) 2011,2017 Yak! / Yasutaka ATARASHI */
/* */
/* This software is distributed under the terms of a zlib/libpng */
/* License. */
/* */
/* $Id: 3637c03a3f2adb2e627120e488208f73bb995977 $ */
/* */
/***********************************************************************/
#ifndef ODSTREAM_HPP
#define ODSTREAM_HPP
#include <windows.h>
#include <utility>
#include <memory>
#ifdef YAK_DEBUG_NO_HEADER_ONLY
#include <iosfwd>
#ifdef DEBUG
#ifndef ODSTREAM_NO_INCLUDE_IOSTREAM
#include <iostream>
#endif // ifndef ODSTREAM_NO_INCLUDE_IOSTREAM
#endif // ifdef DEBUG
#else // ifdef YAK_DEBUG_NO_HEADER_ONLY
#include <windows.h>
#include <iostream>
#include <sstream>
#endif // ifdef YAK_DEBUG_NO_HEADER_ONLY
#ifdef DEBUG
#define CALLDEBUG(arg) (void)(arg)
#else /* def DEBUG */
#define CALLDEBUG(arg) (void)0
#endif /* def DEBUG */
#define ODS_IMPL(type, ods, arg) (CALLDEBUG((*yak::debug::ods<type>() arg).flush()))
#define ODS_NOFLUSH_IMPL(type, ods, arg) (CALLDEBUG(*yak::debug::ods<type>() arg))
#define ODS(arg) ODS_IMPL(char, ods, arg)
#define ODS_(arg) ODS_IMPL(char, ods_, arg)
#define ODS_NOFLUSH(arg) ODS_NOFLUSH_IMPL(char, ods, arg)
#define ODS_NOFLUSH_(arg) ODS_NOFLUSH_IMPL(char, ods_, arg)
#define WODS(arg) ODS_IMPL(wchar_t, ods, arg)
#define WODS_(arg) ODS_IMPL(wchar_t, ods_, arg)
#define WODS_NOFLUSH(arg) ODS_NOFLUSH_IMPL(wchar_t, ods, arg)
#define WODS_NOFLUSH_(arg) ODS_NOFLUSH_IMPL(wchar_t, ods_, arg)
namespace yak {
namespace debug_yes {
#ifdef YAK_DEBUG_NO_HEADER_ONLY
// Use the Construct On First Use Idiom, to avoid static initialization order fiasco
namespace detail {
template<typename CharT>
struct traits
{
static void OutputDebugStr(const char* lp);
static const char* empty();
};
template<>
struct traits<wchar_t>
{
static void OutputDebugStr(const wchar_t* lp);
static const wchar_t* empty();
};
extern template struct traits<char>;
extern template struct traits<wchar_t>;
} // namespace detail
template<typename CharT>
struct debug_yes_impl
{
private:
typedef detail::traits<CharT> traits;
class odstringbuf;
static odstringbuf& odsbuf();
public:
static std::basic_ostream<CharT>& ods();
}; // struct debug_yes_impl
extern template struct debug_yes_impl<char>;
extern template struct debug_yes_impl<wchar_t>;
#else
// Use the Construct On First Use Idiom, to avoid static initialization order fiasco
namespace detail {
template<typename CharT>
struct traits
{
static void OutputDebugStr(const char* lp) { ::OutputDebugStringA(lp); }
static const char* empty() {
static const char empty_[] = "";
return empty_;
}
};
template<>
struct traits<wchar_t>
{
static void OutputDebugStr(const wchar_t* lp) { ::OutputDebugStringW(lp); }
static const wchar_t* empty() {
static const wchar_t empty_[] = L"";
return empty_;
}
};
} // namespace detail
template<typename CharT>
struct debug_yes_impl
{
private:
typedef detail::traits<CharT> traits;
class odstringbuf : public std::basic_stringbuf<CharT>
{
protected:
virtual int sync(void) {
traits::OutputDebugStr(this->str().c_str());
this->str(traits::empty());
return 0;
}
};
static odstringbuf& odsbuf() {
static odstringbuf odsbuf_;
return odsbuf_;
}
public:
static std::basic_ostream<CharT>& ods() {
static std::basic_ostream<CharT> ods_(&odsbuf());
return ods_;
}
}; // struct debug_yes_impl
#endif
namespace detail {
struct critical_section : CRITICAL_SECTION
{
critical_section() { InitializeCriticalSection(this); }
~critical_section() { DeleteCriticalSection(this); }
template<typename T>
static LPCRITICAL_SECTION getPtr()
{
static critical_section cs;
return &cs;
}
};
struct cs_deleter
{
private:
LPCRITICAL_SECTION pcs;
public:
cs_deleter(LPCRITICAL_SECTION pcs) : pcs(pcs) {}
template<typename T>
void operator()(T*) { LeaveCriticalSection(pcs); }
};
} // namespace detail
// without lock
template<typename CharT = char>
inline std::basic_ostream<CharT>* ods_() {
return &debug_yes_impl<CharT>::ods();
}
template<typename CharT = char>
inline std::basic_ostream<CharT>* ods_flush_() {
return &debug_yes_impl<CharT>::ods().flush();
}
// with lock
template<typename CharT = char>
inline std::unique_ptr<std::basic_ostream<CharT>, detail::cs_deleter> ods() {
LPCRITICAL_SECTION pcs = detail::critical_section::getPtr<CharT>();
EnterCriticalSection(pcs);
return std::unique_ptr<std::basic_ostream<CharT>, detail::cs_deleter>(ods_<CharT>(), detail::cs_deleter(pcs));
}
template<typename CharT = char>
inline std::unique_ptr<std::basic_ostream<CharT>, detail::cs_deleter> ods_flush() {
LPCRITICAL_SECTION pcs = detail::critical_section::getPtr<CharT>();
EnterCriticalSection(pcs);
auto p = std::unique_ptr<std::basic_ostream<CharT>, detail::cs_deleter>(ods_<CharT>(), detail::cs_deleter(pcs));
p->flush();
return std::move(p);
}
} // namespace debug_yes
namespace debug_no {
#ifdef YAK_DEBUG_NO_HEADER_ONLY
template<typename CharT>
class pseudo_null_stream
{
private:
class nullstreambuf;
static nullstreambuf& nullbuf();
static std::basic_ostream<CharT>& null_stream();
public:
template<typename T>
const pseudo_null_stream& operator << (T) const { return *this; }
const pseudo_null_stream& operator << (std::basic_ostream<CharT>& (*)(std::basic_ostream<CharT>&)) const { return *this; }
const pseudo_null_stream& operator << (std::ios_base& (*)(std::ios_base&)) const { return *this; }
const pseudo_null_stream& operator << (std::basic_ios<CharT>& (*)(std::basic_ios<CharT>&)) const { return *this; }
const pseudo_null_stream& flush(void) const { return *this; }
operator std::basic_ostream<CharT>& () { return null_stream(); }
static pseudo_null_stream& ods();
};
extern template struct pseudo_null_stream<char>;
extern template struct pseudo_null_stream<wchar_t>;
#else // ifdef YAK_DEBUG_NO_HEADER_ONLY
template<typename CharT>
class pseudo_null_stream
{
private:
class nullstreambuf : public std::basic_streambuf<CharT> {};
static nullstreambuf& nullbuf() {
static nullstreambuf nullbuf_;
return nullbuf_;
}
static std::basic_ostream<CharT>& null_stream() {
static std::basic_ostream<CharT> null_stream_(&nullbuf());
return null_stream_;
}
public:
template<typename T>
const pseudo_null_stream& operator << (T) const { return *this; }
const pseudo_null_stream& operator << (std::basic_ostream<CharT>& (*)(std::basic_ostream<CharT>&)) const { return *this; }
const pseudo_null_stream& operator << (std::ios_base& (*)(std::ios_base&)) const { return *this; }
const pseudo_null_stream& operator << (std::basic_ios<CharT>& (*)(std::basic_ios<CharT>&)) const { return *this; }
const pseudo_null_stream& flush(void) const { return *this; }
operator std::basic_ostream<CharT>& () { return null_stream(); }
static pseudo_null_stream& ods() {
static pseudo_null_stream ods_;
return ods_;
}
};
#endif
namespace detail {
template<typename CharT>
struct holder
{
private:
pseudo_null_stream<CharT>& r;
public:
holder(pseudo_null_stream<CharT>& r) : r(r) {}
std::basic_ostream<CharT>* operator->() const { return &static_cast<std::basic_ostream<CharT>&>(r); }
pseudo_null_stream<CharT>& operator*() { return r; }
};
} // namespace detail
template<typename CharT = char>
inline detail::holder<CharT> ods_() {
return detail::holder<CharT>(pseudo_null_stream<CharT>::ods());
}
template<typename CharT = char>
inline detail::holder<CharT> ods_flush_() {
return detail::holder<CharT>(pseudo_null_stream<CharT>::ods());
}
template<typename CharT = char>
inline detail::holder<CharT> ods() {
return detail::holder<CharT>(pseudo_null_stream<CharT>::ods());
}
template<typename CharT = char>
inline detail::holder<CharT> ods_flush() {
return detail::holder<CharT>(pseudo_null_stream<CharT>::ods());
}
} // namespace debug_no
#ifndef YAK_DEBUG_NO_DECLARE_NAMESPACE
#ifdef DEBUG
namespace debug = debug_yes;
#else
namespace debug = debug_no;
#endif
#endif
} // namespace yak
#endif