Debian Codegraph @

LspCpp/include/LibLsp/JsonRpc/stream.h

raw
1 #pragma once
2 #include <mutex>
3 #include <string>
4 namespace lsp
5 {
6 class stream
7 {
8 public:
9 virtual ~stream() = default;
10 virtual bool fail() = 0;
11 virtual bool bad() = 0;
12 virtual bool eof() = 0;
13 virtual bool good() = 0;
14 virtual void clear() = 0;
15 virtual std::string what() = 0;
16 virtual bool need_to_clear_the_state()
17 {
18 return false;
19 }
20
21 bool operator!()
22 {
23 return bad();
24 }
25 };
26 class istream : public stream
27 {
28 public:
29 virtual int get() = 0;
30 virtual ~istream() = default;
31 virtual istream& read(char* str, std::streamsize count) = 0;
32 };
33 template<class T>
34 class base_istream : public istream
35 {
36 public:
37 explicit base_istream(T& _t) : _impl(_t)
38 {
39 }
40
41 int get() override
42 {
43 return _impl.get();
44 }
45 bool fail() override
46 {
47 return _impl.fail();
48 }
49 bool bad() override
50 {
51 return _impl.bad();
52 }
53 bool eof() override
54 {
55 return _impl.eof();
56 }
57 bool good() override
58 {
59 return _impl.good();
60 }
61 istream& read(char* str, std::streamsize count) override
62 {
63 _impl.read(str, count);
64 return *this;
65 }
66
67 void clear() override
68 {
69 _impl.clear();
70 }
71 T& _impl;
72 };
73 class ostream : public stream
74 {
75 public:
76 virtual ~ostream() = default;
77
78 virtual ostream& write(std::string const&) = 0;
79 virtual ostream& write(std::streamsize) = 0;
80 virtual ostream& flush() = 0;
81 };
82 template<class T>
83 class base_ostream : public ostream
84 {
85 public:
86 explicit base_ostream(T& _t) : _impl(_t)
87 {
88 }
89
90 bool fail() override
91 {
92 return _impl.fail();
93 }
94 bool good() override
95 {
96 return _impl.good();
97 }
98 bool bad() override
99 {
100 return _impl.bad();
101 }
102 bool eof() override
103 {
104 return _impl.eof();
105 }
106
107 ostream& write(std::string const& c) override
108 {
109 _impl << c;
110 return *this;
111 }
112
113 ostream& write(std::streamsize _s) override
114 {
115
116 _impl << std::to_string(_s);
117 return *this;
118 }
119
120 ostream& flush() override
121 {
122 _impl.flush();
123 return *this;
124 }
125
126 void clear() override
127 {
128 _impl.clear();
129 }
130
131 protected:
132 T& _impl;
133 };
134
135 template<class T>
136 class base_iostream : public istream, public ostream
137 {
138 public:
139 explicit base_iostream(T& _t) : _impl(_t)
140 {
141 }
142
143 int get() override
144 {
145 return _impl.get();
146 }
147 bool fail() override
148 {
149 return _impl.fail();
150 }
151 bool bad() override
152 {
153 return _impl.bad();
154 }
155 bool eof() override
156 {
157 return _impl.eof();
158 }
159 bool good() override
160 {
161 return _impl.good();
162 }
163 istream& read(char* str, std::streamsize count) override
164 {
165 _impl.read(str, count);
166 return *this;
167 }
168 ostream& write(std::string const& c) override
169 {
170 _impl << c;
171 return *this;
172 }
173
174 ostream& write(std::streamsize _s) override
175 {
176 _impl << std::to_string(_s);
177 return *this;
178 }
179
180 ostream& flush() override
181 {
182 _impl.flush();
183 return *this;
184 }
185
186 void clear() override
187 {
188 _impl.clear();
189 }
190
191 protected:
192 T& _impl;
193 };
194 } // namespace lsp