-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquote.h
101 lines (78 loc) · 1.72 KB
/
quote.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
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
class Quote
{
const char *const &cmd;
public:
using flush_t = Stream::flush_t;
static constexpr flush_t flush = Stream::flush;
auto &get_cmd() const { return cmd; }
bool has_cmd() const { return strnlen(cmd,64); }
protected:
auto &get_cmd() { return cmd; }
public:
// Send to server
Quote &operator<<(const flush_t);
Quote &operator()(const std::string &str); // flush automatically
Quote &operator()(); // flush automatically
// Append to stream
template<class T> Quote &operator<<(const T &t);
Quote(const char *const &cmd = "", const milliseconds &delay = 0ms);
~Quote() noexcept;
};
inline
Quote::Quote(const char *const &cmd,
const milliseconds &delay):
cmd(cmd)
{
auto &sock(get_sock());
sock.set_delay(delay);
if(has_cmd())
sock << cmd << " ";
}
inline
Quote::~Quote()
noexcept
{
auto &sock(get_sock());
if(std::uncaught_exception())
{
sock.clear();
return;
}
if(sock.has_pending())
operator<<(flush);
}
inline
Quote &Quote::operator()()
{
operator<<(flush);
return *this;
}
inline
Quote &Quote::operator()(const std::string &str)
{
operator<<(str);
operator<<(flush);
return *this;
}
template<class T>
Quote &Quote::operator<<(const T &t)
{
auto &sock(get_sock());
if(has_cmd() && !sock.has_pending())
sock << cmd << " ";
sock << t;
return *this;
}
inline
Quote &Quote::operator<<(const flush_t)
{
auto &sock(get_sock());
sock << flush;
return *this;
}