BEEP-8 Helper Lib 1.0.0
Loading...
Searching...
No Matches
pipe.h
Go to the documentation of this file.
1
62#pragma once
63#include <cstddef>
64#include <vector>
65#include <memory>
66#include <b8/type.h>
67namespace Pipe {
68
72class CPipe {
73protected:
74 size_t _pushed_in_bytes = 0;
75private:
76 virtual bool vOnPush( u8 x_ ){
77 (void)x_;
78 return false;
79 }
80public:
87 bool Push(const std::string& str_) {
88 bool failed = false;
89 for( auto it : str_ ){
90 if( ! Push( it ) ){
91 failed = true;
92 }
93 }
94 return failed ? false : true;
95 }
96
103 bool Push(u8 x_) {
104 bool result = vOnPush(x_);
105 if (result) ++_pushed_in_bytes;
106 return result;
107 }
108
109protected:
110 size_t _poped_in_bytes = 0;
111private:
112 virtual bool vOnPop(u8& x_) {
113 (void)x_;
114 return false;
115 }
116public:
122 void SeekPop(size_t offset_ = 0) {
123 _poped_in_bytes = offset_;
124 }
125
132 bool Pop(u8& x_) {
133 const bool result = vOnPop(x_);
134 if (result) ++_poped_in_bytes;
135 return result;
136 }
137
138public:
139 virtual ~CPipe() {}
140};
141
145class CNullPipe : public CPipe {
146public:
147 CNullPipe();
148 virtual ~CNullPipe();
149};
150
154class CMemReaderPipe : public CPipe {
155 const u8* _addr;
156 size_t _bytesize;
157
158 bool vOnPop(u8& x_) override {
159 if (_poped_in_bytes < _bytesize) {
160 x_ = *(_addr + _poped_in_bytes);
161 return true;
162 }
163 return false;
164 }
165
166public:
172 size_t Size() const {
173 return _bytesize;
174 }
175
182 CMemReaderPipe(const u8* addr_, size_t bytesize_)
183 : _addr(addr_),
184 _bytesize(bytesize_) {}
185};
186
190class CMemBufferPipe : public CPipe {
191public:
192 std::vector<u8> _buff;
193
197 void Dump();
198
204 size_t Size() const {
205 return _buff.size();
206 }
207private:
208 bool vOnPush(u8 x_) override {
209 _buff.push_back(x_);
210 return true;
211 }
212
213 bool vOnPop(u8& x_) override {
214 if (_poped_in_bytes < _buff.size()) {
215 x_ = _buff[_poped_in_bytes];
216 return true;
217 }
218 return false;
219 }
220};
221
225class CFilePipe : public CPipe {
226 FILE* _fp = nullptr;
227private:
228 bool vOnPush(u8 x_) override {
229 fputc(x_, _fp);
230 return true;
231 }
232
233 bool vOnPop(u8& x_) override {
234 int c = fgetc(_fp);
235 if (c != EOF) {
236 x_ = c;
237 return true;
238 } else {
239 return false;
240 }
241 }
242public:
248 CFilePipe(FILE* fp_) {
249 _fp = fp_;
250 }
251};
252
261extern void Move(
262 std::shared_ptr< Pipe::CPipe > pipe_in_,
263 std::shared_ptr< Pipe::CPipe > pipe_out_
264);
265
266} // namespace Pipe
A file pipe class that reads and writes data to a file.
Definition pipe.h:225
CFilePipe(FILE *fp_)
Constructs a file pipe.
Definition pipe.h:248
A memory buffer pipe class that holds data in a buffer.
Definition pipe.h:190
void Dump()
Dumps the contents of the memory buffer.
Definition pipe.cpp:9
size_t Size() const
Gets the size of the memory buffer.
Definition pipe.h:204
A memory reader pipe class that reads data from a memory buffer.
Definition pipe.h:154
CMemReaderPipe(const u8 *addr_, size_t bytesize_)
Constructs a memory reader pipe.
Definition pipe.h:182
size_t Size() const
Gets the size of the memory buffer.
Definition pipe.h:172
A null pipe class that does nothing.
Definition pipe.h:145
Base class for all pipe implementations.
Definition pipe.h:72
void SeekPop(size_t offset_=0)
Sets the position for the next Pop operation.
Definition pipe.h:122
bool Push(u8 x_)
Pushes a byte of data into the pipe.
Definition pipe.h:103
bool Pop(u8 &x_)
Pops a byte of data from the pipe.
Definition pipe.h:132
bool Push(const std::string &str_)
Pushes a string of data into the pipe.
Definition pipe.h:87