forked from ctlbox/controlbox-cpp
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCompositeDataStream.h
139 lines (116 loc) · 3.7 KB
/
CompositeDataStream.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright 2014-2015 Matthew McGowan.
*
* This file is part of Nice Firmware.
*
* BrewPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BrewPi. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "DataStream.h"
#include <functional>
#include <iterator>
#include <boost/range/iterator.hpp>
#include <boost/iterator/transform_iterator.hpp>
template <typename Iterator> using DataInRange = boost::iterator_range<Iterator>;
template <typename Iterator> using DataOutRange = boost::iterator_range<Iterator>;
template <typename Iterator> using DataInRangeProvider = std::function<DataInRange<Iterator>(void)>;
template <typename Iterator> using DataOutRangeProvider = std::function<DataOutRange<Iterator>(void)>;
/**
* A composite output stream.
*/
template <typename Iterator>
class CompositeDataOut : public DataOut
{
using Streams = DataOutRangeProvider<Iterator>;
Streams streams;
public:
CompositeDataOut(Streams _streams) : streams(_streams) {}
CompositeDataOut(const CompositeDataOut&) = delete;
CompositeDataOut()=delete;
virtual bool write(uint8_t data) override
{
bool result = true;
for (auto& stream : streams()) {
bool written = stream.write(data);
result = result && written;
}
return result;
}
virtual bool writeBuffer(const void* data, stream_size_t len) override
{
bool result = true;
for (auto& stream : streams()) {
bool written = stream.writeBuffer(data, len);
result = result && written;
}
return result;
}
};
template <typename Iterator>
class CompositeDataIn : public DataIn
{
using Streams = DataInRangeProvider<Iterator>;
Streams streams;
/**
*/
DataIn* nextStream;
void findNext() {
if (!nextStream) {
nextStream = findNextStream();
}
}
/**
* Retrieves the next stream that has a data byte ready now.
* @return The stream that has a data byte ready. If there are still streams open
* but none a ready, NULL is returned. If all streams are closed, this is returned.
*/
DataIn* findNextStream() {
bool open = false;
for (auto& stream : streams()) {
if (stream.hasNext()) {
open = true;
if (stream.peek()>=0) {
return &stream;
}
}
}
return open ? nullptr : this;
}
public:
CompositeDataIn(Streams _streams) : streams(_streams), nextStream(nullptr) {}
CompositeDataIn() = delete;
CompositeDataIn(const CompositeDataIn&) = delete;
virtual bool hasNext() override {
findNext();
return this!=nextStream;
}
virtual uint8_t next() override {
findNext();
if (nextStream==nullptr || nextStream==this)
return -1;
return nextStream->next();
}
virtual uint8_t peek() override {
findNext();
if (nextStream==nullptr || nextStream==this)
return -1;
return nextStream->peek();
}
/**
* Resets the state to look for another stream with data.
*/
void reset() {
nextStream = nullptr;
}
};