StormByte C++ Library: Buffer module 1.2.0
StormByte-Buffer is the buffer module of the StormByte C++ suite.
Loading...
Searching...
No Matches
fifo.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Buffer.
5 *
6 * StormByte-Buffer is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 3
8 * or later, as published by the Free Software Foundation.
9 *
10 * StormByte-Buffer is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with StormByte-Buffer. If not, see
17 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
18 */
19
20#pragma once
21
24#include <StormByte/string.hxx>
25
26#include <vector>
27#include <span>
28#include <string>
29#include <sstream>
30#include <utility>
31
36namespace StormByte::Buffer {
59 public:
68 FIFO() noexcept = default;
69
74 inline FIFO(const DataType& data) noexcept
75 : m_buffer(data), m_position_offset(0) {}
76
81 inline FIFO(DataType&& data) noexcept
82 : m_buffer(std::move(data)), m_position_offset(0) {}
83
90 template<Type::ByteInputRange R>
91 requires (!Type::SameAs<R, DataType>)
92 inline FIFO(const R& r) noexcept
93 : m_buffer(DataConvert(r)), m_position_offset(0) {}
94
100 template<Type::ByteInputRange Rr>
101 inline FIFO(Rr&& r) noexcept
102 : m_buffer(DataConvert(std::forward<Rr>(r))), m_position_offset(0) {}
103
108 inline FIFO(std::string_view sv) noexcept
109 : m_buffer(DataConvert(sv)), m_position_offset(0) {}
110
115 inline FIFO(const char* s) noexcept
116 : FIFO(s ? std::string_view(s) : std::string_view()) {}
117
122 FIFO(const FIFO& other) noexcept;
123
128 FIFO(FIFO&& other) noexcept;
129
133 virtual ~FIFO() noexcept;
134
140 FIFO& operator=(const FIFO& other);
141
147 FIFO& operator=(FIFO&& other) noexcept;
148
161 inline bool operator==(const FIFO& other) const noexcept {
162 return m_buffer == other.m_buffer &&
163 m_position_offset == other.m_position_offset &&
164 m_closed == other.m_closed &&
165 m_error == other.m_error;
166 }
167
173 inline bool operator!=(const FIFO& other) const noexcept {
174 return !(*this == other);
175 }
176
189 virtual std::size_t AvailableBytes() const noexcept;
190
195 virtual const DataType& Data() const noexcept override;
196
204 virtual bool Empty() const noexcept override;
205
210 virtual bool EoF() const noexcept override;
211
216 virtual bool IsReadable() const noexcept override;
217
222 virtual bool IsWritable() const noexcept override;
223
229 virtual std::size_t Size() const noexcept override;
230
241 virtual void Clean() noexcept override;
242
248 virtual void Clear() noexcept override;
249
255 void Close() noexcept override;
256
263 virtual bool Drop(const std::size_t& count) noexcept override;
264
271 virtual void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept override;
272
276 void SetError() noexcept override;
277
291 bool Extract(const std::size_t& count, DataType& outBuffer) noexcept override;
292
299 bool Extract(const std::size_t& count, WriteOnly& outBuffer) noexcept override;
300
302 using ReadOnly::Extract;
303
308 void ExtractUntilEoF(DataType& outBuffer) noexcept override;
309
314 void ExtractUntilEoF(WriteOnly& outBuffer) noexcept override;
315
329 bool Read(const std::size_t& count, DataType& outBuffer) const noexcept override;
330
337 bool Read(const std::size_t& count, WriteOnly& outBuffer) const noexcept override;
338
340 using ReadOnly::Read;
341
346 void ReadUntilEoF(DataType& outBuffer) const noexcept override;
347
352 void ReadUntilEoF(WriteOnly& outBuffer) const noexcept override;
353
367 bool Peek(const std::size_t& count, DataType& outBuffer) const noexcept override;
368
375 bool Peek(const std::size_t& count, WriteOnly& outBuffer) const noexcept override;
376
391 virtual std::string HexDump(const std::size_t& columns = 16,
392 const std::size_t& byte_limit = 0) const noexcept;
393
407 bool Write(const std::size_t& count, const DataType& data) noexcept override;
408
415 bool Write(const std::size_t& count, DataType&& data) noexcept override;
416
423 bool Write(const std::size_t& count, const ReadOnly& data) noexcept override;
424
431 bool Write(const std::size_t& count, ReadOnly&& data) noexcept override;
432
434 using WriteOnly::Write;
435
438 protected:
444 std::size_t AvailableBytesInternal() const noexcept;
445
446 DataType m_buffer;
447 mutable std::size_t m_position_offset {0};
448 bool m_closed {false};
449 bool m_error {false};
450
454 enum class Operation {
455 Extract,
456 Read,
457 Peek
458 };
459
467 static std::string FormatHexLines(std::span<const std::byte>& data,
468 std::size_t start_offset,
469 std::size_t columns) noexcept;
470
475 virtual std::ostringstream HexDumpHeader() const noexcept;
476
489 virtual bool ReadInternal(const std::size_t& count, DataType& outBuffer,
490 const Operation& flag) noexcept;
491
499 virtual bool ReadInternal(const std::size_t& count, WriteOnly& outBuffer,
500 const Operation& flag) noexcept;
501
507 virtual void ReadUntilEoFInternal(DataType& outBuffer, const Operation& flag) noexcept;
508
514 virtual void ReadUntilEoFInternal(WriteOnly& outBuffer, const Operation& flag) noexcept;
515
522 virtual bool WriteInternal(const std::size_t& count, const DataType& src) noexcept;
523
530 virtual bool WriteInternal(const std::size_t& count, DataType&& src) noexcept;
531
538 virtual bool WriteInternal(const std::size_t& count, const ReadOnly& src) noexcept;
539
546 virtual bool WriteInternal(const std::size_t& count, ReadOnly&& src) noexcept;
547
549 };
550}
Byte-oriented FIFO buffer with grow-on-demand and close/error support.
Definition fifo.hxx:58
virtual std::ostringstream HexDumpHeader() const noexcept
Build the hexdump header (size / position / status).
FIFO(FIFO &&other) noexcept
Move construct.
virtual ~FIFO() noexcept
Virtual destructor.
FIFO(std::string_view sv) noexcept
Construct FIFO from a string view (no trailing NUL).
Definition fifo.hxx:108
FIFO(const FIFO &other) noexcept
Copy construct, preserving buffer contents and state.
DataType m_buffer
Owned contiguous byte storage.
Definition fifo.hxx:446
FIFO(const R &r) noexcept
Construct FIFO from an input range (copy / convert).
Definition fifo.hxx:92
static std::string FormatHexLines(std::span< const std::byte > &data, std::size_t start_offset, std::size_t columns) noexcept
Format hex/ASCII lines for a data span.
FIFO() noexcept=default
Default construct an empty FIFO.
FIFO(Rr &&r) noexcept
Construct FIFO from an rvalue range (moves when DataType).
Definition fifo.hxx:101
virtual std::size_t AvailableBytes() const noexcept
Bytes available from the current read position.
Operation
Kind of internal read operation.
Definition fifo.hxx:454
bool operator!=(const FIFO &other) const noexcept
Inequality comparison.
Definition fifo.hxx:173
FIFO(const char *s) noexcept
Construct FIFO from a null-terminated C string.
Definition fifo.hxx:115
FIFO(DataType &&data) noexcept
Construct FIFO with initial data (move).
Definition fifo.hxx:81
Pure interface for a buffer that can be read but not written.
Definition generic.hxx:164
Pure interface combining ReadOnly and WriteOnly.
Definition generic.hxx:762
Pure interface for a buffer that can be written but not read.
Definition generic.hxx:420
Buffer module of the StormByte suite.
std::vector< std::byte > DataType
Primary byte storage type used throughout the buffer API.
Definition typedefs.hxx:62
Position
Forward declaration of the WriteOnly interface.
Definition typedefs.hxx:50
#define STORMBYTE_BUFFER_PUBLIC
Definition visibility.h:28