-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgstream.h
273 lines (222 loc) · 5.86 KB
/
msgstream.h
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
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// msgstream.h
#ifndef _MSGSTREAM_H
# define _MSGSTREAM_H
# include "misc.h"
# include "stringtool.h"
# include "multithread.h"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// msgstream
/** msgstream.
<p>Before writing to omsgstream, you must acquire lock by calling
<code>acquire()</code>. Then after completion of writing, you
must call <code>release()</code>.</p>
<p>Omsgbuf calls <code>PostMessage(hwnd, messageid, 0,
(LPARAM)omsgbuf)</code> to notify that string is ready to get.
When the window (<code>hwnd</code>) get the message, you can get
the string containd in the omsgbuf by calling
<code>acquireString()</code>. After calling
<code>acquireString()</code>, you must / call releaseString().</p>
*/
template<class T, size_t SIZE = 1024,
class TR = std::char_traits<T>, class A = std::allocator<T> >
class basic_msgbuf : public std::basic_streambuf<T, TR>, public SyncObject
{
public:
typedef std::basic_string<T, TR, A> String; ///
typedef std::basic_streambuf<T, TR> Super; ///
private:
HWND m_hwnd; /** window handle for
notification */
UINT m_messageId; /// messageid for notification
T *m_buf; /// for streambuf
String m_str; /// for notification
CriticalSection m_cs; /// lock
A m_allocator; /// allocator
/** debug level.
if ( m_msgDebugLevel <= m_debugLevel ), message is displayed
*/
int m_debugLevel;
int m_msgDebugLevel; ///
private:
basic_msgbuf(const basic_msgbuf &); /// disable copy constructor
public:
///
basic_msgbuf(UINT i_messageId, HWND i_hwnd = 0)
: m_hwnd(i_hwnd),
m_messageId(i_messageId),
m_buf(m_allocator.allocate(SIZE, 0)),
m_debugLevel(0),
m_msgDebugLevel(0) {
ASSERT(m_buf);
setp(m_buf, m_buf + SIZE);
}
///
~basic_msgbuf() {
sync();
m_allocator.deallocate(m_buf, SIZE);
}
/// attach/detach a window
basic_msgbuf* attach(HWND i_hwnd) {
Acquire a(&m_cs);
ASSERT( !m_hwnd && i_hwnd );
m_hwnd = i_hwnd;
if (!m_str.empty())
PostMessage(m_hwnd, m_messageId, 0, (LPARAM)this);
return this;
}
///
basic_msgbuf* detach() {
Acquire a(&m_cs);
sync();
m_hwnd = 0;
return this;
}
/// get window handle
HWND getHwnd() const {
return m_hwnd;
}
/// is a window attached ?
int is_open() const {
return !!m_hwnd;
}
/// acquire string and release the string
const String &acquireString() {
m_cs.acquire();
return m_str;
}
///
void releaseString() {
m_str.resize(0);
m_cs.release();
}
/// set debug level
void setDebugLevel(int i_debugLevel) {
m_debugLevel = i_debugLevel;
}
///
int getDebugLevel() const {
return m_debugLevel;
}
// for stream
typename Super::int_type overflow(typename Super::int_type i_c = TR::eof()) {
if (sync() == TR::eof()) // sync before new buffer created below
return TR::eof();
if (i_c != TR::eof()) {
*pptr() = TR::to_char_type(i_c);
pbump(1);
sync();
}
return TR::not_eof(i_c); // return something other than EOF if successful
}
// for stream
int sync() {
T *begin = pbase();
T *end = pptr();
T *i;
for (i = begin; i < end; ++ i)
if (_istlead(*i))
++ i;
if (i == end) {
if (m_msgDebugLevel <= m_debugLevel)
m_str += String(begin, end - begin);
setp(m_buf, m_buf + SIZE);
} else { // end < i
if (m_msgDebugLevel <= m_debugLevel)
m_str += String(begin, end - begin - 1);
m_buf[0] = end[-1];
setp(m_buf, m_buf + SIZE);
pbump(1);
}
return TR::not_eof(0);
}
// sync object
/// begin writing
virtual void acquire() {
m_cs.acquire();
}
/// begin writing
virtual void acquire(int i_msgDebugLevel) {
m_cs.acquire();
m_msgDebugLevel = i_msgDebugLevel;
}
/// end writing
virtual void release() {
if (!m_str.empty())
PostMessage(m_hwnd, m_messageId, 0, reinterpret_cast<LPARAM>(this));
m_msgDebugLevel = m_debugLevel;
m_cs.release();
}
};
///
template<class T, size_t SIZE = 1024,
class TR = std::char_traits<T>, class A = std::allocator<T> >
class basic_omsgstream : public std::basic_ostream<T, TR>, public SyncObject
{
public:
typedef std::basic_ostream<T, TR> Super; ///
typedef basic_msgbuf<T, SIZE, TR, A> StreamBuf; ///
typedef std::basic_string<T, TR, A> String; ///
private:
StreamBuf m_streamBuf; ///
public:
///
explicit basic_omsgstream(UINT i_messageId, HWND i_hwnd = 0)
: Super(&m_streamBuf), m_streamBuf(i_messageId, i_hwnd) {
}
///
virtual ~basic_omsgstream() {
}
///
StreamBuf *rdbuf() const {
return const_cast<StreamBuf *>(&m_streamBuf);
}
/// attach a msg control
void attach(HWND i_hwnd) {
m_streamBuf.attach(i_hwnd);
}
/// detach a msg control
void detach() {
m_streamBuf.detach();
}
/// get window handle of the msg control
HWND getHwnd() const {
return m_streamBuf.getHwnd();
}
/// is the msg control attached ?
int is_open() const {
return m_streamBuf.is_open();
}
/// set debug level
void setDebugLevel(int i_debugLevel) {
m_streamBuf.setDebugLevel(i_debugLevel);
}
///
int getDebugLevel() const {
return m_streamBuf.getDebugLevel();
}
/// acquire string and release the string
const String &acquireString() {
return m_streamBuf.acquireString();
}
///
void releaseString() {
m_streamBuf->releaseString();
}
// sync object
/// begin writing
virtual void acquire() {
m_streamBuf.acquire();
}
/// begin writing
virtual void acquire(int i_msgDebugLevel) {
m_streamBuf.acquire(i_msgDebugLevel);
}
/// end writing
virtual void release() {
m_streamBuf.release();
}
};
///
typedef basic_omsgstream<_TCHAR> tomsgstream;
#endif // !_MSGSTREAM_H