-
Notifications
You must be signed in to change notification settings - Fork 0
/
multistream.h
70 lines (65 loc) · 1.27 KB
/
multistream.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
#ifndef __MULTISTREAM_H__
#define __MULTISTREAM_H__
/*
* MultiStream
*
* Merge multiple streams into one
*
* by Thomas Mailänder <[email protected]>
* 21.01.2020
*/
#include "Stream.h"
#include "stdio.h"
class MultiStream: public Stream {
public:
MultiStream(Stream* streams[], int len) : streams(streams), len(len) {}
template<class T>
MultiStream& operator<<(const T& x) {
for (int i = 0; i < len; i++)
streams[i] << x;
return *this;
}
template<class T>
MultiStream& operator>>(const T& x) {
for (int i = 0; i < len; i++)
streams[i] >> x;
return *this;
}
int available() {
for (int i = 0; i < len; i++) {
if (streams[i]->available())
return true;
}
return false;
}
size_t write(uint8_t d) {
size_t ret;
for (int i = 0; i < len; i++)
ret = streams[i]->write(d);
return ret;
}
int read() {
for (int i = 0; i < len; i++) {
int val = streams[i]->read();
if (val != EOF)
return val;
}
return EOF;
}
int peek() {
for (int i = 0; i < len; i++) {
int val = streams[i]->peek();
if (val != EOF)
return val;
}
return EOF;
}
void flush() {
for (int i = 0; i < len; i++)
streams[i]->flush();
}
private:
Stream** streams;
int len;
};
#endif