-
Notifications
You must be signed in to change notification settings - Fork 0
/
dualstream.h
60 lines (54 loc) · 1.03 KB
/
dualstream.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
#ifndef __DUALSTREAM_H__
#define __DUALSTREAM_H__
/*
* DualStream
*
* Merge two streams into one
*
* by Thomas Mailänder <[email protected]>
* 21.01.2020
*/
#include "Stream.h"
#include "stdio.h"
class DualStream: public Stream {
public:
DualStream(Stream& s1, Stream& s2) : s1(s1), s2(s2) {}
template<class T>
DualStream& operator<<(const T& x) {
s1 << x;
s2 << x;
return *this;
}
template<class T>
DualStream& operator>>(const T& x) {
s1 >> x;
s2 >> x;
return *this;
}
int available() { return s1.available() || s2.available(); }
size_t write(uint8_t d) {
size_t ret;
ret |= s1.write(d);
ret |= s2.write(d);
return ret;
}
int read() {
int val = s1.read();
if (val == EOF)
val = s2.read();
return val;
}
int peek() {
int val = s1.peek();
if (val == EOF)
val = s2.peek();
return val;
}
void flush() {
s1.flush();
s2.flush();
}
private:
Stream &s1, &s2;
};
#endif