-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.h
72 lines (53 loc) · 1.56 KB
/
stream.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
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
class Stream
{
static thread_local std::ostringstream sbuf; // bot.cpp
public:
IRCBOT_OVERLOAD(flush) // Stream is terminated and sent
auto &get_sbuf() const { return sbuf; }
auto has_sbuf() const { return !get_sbuf().str().empty(); }
auto get_str() const { return get_sbuf().str(); }
protected:
auto &get_sbuf() { return sbuf; }
public:
static void clear(); // clear all in sbuf
virtual Stream &operator<<(const flush_t) = 0;
Stream &operator()(const std::string &str); // flush automatically
Stream &operator()(); // flush automatically
template<class T> Stream &operator<<(const T &t); // Append data to sbuf stream
Stream();
virtual ~Stream() = default;
};
inline
Stream::Stream(void)
{
sbuf.exceptions(std::ios_base::badbit|std::ios_base::failbit);
}
template<class T>
Stream &Stream::operator<<(const T &t)
{
sbuf << t;
return *this;
}
inline
Stream &Stream::operator()()
{
return operator<<(flush);
}
inline
Stream &Stream::operator()(const std::string &str)
{
operator<<(str);
return operator<<(flush);
}
inline
void Stream::clear()
{
sbuf.clear();
sbuf.str(std::string{});
}