StormByte-Buffer 4.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
44#include <StormByte/type_traits/safe.hxx>
45
48#include <StormByte/safe/string.hxx>
49
50#include <span>
51#include <sstream>
52#include <string_view>
53#include <utility>
54
59namespace StormByte {
64 namespace Buffer {
87 public:
92
96 FIFO() noexcept = default;
97
102 inline FIFO(const StormByte::Safe::Binary& data) noexcept
103 : m_buffer(data), m_position_offset(0) {}
104
109 inline FIFO(StormByte::Safe::Binary&& data) noexcept
110 : m_buffer(std::move(data)), m_position_offset(0) {}
111
118 template<Type::ByteInputRange R>
120 inline FIFO(const R& r) noexcept
122
128 template<Type::ByteInputRange Rr>
129 inline FIFO(Rr&& r) noexcept
130 : m_buffer(DataConvert(std::forward<Rr>(r))), m_position_offset(0) {}
131
136 inline FIFO(std::string_view sv) noexcept
138
143 inline FIFO(const char* s) noexcept
144 : FIFO(s ? std::string_view(s) : std::string_view()) {}
145
150 FIFO(const FIFO& other) noexcept;
151
156 FIFO(FIFO&& other) noexcept;
157
161 virtual ~FIFO() noexcept;
162
168 FIFO& operator=(const FIFO& other);
169
175 FIFO& operator=(FIFO&& other) noexcept;
176
180
185
191 inline bool operator==(const FIFO& other) const noexcept {
192 return m_buffer == other.m_buffer &&
193 m_position_offset == other.m_position_offset &&
194 m_closed == other.m_closed &&
195 m_error == other.m_error;
196 }
197
203 inline bool operator!=(const FIFO& other) const noexcept {
204 return !(*this == other);
205 }
206
210
215
221 virtual StormByte::ByteSize Available() const noexcept override;
222
227 virtual const StormByte::Safe::Binary& Data() const noexcept override;
228
235 virtual bool Empty() const noexcept override;
236
241 virtual bool EoF() const noexcept override;
242
247 virtual bool IsReadable() const noexcept override;
248
253 virtual bool IsWritable() const noexcept override;
254
260 virtual StormByte::ByteSize Size() const noexcept override;
261
265
270
274 virtual void Clean() noexcept override;
275
281 virtual void Clear() noexcept override;
282
287 void Close() noexcept override;
288
295 virtual bool Drop(const StormByte::ByteSize& count) noexcept override;
296
303 virtual void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept override;
304
308 void SetError() noexcept override;
309
313
318
325 bool Extract(const StormByte::ByteSize& count, StormByte::Safe::Binary& outBuffer) noexcept override;
326
333 bool Extract(const StormByte::ByteSize& count, WriteOnly& outBuffer) noexcept override;
334
338 using ReadOnly::Extract;
339
344 void ExtractUntilEoF(StormByte::Safe::Binary& outBuffer) noexcept override;
345
350 void ExtractUntilEoF(WriteOnly& outBuffer) noexcept override;
351
355
360
367 bool Read(const StormByte::ByteSize& count, StormByte::Safe::Binary& outBuffer) const noexcept override;
368
375 bool Read(const StormByte::ByteSize& count, WriteOnly& outBuffer) const noexcept override;
376
380 using ReadOnly::Read;
381
386 void ReadUntilEoF(StormByte::Safe::Binary& outBuffer) const noexcept override;
387
392 void ReadUntilEoF(WriteOnly& outBuffer) const noexcept override;
393
397
402
409 bool Peek(const StormByte::ByteSize& count, StormByte::Safe::Binary& outBuffer) const noexcept override;
410
417 bool Peek(const StormByte::ByteSize& count, WriteOnly& outBuffer) const noexcept override;
418
422
427
434 virtual StormByte::Safe::String HexDump(const StormByte::ByteSize& columns = 16,
435 const StormByte::ByteSize& byte_limit = 0) const noexcept;
436
440
445
452 bool Write(const StormByte::ByteSize& count, const StormByte::Safe::Binary& data) noexcept override;
453
460 bool Write(const StormByte::ByteSize& count, StormByte::Safe::Binary&& data) noexcept override;
461
468 bool Write(const StormByte::ByteSize& count, const ReadOnly& data) noexcept override;
469
476 bool Write(const StormByte::ByteSize& count, ReadOnly&& data) noexcept override;
477
481 using WriteOnly::Write;
482
486
487 protected:
494
497 bool m_closed {false};
498 bool m_error {false};
499
503 enum class Operation {
507 };
508
516 static StormByte::Safe::String FormatHexLines(std::span<const std::byte>& data,
517 StormByte::ByteSize start_offset,
518 StormByte::ByteSize columns) noexcept;
519
524 virtual StormByte::Safe::String HexDumpHeader() const noexcept;
525
530
538 virtual bool ReadInternal(const StormByte::ByteSize& count, StormByte::Safe::Binary& outBuffer,
539 const Operation& flag) noexcept;
540
548 virtual bool ReadInternal(const StormByte::ByteSize& count, WriteOnly& outBuffer,
549 const Operation& flag) noexcept;
550
556 virtual void ReadUntilEoFInternal(StormByte::Safe::Binary& outBuffer, const Operation& flag) noexcept;
557
563 virtual void ReadUntilEoFInternal(WriteOnly& outBuffer, const Operation& flag) noexcept;
564
571 virtual bool WriteInternal(const StormByte::ByteSize& count, const StormByte::Safe::Binary& src) noexcept;
572
579 virtual bool WriteInternal(const StormByte::ByteSize& count, StormByte::Safe::Binary&& src) noexcept;
580
587 virtual bool WriteInternal(const StormByte::ByteSize& count, const ReadOnly& src) noexcept;
588
595 virtual bool WriteInternal(const StormByte::ByteSize& count, ReadOnly&& src) noexcept;
596
600 };
601 }
602}
603
virtual bool Empty() const noexcept override
Whether the underlying storage is empty.
StormByte::ByteSize AvailableInternal() const noexcept
Return unread bytes without virtual dispatch.
void ExtractUntilEoF(StormByte::Safe::Binary &outBuffer) noexcept override
Extract until EoF into owned storage.
virtual bool IsWritable() const noexcept override
Whether the buffer accepts writes.
virtual StormByte::Safe::String HexDump(const StormByte::ByteSize &columns=16, const StormByte::ByteSize &byte_limit=0) const noexcept
Hexdump of unread contents from the current read position.
FIFO(FIFO &&other) noexcept
Take a FIFO.
virtual ~FIFO() noexcept
Destroy the FIFO.
void ReadUntilEoF(StormByte::Safe::Binary &outBuffer) const noexcept override
Read until EoF into owned storage.
FIFO(std::string_view sv) noexcept
Construct a FIFO from a string view.
Definition fifo.hxx:136
FIFO(const FIFO &other) noexcept
Copy a FIFO, including contents and state.
virtual void ReadUntilEoFInternal(StormByte::Safe::Binary &outBuffer, const Operation &flag) noexcept
Drain until EoF into owned storage.
virtual StormByte::ByteSize Size() const noexcept override
Total number of bytes stored, including the already-read prefix.
static StormByte::Safe::String FormatHexLines(std::span< const std::byte > &data, StormByte::ByteSize start_offset, StormByte::ByteSize columns) noexcept
Format hex/ASCII lines for a data span.
FIFO(const R &r) noexcept
Construct a FIFO from an input range.
Definition fifo.hxx:120
virtual bool WriteInternal(const StormByte::ByteSize &count, const StormByte::Safe::Binary &src) noexcept
Append from owned storage (copy).
FIFO(StormByte::Safe::Binary &&data) noexcept
Construct a FIFO with initial data (move).
Definition fifo.hxx:109
virtual StormByte::ByteSize Available() const noexcept override
Bytes available from the current read position.
void Close() noexcept override
Mark the buffer closed for further writes.
FIFO() noexcept=default
Default construct an empty FIFO.
virtual bool EoF() const noexcept override
End-of-stream condition.
virtual StormByte::Safe::String HexDumpHeader() const noexcept
Build the hexdump header.
FIFO(Rr &&r) noexcept
Construct a FIFO from an rvalue range.
Definition fifo.hxx:129
bool m_closed
Once true, further writes fail.
Definition fifo.hxx:497
Operation
Kind of internal read operation.
Definition fifo.hxx:503
virtual void Clear() noexcept override
Clear stored bytes and reset the read position.
virtual const StormByte::Safe::Binary & Data() const noexcept override
Access the internal storage.
StormByte::Safe::Binary m_buffer
Owned contiguous byte storage.
Definition fifo.hxx:495
bool operator!=(const FIFO &other) const noexcept
Compare two FIFOs.
Definition fifo.hxx:203
bool Extract(const StormByte::ByteSize &count, StormByte::Safe::Binary &outBuffer) noexcept override
Extract bytes into owned storage.
StormByte::ByteSize m_position_offset
Logical read offset from the start of m_buffer.
Definition fifo.hxx:496
bool Peek(const StormByte::ByteSize &count, StormByte::Safe::Binary &outBuffer) const noexcept override
Peek into owned storage without advancing the position.
virtual bool IsReadable() const noexcept override
Whether the buffer can be read.
bool Write(const StormByte::ByteSize &count, const StormByte::Safe::Binary &data) noexcept override
Append bytes from owned storage (copy).
virtual void Seek(const std::ptrdiff_t &offset, const Position &mode) const noexcept override
Move the logical read position.
FIFO(const char *s) noexcept
Construct a FIFO from a C string.
Definition fifo.hxx:143
virtual void Clean() noexcept override
Discard data from the start up to the current read position.
bool Read(const StormByte::ByteSize &count, StormByte::Safe::Binary &outBuffer) const noexcept override
Read into owned storage and advance the position.
virtual bool ReadInternal(const StormByte::ByteSize &count, StormByte::Safe::Binary &outBuffer, const Operation &flag) noexcept
Shared implementation for Extract, Read and Peek into owned storage.
void SetError() noexcept override
Enter the permanent error state.
virtual bool Drop(const StormByte::ByteSize &count) noexcept override
Discard unread bytes and advance the read position.
bool m_error
Permanent error: unreadable and unwritable.
Definition fifo.hxx:498
static STORMBYTE_FORCE_INLINE Safe::Binary DataConvert(const Src &src) noexcept
Convert an lvalue input range to StormByte::Safe::Binary.
Definition generic.hxx:156
Buffer that can be read and not written.
Definition generic.hxx:220
ReadWrite() noexcept
Default constructor.
Buffer that can be written and not read.
Definition generic.hxx:497
STORMBYTE_DECLARE_MAYBE_SAFE(class StormByte::System::Device::Access)
Buffer module of the StormByte suite.
Position
Forward declaration of the WriteOnly interface.
Definition typedefs.hxx:74
Root namespace of the StormByte C++ suite.
STL namespace.
#define STORMBYTE_BUFFER_PUBLIC
Definition visibility.h:54