StormByte-Buffer 2.0.0
C++26 buffer module of the StormByte 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 original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-Buffer source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository (including everything under
22 * thirdparty/, and in particular the bundled StormByte-Logger tree and
23 * the rest of the StormByte suite it vendors), which remains under its own
24 * license.
25 *
26 * Neither license grants any patent rights. Any patent licenses required
27 * to use this software or third-party components must be obtained separately
28 * from the patent holders.
29 *
30 * StormByte-Buffer is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU Lesser General Public License for more details.
34 *
35 * You should have received a copy of the GNU Lesser General Public License
36 * version 3 along with StormByte-Buffer. If not, see
37 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
38 *
39 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
40 */
41
42#pragma once
43
47
48#include <sstream>
49#include <span>
50#include <utility>
51
56namespace StormByte::Buffer {
79 public:
88 FIFO() noexcept = default;
89
94 inline FIFO(const StormByte::BinaryData& data) noexcept
95 : m_buffer(data), m_position_offset(0) {}
96
101 inline FIFO(StormByte::BinaryData&& data) noexcept
102 : m_buffer(std::move(data)), m_position_offset(0) {}
103
110 template<Type::ByteInputRange R>
112 inline FIFO(const R& r) noexcept
113 : m_buffer(DataConvert(r)), m_position_offset(0) {}
114
120 template<Type::ByteInputRange Rr>
121 inline FIFO(Rr&& r) noexcept
122 : m_buffer(DataConvert(std::forward<Rr>(r))), m_position_offset(0) {}
123
128 inline FIFO(std::string_view sv) noexcept
129 : m_buffer(DataConvert(sv)), m_position_offset(0) {}
130
135 inline FIFO(const char* s) noexcept
136 : FIFO(s ? std::string_view(s) : std::string_view()) {}
137
142 FIFO(const FIFO& other) noexcept;
143
148 FIFO(FIFO&& other) noexcept;
149
153 virtual ~FIFO() noexcept;
154
160 FIFO& operator=(const FIFO& other);
161
167 FIFO& operator=(FIFO&& other) noexcept;
168
181 inline bool operator==(const FIFO& other) const noexcept {
182 return m_buffer == other.m_buffer &&
183 m_position_offset == other.m_position_offset &&
184 m_closed == other.m_closed &&
185 m_error == other.m_error;
186 }
187
193 inline bool operator!=(const FIFO& other) const noexcept {
194 return !(*this == other);
195 }
196
209 virtual StormByte::ByteSize Available() const noexcept override;
210
215 virtual const StormByte::BinaryData& Data() const noexcept override;
216
224 virtual bool Empty() const noexcept override;
225
230 virtual bool EoF() const noexcept override;
231
236 virtual bool IsReadable() const noexcept override;
237
242 virtual bool IsWritable() const noexcept override;
243
249 virtual StormByte::ByteSize Size() const noexcept override;
250
261 virtual void Clean() noexcept override;
262
268 virtual void Clear() noexcept override;
269
275 void Close() noexcept override;
276
283 virtual bool Drop(const StormByte::ByteSize& count) noexcept override;
284
291 virtual void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept override;
292
296 void SetError() noexcept override;
297
311 bool Extract(const StormByte::ByteSize& count, StormByte::BinaryData& outBuffer) noexcept override;
312
319 bool Extract(const StormByte::ByteSize& count, WriteOnly& outBuffer) noexcept override;
320
322 using ReadOnly::Extract;
323
328 void ExtractUntilEoF(StormByte::BinaryData& outBuffer) noexcept override;
329
334 void ExtractUntilEoF(WriteOnly& outBuffer) noexcept override;
335
349 bool Read(const StormByte::ByteSize& count, StormByte::BinaryData& outBuffer) const noexcept override;
350
357 bool Read(const StormByte::ByteSize& count, WriteOnly& outBuffer) const noexcept override;
358
360 using ReadOnly::Read;
361
366 void ReadUntilEoF(StormByte::BinaryData& outBuffer) const noexcept override;
367
372 void ReadUntilEoF(WriteOnly& outBuffer) const noexcept override;
373
387 bool Peek(const StormByte::ByteSize& count, StormByte::BinaryData& outBuffer) const noexcept override;
388
395 bool Peek(const StormByte::ByteSize& count, WriteOnly& outBuffer) const noexcept override;
396
411 virtual StormByte::String::String HexDump(const StormByte::ByteSize& columns = 16,
412 const StormByte::ByteSize& byte_limit = 0) const noexcept;
413
427 bool Write(const StormByte::ByteSize& count, const StormByte::BinaryData& data) noexcept override;
428
435 bool Write(const StormByte::ByteSize& count, StormByte::BinaryData&& data) noexcept override;
436
443 bool Write(const StormByte::ByteSize& count, const ReadOnly& data) noexcept override;
444
451 bool Write(const StormByte::ByteSize& count, ReadOnly&& data) noexcept override;
452
454 using WriteOnly::Write;
455
458 protected:
464 StormByte::ByteSize AvailableInternal() const noexcept;
465
467 mutable StormByte::ByteSize m_position_offset {0};
468 bool m_closed {false};
469 bool m_error {false};
470
474 enum class Operation {
475 Extract,
476 Read,
477 Peek
478 };
479
487 static StormByte::String::String FormatHexLines(std::span<const std::byte>& data,
488 StormByte::ByteSize start_offset,
489 StormByte::ByteSize columns) noexcept;
490
495 virtual std::ostringstream HexDumpHeader() const noexcept;
496
509 virtual bool ReadInternal(const StormByte::ByteSize& count, StormByte::BinaryData& outBuffer,
510 const Operation& flag) noexcept;
511
519 virtual bool ReadInternal(const StormByte::ByteSize& count, WriteOnly& outBuffer,
520 const Operation& flag) noexcept;
521
527 virtual void ReadUntilEoFInternal(StormByte::BinaryData& outBuffer, const Operation& flag) noexcept;
528
534 virtual void ReadUntilEoFInternal(WriteOnly& outBuffer, const Operation& flag) noexcept;
535
542 virtual bool WriteInternal(const StormByte::ByteSize& count, const StormByte::BinaryData& src) noexcept;
543
550 virtual bool WriteInternal(const StormByte::ByteSize& count, StormByte::BinaryData&& src) noexcept;
551
558 virtual bool WriteInternal(const StormByte::ByteSize& count, const ReadOnly& src) noexcept;
559
566 virtual bool WriteInternal(const StormByte::ByteSize& count, ReadOnly&& src) noexcept;
567
569 };
570}
Byte-oriented FIFO buffer with grow-on-demand and close/error support.
Definition fifo.hxx:78
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:128
FIFO(const FIFO &other) noexcept
Copy construct, preserving buffer contents and state.
FIFO(StormByte::BinaryData &&data) noexcept
Construct FIFO with initial data (move).
Definition fifo.hxx:101
static StormByte::String::String FormatHexLines(std::span< const std::byte > &data, StormByte::ByteSize start_offset, StormByte::ByteSize columns) noexcept
Format hex/ASCII lines for a data span.
virtual StormByte::ByteSize Available() const noexcept override
Bytes available from the current read position.
FIFO() noexcept=default
Default construct an empty FIFO.
StormByte::BinaryData m_buffer
Owned contiguous byte storage.
Definition fifo.hxx:466
FIFO(Rr &&r) noexcept
Construct FIFO from an rvalue range (moves when StormByte::BinaryData).
Definition fifo.hxx:121
FIFO(const R &r) noexcept
Construct FIFO from an input range (copy / convert).
Definition fifo.hxx:112
Operation
Kind of internal read operation.
Definition fifo.hxx:474
bool operator!=(const FIFO &other) const noexcept
Inequality comparison.
Definition fifo.hxx:193
FIFO(const char *s) noexcept
Construct FIFO from a null-terminated C string.
Definition fifo.hxx:135
Buffer that can be read and not written.
Definition generic.hxx:228
Read and write interface.
Definition generic.hxx:872
Buffer that can be written and not read.
Definition generic.hxx:505
Buffer module of the StormByte suite.
Position
Forward declaration of the WriteOnly interface.
Definition typedefs.hxx:73
Root namespace of the StormByte C++ suite.
STL namespace.
#define STORMBYTE_BUFFER_PUBLIC
Definition visibility.h:54