This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmsgstream.h
377 lines (323 loc) · 7.55 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 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>
*/
using namespace std;
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:
#if defined(WIN32)
HWND m_hwnd; /** window handle for
notification */
#elif defined(__linux__) || defined(__APPLE__)
// TODO:ƒtƒ@ƒCƒ‹o—Í
int m_file; // file discriptor
#endif
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(
#if defined(WIN32)
UINT i_messageId,
HWND i_hwnd = 0
#elif defined(__linux__) || defined(__APPLE__)
int i_file
#endif
)
:
#if defined(WIN32)
m_hwnd(i_hwnd),
m_messageId(i_messageId),
#elif defined(__linux__) || defined(__APPLE__)
m_file(i_file),
#endif
m_buf(m_allocator.allocate(SIZE, 0)),
m_debugLevel(0),
m_msgDebugLevel(0)
{
ASSERT(m_buf);
this->setp(m_buf, m_buf + SIZE);
}
///
~basic_msgbuf()
{
sync();
m_allocator.deallocate(m_buf, SIZE);
}
/// attach/detach a window
#if defined(WIN32)
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;
}
#else //linux and mac // TODO:
basic_msgbuf* attach(int i_file)
{
Acquire a(&m_cs);
ASSERT( !m_file && i_file );
m_file = i_file;
if (!m_str.empty())
{
// fputs(m_file, m_str.c_str());
write(m_file, m_str.c_str(), m_str.size() + sizeof(_T('\n')));
releaseString();
}
return this;
}
///
basic_msgbuf* detach()
{
Acquire a(&m_cs);
m_file = -1;
return this;
}
#endif
#if defined(WIN32)
/// get window handle
HWND getHwnd() const { return m_hwnd; }
/// is a window attached ?
int is_open() const { return !!m_hwnd; }
#elif defined(__linux__) || defined(__APPLE__)
/// get file descriptor
int getFile() const { return m_file; }
/// is a window attached ?
int is_open() const { return m_file != -1; }
#endif
/// 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())
{
*basic_streambuf<T, TR>::pptr() = TR::to_char_type(i_c);
basic_streambuf<T, TR>::pbump(1);
sync();
}
return TR::not_eof(i_c); // return something other than EOF if successful
}
// for stream
int sync()
{
T *begin = basic_streambuf<T, TR>::pbase();
T *end = basic_streambuf<T, TR>::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);
this->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];
this->setp(m_buf, m_buf + SIZE);
basic_streambuf<T, TR>::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 defined(WIN32)
if (!m_str.empty())
PostMessage(m_hwnd, m_messageId, 0, reinterpret_cast<LPARAM>(this));
#elif defined(__linux__) || defined(__APPLE__)
if (!m_str.empty())
{
write(m_file, m_str.c_str(), m_str.size() + sizeof(_T('\n')));
releaseString();
}
#endif
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(
#if defined(WIN32)
UINT i_messageId
, HWND i_hwnd = 0
#endif
)
: Super(&m_streamBuf), m_streamBuf(
#if defined(WIN32)
i_messageId
, i_hwnd
#elif defined(__linux__) || defined(__APPLE__)
2 // stdout
#endif
)
{
}
///
virtual ~basic_omsgstream()
{
}
///
StreamBuf *rdbuf() const
{
return const_cast<StreamBuf *>(&m_streamBuf);
}
#if defined(WIN32)
/// attach a msg control
void attach(HWND i_hwnd)
{
m_streamBuf.attach(i_hwnd);
}
#else
void attach(int i_file)
{
m_streamBuf.attach(i_file);
}
#endif
/// detach a msg control
void detach()
{
m_streamBuf.detach();
}
#if defined(WIN32)
/// 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();
}
#endif
/// 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