3#include <StormByte/buffer/generic.hxx>
4#include <StormByte/buffer/typedefs.hxx>
5#include <StormByte/string.hxx>
20namespace StormByte::Buffer {
51 inline
FIFO(const DataType& data) noexcept:
ReadWrite(data), m_position_offset(0) {}
57 inline FIFO(DataType&& data)
noexcept:
ReadWrite(std::move(data)), m_position_offset(0) {}
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) {}
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) {}
86 inline FIFO(std::string_view sv)
noexcept:
ReadWrite(DataConvert(sv)), m_position_offset(0) {}
91 inline FIFO(
const char* s)
noexcept:
FIFO(s ? std::string_view(s) : std::string_view()) {}
131 inline
bool operator==(const
FIFO& other) const noexcept {
132 return m_buffer == other.
m_buffer && m_position_offset == other.m_position_offset;
141 return !(*
this == other);
150 const std::size_t current_size = m_buffer.size();
151 return (m_position_offset <= current_size) ? (current_size - m_position_offset) : 0;
157 virtual void Clean() noexcept override;
165 inline virtual
void Clear() noexcept
override {
167 m_position_offset = 0;
174 inline virtual const DataType&
Data() const noexcept
override {
175 return ReadOnly::Data();
184 virtual bool Drop(
const std::size_t& count)
noexcept override;
193 inline virtual bool Empty() const noexcept
override {
194 return m_buffer.empty();
203 inline virtual bool EoF() const noexcept
override {
204 return AvailableBytes() == 0;
213 inline bool Extract(
const std::size_t& count, DataType& outBuffer)
noexcept override {
214 return const_cast<FIFO*
>(
this)->ReadInternal(count, outBuffer, Operation::Extract);
224 return const_cast<FIFO*
>(
this)->ReadInternal(count, outBuffer, Operation::Extract);
228 using ReadOnly::Extract;
235 ReadUntilEoFInternal(outBuffer, Operation::Extract);
243 ReadUntilEoFInternal(outBuffer, Operation::Extract);
265 virtual std::string
HexDump(
const std::size_t& collumns = 16,
const std::size_t& byte_limit = 0) const noexcept;
271 inline virtual
bool IsReadable() const noexcept
override {
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);
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);
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);
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);
346 using ReadOnly::Read;
352 inline void ReadUntilEoF(DataType& outBuffer)
const noexcept override {
353 const_cast<FIFO*
>(
this)->ReadUntilEoFInternal(outBuffer, Operation::Read);
362 const_cast<FIFO*
>(
this)->ReadUntilEoFInternal(outBuffer, Operation::Read);
374 virtual void Seek(
const std::ptrdiff_t& offset,
const Position& mode)
const noexcept override;
381 inline virtual std::size_t
Size() const noexcept
override {
382 return m_buffer.size();
394 inline bool Write(
const std::size_t& count,
const DataType& data)
noexcept override {
395 return WriteInternal(count, data);
407 inline bool Write(
const std::size_t& count, DataType&& data)
noexcept override {
408 return WriteInternal(count, std::move(data));
420 inline bool Write(
const std::size_t& count,
const ReadOnly& data)
noexcept override {
421 return WriteInternal(count, data);
433 inline bool Write(
const std::size_t& count,
ReadOnly&& data)
noexcept override {
434 return WriteInternal(count, std::move(data));
438 using WriteOnly::Write;
446 mutable std::size_t m_position_offset {0};
468 static std::string
FormatHexLines(std::span<const std::byte>& data, std::size_t start_offset, std::size_t collumns)
noexcept;
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;
502 virtual
void ReadUntilEoFInternal(DataType& outBuffer, const
Operation& flag) noexcept;
519 virtual
bool WriteInternal(const std::
size_t& count, const DataType& src) noexcept;
528 virtual
bool WriteInternal(const std::
size_t& count, DataType&& src) noexcept;
536 virtual
bool WriteInternal(const std::
size_t& count, const
ReadOnly& src) noexcept;
544 virtual
bool WriteInternal(const std::
size_t& count,
ReadOnly&& src) noexcept;
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