StormByte C++ Library: Buffer module 0.0.9999
StormByte-Buffer is a StormByte library module for handling buffers
Loading...
Searching...
No Matches
fifo.hxx
1#pragma once
2
3#include <StormByte/buffer/generic.hxx>
4#include <StormByte/buffer/typedefs.hxx>
5#include <StormByte/string.hxx>
6
7#include <vector>
8#include <span>
9#include <string>
10#include <sstream>
11#include <utility>
12
20namespace StormByte::Buffer {
40 class STORMBYTE_BUFFER_PUBLIC FIFO: public ReadWrite {
41 public:
45 FIFO() noexcept = default;
46
51 inline FIFO(const DataType& data) noexcept: ReadWrite(data), m_position_offset(0) {}
52
57 inline FIFO(DataType&& data) noexcept: ReadWrite(std::move(data)), m_position_offset(0) {}
58
67 template<std::ranges::input_range R>
68 requires (!std::is_class_v<std::remove_cv_t<std::ranges::range_value_t<R>>>) &&
69 requires(std::ranges::range_value_t<R> v) { static_cast<std::byte>(v); } &&
70 (!std::same_as<std::remove_cvref_t<R>, DataType>)
71 inline FIFO(const R& r) noexcept: ReadWrite(DataConvert(r)), m_position_offset(0) {}
72
78 template<std::ranges::input_range Rr>
79 requires (!std::is_class_v<std::remove_cv_t<std::ranges::range_value_t<Rr>>>) &&
80 requires(std::ranges::range_value_t<Rr> v) { static_cast<std::byte>(v); }
81 inline FIFO(Rr&& r) noexcept: ReadWrite(DataConvert(std::forward<Rr>(r))), m_position_offset(0) {}
82
86 inline FIFO(std::string_view sv) noexcept: ReadWrite(DataConvert(sv)), m_position_offset(0) {}
87
91 inline FIFO(const char* s) noexcept: FIFO(s ? std::string_view(s) : std::string_view()) {}
92
97 FIFO(const FIFO& other) noexcept;
98
103 FIFO(FIFO&& other) noexcept;
104
108 virtual ~FIFO() noexcept = default;
109
115 FIFO& operator=(const FIFO& other);
116
122 FIFO& operator=(FIFO&& other) noexcept;
123
131 inline bool operator==(const FIFO& other) const noexcept {
132 return m_buffer == other.m_buffer && m_position_offset == other.m_position_offset;
133 }
134
140 inline bool operator!=(const FIFO& other) const noexcept {
141 return !(*this == other);
142 }
143
149 inline virtual std::size_t AvailableBytes() const noexcept {
150 const std::size_t current_size = m_buffer.size();
151 return (m_position_offset <= current_size) ? (current_size - m_position_offset) : 0;
152 }
153
157 virtual void Clean() noexcept override;
158
165 inline virtual void Clear() noexcept override {
166 m_buffer.clear();
167 m_position_offset = 0;
168 }
169
174 inline virtual const DataType& Data() const noexcept override {
175 return ReadOnly::Data();
176 }
177
184 virtual bool Drop(const std::size_t& count) noexcept override;
185
193 inline virtual bool Empty() const noexcept override {
194 return m_buffer.empty();
195 }
196
203 inline virtual bool EoF() const noexcept override {
204 return AvailableBytes() == 0;
205 }
206
213 inline bool Extract(const std::size_t& count, DataType& outBuffer) noexcept override {
214 return const_cast<FIFO*>(this)->ReadInternal(count, outBuffer, Operation::Extract);
215 }
216
223 inline bool Extract(const std::size_t& count, WriteOnly& outBuffer) noexcept override {
224 return const_cast<FIFO*>(this)->ReadInternal(count, outBuffer, Operation::Extract);
225 }
226
228 using ReadOnly::Extract;
229
234 inline void ExtractUntilEoF(DataType& outBuffer) noexcept override {
235 ReadUntilEoFInternal(outBuffer, Operation::Extract);
236 }
237
242 inline void ExtractUntilEoF(WriteOnly& outBuffer) noexcept override {
243 ReadUntilEoFInternal(outBuffer, Operation::Extract);
244 }
245
265 virtual std::string HexDump(const std::size_t& collumns = 16, const std::size_t& byte_limit = 0) const noexcept;
266
271 inline virtual bool IsReadable() const noexcept override {
272 return true;
273 }
274
279 inline virtual bool IsWritable() const noexcept override {
280 return true;
281 }
282
300 inline bool Peek(const std::size_t& count, DataType& outBuffer) const noexcept override {
301 return const_cast<FIFO*>(this)->ReadInternal(count, outBuffer, Operation::Peek);
302 }
303
321 inline bool Peek(const std::size_t& count, WriteOnly& outBuffer) const noexcept override {
322 return const_cast<FIFO*>(this)->ReadInternal(count, outBuffer, Operation::Peek);
323 }
324
331 inline bool Read(const std::size_t& count, DataType& outBuffer) const noexcept override {
332 return const_cast<FIFO*>(this)->ReadInternal(count, outBuffer, Operation::Read);
333 }
334
341 inline bool Read(const std::size_t& count, WriteOnly& outBuffer) const noexcept override {
342 return const_cast<FIFO*>(this)->ReadInternal(count, outBuffer, Operation::Read);
343 }
344
346 using ReadOnly::Read;
347
352 inline void ReadUntilEoF(DataType& outBuffer) const noexcept override {
353 const_cast<FIFO*>(this)->ReadUntilEoFInternal(outBuffer, Operation::Read);
354 }
355
361 inline void ReadUntilEoF(WriteOnly& outBuffer) const noexcept override {
362 const_cast<FIFO*>(this)->ReadUntilEoFInternal(outBuffer, Operation::Read);
363 }
364
374 virtual void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept override;
375
381 inline virtual std::size_t Size() const noexcept override {
382 return m_buffer.size();
383 }
384
394 inline bool Write(const std::size_t& count, const DataType& data) noexcept override {
395 return WriteInternal(count, data);
396 }
397
407 inline bool Write(const std::size_t& count, DataType&& data) noexcept override {
408 return WriteInternal(count, std::move(data));
409 }
410
420 inline bool Write(const std::size_t& count, const ReadOnly& data) noexcept override {
421 return WriteInternal(count, data);
422 }
423
433 inline bool Write(const std::size_t& count, ReadOnly&& data) noexcept override {
434 return WriteInternal(count, std::move(data));
435 }
436
438 using WriteOnly::Write;
439
440 protected:
446 mutable std::size_t m_position_offset {0};
447
451 enum class Operation {
452 Extract,
453 Read,
454 Peek
455 };
456
468 static std::string FormatHexLines(std::span<const std::byte>& data, std::size_t start_offset, std::size_t collumns) noexcept;
469
474 virtual std::ostringstream HexDumpHeader() const noexcept;
475
485 virtual bool ReadInternal(const std::size_t& count, DataType& outBuffer, const Operation& flag) noexcept;
495 virtual bool ReadInternal(const std::size_t& count, WriteOnly& outBuffer, const Operation& flag) noexcept;
496
502 virtual void ReadUntilEoFInternal(DataType& outBuffer, const Operation& flag) noexcept;
503
510 virtual void ReadUntilEoFInternal(WriteOnly& outBuffer, const Operation& flag) noexcept;
511
519 virtual bool WriteInternal(const std::size_t& count, const DataType& src) noexcept;
520
528 virtual bool WriteInternal(const std::size_t& count, DataType&& src) noexcept;
529
536 virtual bool WriteInternal(const std::size_t& count, const ReadOnly& src) noexcept;
537
544 virtual bool WriteInternal(const std::size_t& count, ReadOnly&& src) noexcept;
545 };
546}
Byte-oriented FIFO buffer with grow-on-demand.
Definition fifo.hxx:40
void ExtractUntilEoF(WriteOnly &outBuffer) noexcept override
Read all bytes until end-of-file into a WriteOnly buffer.
Definition fifo.hxx:242
bool Read(const std::size_t &count, DataType &outBuffer) const noexcept override
Non destructive read that removes data from the buffer into an existing vector.
Definition fifo.hxx:331
virtual bool Empty() const noexcept override
Check if the buffer is empty.
Definition fifo.hxx:193
virtual std::ostringstream HexDumpHeader() const noexcept
Produce a hexdump header with size and read position.
virtual bool IsWritable() const noexcept override
Check if the buffer is writable.
Definition fifo.hxx:279
FIFO(FIFO &&other) noexcept
Move construct, preserving buffer state and initial capacity.
virtual bool Drop(const std::size_t &count) noexcept override
Drop bytes in the buffer and updates read position.
FIFO(std::string_view sv) noexcept
Construct FIFO from a string view (does not include terminating NUL).
Definition fifo.hxx:86
FIFO(const FIFO &other) noexcept
Copy construct, preserving buffer state and initial capacity.
bool Write(const std::size_t &count, DataType &&data) noexcept override
Move bytes from a vector to the buffer.
Definition fifo.hxx:407
void ReadUntilEoF(WriteOnly &outBuffer) const noexcept override
Read all bytes until end-of-file into a WriteOnly buffer.
Definition fifo.hxx:361
static std::string FormatHexLines(std::span< const std::byte > &data, std::size_t start_offset, std::size_t collumns) noexcept
Produce a hexdump of the given data span.
virtual std::size_t Size() const noexcept override
Get the current number of bytes stored in the buffer.
Definition fifo.hxx:381
virtual std::string HexDump(const std::size_t &collumns=16, const std::size_t &byte_limit=0) const noexcept
Produce a hexdump of the unread contents starting at the current read position.
virtual const DataType & Data() const noexcept override
Access the internal data buffer.
Definition fifo.hxx:174
FIFO() noexcept=default
Construct FIFO.
virtual bool EoF() const noexcept override
Check if the reader has reached end-of-file.
Definition fifo.hxx:203
bool Write(const std::size_t &count, ReadOnly &&data) noexcept override
Move bytes from a vector to the buffer.
Definition fifo.hxx:433
bool Write(const std::size_t &count, const ReadOnly &data) noexcept override
Write bytes from a vector to the buffer.
Definition fifo.hxx:420
virtual std::size_t AvailableBytes() const noexcept
Get the number of bytes available for reading.
Definition fifo.hxx:149
virtual ~FIFO() noexcept=default
Virtual destructor.
void ReadUntilEoF(DataType &outBuffer) const noexcept override
Read all bytes until end-of-file into an existing buffer.
Definition fifo.hxx:352
Operation
Enumeration of read operation types.
Definition fifo.hxx:451
void ExtractUntilEoF(DataType &outBuffer) noexcept override
Read all bytes until end-of-file into an existing buffer.
Definition fifo.hxx:234
bool operator!=(const FIFO &other) const noexcept
Inequality comparison.
Definition fifo.hxx:140
bool Write(const std::size_t &count, const DataType &data) noexcept override
Write bytes from a vector to the buffer.
Definition fifo.hxx:394
bool Read(const std::size_t &count, WriteOnly &outBuffer) const noexcept override
Destructive read that removes data from the buffer into a vector.
Definition fifo.hxx:341
bool Peek(const std::size_t &count, WriteOnly &outBuffer) const noexcept override
Non-destructive peek at buffer data without advancing read position.
Definition fifo.hxx:321
bool Extract(const std::size_t &count, WriteOnly &outBuffer) noexcept override
Destructive read that removes data from the buffer into a FIFO.
Definition fifo.hxx:223
virtual void Seek(const std::ptrdiff_t &offset, const Position &mode) const noexcept override
Move the read position for non-destructive reads.
FIFO(const char *s) noexcept
Construct FIFO from a C string pointer (null-terminated).
Definition fifo.hxx:91
FIFO(DataType &&data) noexcept
Construct FIFO with initial data using move semantics.
Definition fifo.hxx:57
FIFO(const R &r) noexcept
Construct FIFO from an input range.
Definition fifo.hxx:71
bool Extract(const std::size_t &count, DataType &outBuffer) noexcept override
Destructive read that removes data from the buffer into an existing vector.
Definition fifo.hxx:213
bool Peek(const std::size_t &count, DataType &outBuffer) const noexcept override
Non-destructive peek at buffer data without advancing read position.
Definition fifo.hxx:300
virtual void Clean() noexcept override
Clean buffer data (from start to readposition)
FIFO(Rr &&r) noexcept
Construct FIFO from an rvalue range (moves when DataType rvalue).
Definition fifo.hxx:81
DataType m_buffer
Internal buffer storage.
Definition generic.hxx:73
Generic class providing a buffer that can be read but not written to.
Definition generic.hxx:146
Generic class providing a buffer that can be both read from and written to.
Definition generic.hxx:765
Generic class providing a buffer that can be written to but not read from.
Definition generic.hxx:408