Tool Libs
UMIT-TIROL Institute of Automation and Control Engineering library collection
 
Loading...
Searching...
No Matches
bufferutils.h
Go to the documentation of this file.
1
5#pragma once
6
24struct SplitPush : Sink<Buffer<uint8_t>> {
25 Sink<Buffer<uint8_t>> &left, &right;
26 bool full() override {
27 return left.full() || right.full();
28 }
29 void push(Buffer<uint8_t> &&in) override {
30 left.push(in);
31 right.push(in);
32 }
34 SplitPush(Sink<Buffer<uint8_t>> &left, Sink<Buffer<uint8_t>> &right) : left(left), right(right) {}
35};
36
58struct SplitPull {
59 struct splitter : Source<Buffer<uint8_t>>, Sink<Buffer<uint8_t>> {
63 bool empty() override {
64 while (!q.full() && !from.empty()) {
65 auto b = from.pop();
66 other->push(b);
67 q.push(std::move(b));
68 }
69 return q.empty();
70 }
71 Buffer<uint8_t> pop() override {
72 return q.pop();
73 }
74 bool full() override {
75 return q.full();
76 }
77 void push(Buffer<uint8_t> &&t) override {
78 q.push(std::move(t));
79 }
80 splitter(Source<Buffer<uint8_t>> &from) : from(from) { }
81 };
83 splitter a;
85 splitter b;
88 a.other = &b;
89 b.other = &a;
90 }
91};
92
110struct Tee : Source<Buffer<uint8_t>> {
115 bool empty() override {
116 while (!q.full() && !from.empty()) {
117 auto b = from.pop();
118 q.push(b);
119 to.trypush(std::move(b));
120 }
121 return q.empty();
122 }
124 Buffer<uint8_t> pop() override {
125 return q.pop();
126 }
129 : from(from), to(to) { }
130};
131
136struct Hexify : Sink<Buffer<uint8_t>> {
137 static constexpr uint8_t map[]="0123456789abcdef";
138 static constexpr size_t blen = 512*3;
140 Buffer<uint8_t> work = blen;
141 bool full() override {
142 return down.full();
143 }
144 void push(Buffer<uint8_t> &&in) override {
145 for (auto b: in) {
146 work.append({'\\', map[b >> 4], map[b & 0xf]});
147 }
148 down.push(std::move(work));
149 work = blen;
150 }
151 Hexify(Sink<Buffer<uint8_t>> &down) : down(down) {}
152};
simple Buffer backed queue implementation
Definition queue.h:12
T pop() override
remove front of queue and return it
Definition queue.h:65
bool empty() override
check if queue is empty
Definition queue.h:83
bool full() override
return true if queue is full
Definition queue.h:87
void push(T &&val) override
move element into queue
Definition queue.h:49
dynamically allocated, but fixed-size buffer template
Definition buffer.h:18
Buffer & append(T b)
simple append single item
Definition buffer.h:44
convert Sink stream into printable hex
Definition bufferutils.h:136
void push(Buffer< uint8_t > &&in) override
move semantics does not check for space. guard by using 'if (!full) { ... }'
Definition bufferutils.h:144
bool full() override
check if sink is full
Definition bufferutils.h:141
generic object sink, i.e.
Definition streams.h:15
virtual bool full()=0
check if sink is full
void trypush(T &&t)
use this if you don't care if it's going to be successful. data gets discarded if sink is full.
Definition streams.h:28
virtual void push(const T &t)
copy semantics does not check for space. guard by using 'if (!full) { ... }'
Definition streams.h:20
generic object source, i.e.
Definition streams.h:40
virtual T pop()=0
pull object from source does not check for data. guard by using 'if (!empty) { ......
virtual bool empty()=0
check if source is empty
stream splitter
Definition bufferutils.h:58
splitter b
split data Source
Definition bufferutils.h:85
splitter a
split data Source
Definition bufferutils.h:83
SplitPull(Source< Buffer< uint8_t > > &f)
set underlying data Source
Definition bufferutils.h:87
stream splitter
Definition bufferutils.h:24
void push(Buffer< uint8_t > &&in) override
move semantics does not check for space. guard by using 'if (!full) { ... }'
Definition bufferutils.h:29
SplitPush(Sink< Buffer< uint8_t > > &left, Sink< Buffer< uint8_t > > &right)
set sinks for splitter
Definition bufferutils.h:34
bool full() override
check if sink is full
Definition bufferutils.h:26
stream splitter
Definition bufferutils.h:110
bool empty() override
check source and push available data to sink
Definition bufferutils.h:115
Buffer< uint8_t > pop() override
get available data
Definition bufferutils.h:124
Tee(Source< Buffer< uint8_t > > &from, Sink< Buffer< uint8_t > > &to)
set underlying source and sink
Definition bufferutils.h:128