StormByte-Buffer 4.0.0
C++26 buffer module of the StormByte suite
Loading...
Searching...
No Matches
ring.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/condition_variable_any.hxx>
49#include <StormByte/safe/deque.hxx>
50#include <StormByte/safe/exclusive_lock.hxx>
51#include <StormByte/safe/shared_mutex.hxx>
52#include <StormByte/safe/string.hxx>
53
54#include <cstddef>
55#include <span>
56#include <string_view>
57#include <utility>
58
63namespace StormByte {
68 namespace Buffer {
91 public:
96
100 Ring() noexcept;
101
106 explicit Ring(const StormByte::Safe::Binary& data) noexcept;
107
112 explicit Ring(StormByte::Safe::Binary&& data) noexcept;
113
119 template<Type::ByteInputRange R>
120 requires (!Type::SameAs<R, StormByte::Safe::Binary>)
121 inline explicit Ring(const R& r) noexcept
122 : Ring(DataConvert(r)) {}
123
129 template<Type::ByteInputRange Rr>
130 inline explicit Ring(Rr&& r) noexcept
131 : Ring(DataConvert(std::forward<Rr>(r))) {}
132
137 explicit Ring(std::string_view sv) noexcept;
138
143 inline explicit Ring(const char* s) noexcept
144 : Ring(s ? std::string_view(s) : std::string_view{}) {}
145
149 Ring(const Ring&) = delete;
150
155 Ring(Ring&& other) noexcept;
156
160 virtual ~Ring() noexcept;
161
165 Ring& operator=(const Ring&) = delete;
166
172 Ring& operator=(Ring&& other) noexcept;
173
177
182
188 bool operator==(const Ring& other) const noexcept;
189
195 inline bool operator!=(const Ring& other) const noexcept {
196 return !(*this == other);
197 }
198
202
207
212 StormByte::ByteSize Available() const noexcept override;
213
218 const StormByte::Safe::Binary& Data() const noexcept override;
219
224 bool Empty() const noexcept override;
225
230 bool EoF() const noexcept override;
231
236 bool HasError() const noexcept;
237
242 bool IsReadable() const noexcept override;
243
248 bool IsWritable() const noexcept override;
249
254 StormByte::ByteSize Size() const noexcept override;
255
259
264
268 void Clean() noexcept override;
269
274 void Clear() noexcept override;
275
280 void Close() noexcept override;
281
287 bool Drop(const StormByte::ByteSize& count) noexcept override;
288
294 void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept override;
295
299 void SetError() noexcept override;
300
304
309
316 StormByte::Safe::String HexDump(const StormByte::ByteSize& columns = 16,
317 const StormByte::ByteSize& byte_limit = 0) const noexcept;
318
322
327
334 bool Peek(const StormByte::ByteSize& count, StormByte::Safe::Binary& outBuffer) const noexcept override;
335
342 bool Peek(const StormByte::ByteSize& count, WriteOnly& outBuffer) const noexcept override;
343
347
352
359 bool Read(const StormByte::ByteSize& count, StormByte::Safe::Binary& outBuffer) const noexcept override;
360
367 bool Read(const StormByte::ByteSize& count, WriteOnly& outBuffer) const noexcept override;
368
373 void ReadUntilEoF(StormByte::Safe::Binary& outBuffer) const noexcept override;
374
379 void ReadUntilEoF(WriteOnly& outBuffer) const noexcept override;
380
384
389
396 bool Extract(const StormByte::ByteSize& count, StormByte::Safe::Binary& outBuffer) noexcept override;
397
404 bool Extract(const StormByte::ByteSize& count, WriteOnly& outBuffer) noexcept override;
405
410 void ExtractUntilEoF(StormByte::Safe::Binary& outBuffer) noexcept override;
411
416 void ExtractUntilEoF(WriteOnly& outBuffer) noexcept override;
417
421
426
433 bool Write(const StormByte::ByteSize& count, const StormByte::Safe::Binary& data) noexcept override;
434
441 bool Write(const StormByte::ByteSize& count, StormByte::Safe::Binary&& data) noexcept override;
442
449 bool Write(const StormByte::ByteSize& count, const ReadOnly& data) noexcept override;
450
457 bool Write(const StormByte::ByteSize& count, ReadOnly&& data) noexcept override;
458
462 using WriteOnly::Write;
463
467
468 protected:
472 enum class Operation {
476 };
477
485 static StormByte::Safe::String FormatHexLines(std::span<const std::byte> data,
486 StormByte::ByteSize start_offset,
487 StormByte::ByteSize columns) noexcept;
488
493 virtual StormByte::Safe::String HexDumpHeader() const noexcept;
494
499
507 virtual bool ReadInternal(const StormByte::ByteSize& count, StormByte::Safe::Binary& outBuffer, Operation flag) noexcept;
508
516 virtual bool ReadInternal(const StormByte::ByteSize& count, WriteOnly& outBuffer, Operation flag) noexcept;
517
523 virtual void ReadUntilEoFInternal(StormByte::Safe::Binary& outBuffer, Operation flag) noexcept;
524
530 virtual void ReadUntilEoFInternal(WriteOnly& outBuffer, Operation flag) noexcept;
531
538 virtual bool WriteInternal(const StormByte::ByteSize& count, const StormByte::Safe::Binary& src) noexcept;
539
546 virtual bool WriteInternal(const StormByte::ByteSize& count, StormByte::Safe::Binary&& src) noexcept;
547
551
552 private:
553 StormByte::Safe::Deque<std::byte> m_buffer;
554 mutable StormByte::ByteSize m_position_offset{0};
555 bool m_closed{false};
556 bool m_error{false};
557 StormByte::Safe::String m_error_message;
558
559 mutable StormByte::Safe::Binary m_data_cache;
560 mutable StormByte::Safe::SharedMutex m_mutex;
562
568 void Wait(const StormByte::ByteSize& n, StormByte::Safe::ExclusiveLock& lock) const;
569 };
570 }
571}
572
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.
Highly concurrent thread-safe ring buffer.
Definition ring.hxx:90
virtual ~Ring() noexcept
Destroy the Ring.
bool Read(const StormByte::ByteSize &count, StormByte::Safe::Binary &outBuffer) const noexcept override
Read into owned storage and advance the position.
bool Empty() const noexcept override
Whether the underlying storage is empty.
bool Peek(const StormByte::ByteSize &count, StormByte::Safe::Binary &outBuffer) const noexcept override
Peek into owned storage without advancing the position.
void Seek(const std::ptrdiff_t &offset, const Position &mode) const noexcept override
Move the logical read position.
void Clean() noexcept override
Discard data from the start up to the current read position.
Ring(Ring &&other) noexcept
Take a Ring.
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.
void Close() noexcept override
Close the buffer for further writes and notify waiters.
virtual void ReadUntilEoFInternal(StormByte::Safe::Binary &outBuffer, Operation flag) noexcept
Drain until EoF into owned storage.
bool Write(const StormByte::ByteSize &count, const StormByte::Safe::Binary &data) noexcept override
Append bytes from owned storage (copy).
Ring(const Ring &)=delete
Copy constructor.
StormByte::ByteSize Size() const noexcept override
Total number of bytes stored.
Ring(const char *s) noexcept
Construct from a C string.
Definition ring.hxx:143
bool EoF() const noexcept override
End-of-stream condition.
bool IsWritable() const noexcept override
Whether the buffer accepts writes.
void ExtractUntilEoF(StormByte::Safe::Binary &outBuffer) noexcept override
Extract until EoF into owned storage.
virtual bool WriteInternal(const StormByte::ByteSize &count, const StormByte::Safe::Binary &src) noexcept
Append from owned storage (copy).
void Clear() noexcept override
Clear stored bytes and reset the read position.
bool HasError() const noexcept
Whether SetError() has been called.
StormByte::ByteSize Available() const noexcept override
Bytes available from the current read position.
Ring(Rr &&r) noexcept
Convert an rvalue range and delegate construction to the provider.
Definition ring.hxx:130
bool Extract(const StormByte::ByteSize &count, StormByte::Safe::Binary &outBuffer) noexcept override
Extract bytes into owned storage.
bool IsReadable() const noexcept override
Whether the buffer can still be read.
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.
void ReadUntilEoF(StormByte::Safe::Binary &outBuffer) const noexcept override
Read until EoF into owned storage.
void SetError() noexcept override
Enter the permanent error state and notify waiters.
const StormByte::Safe::Binary & Data() const noexcept override
Snapshot of stored data.
virtual StormByte::Safe::String HexDumpHeader() const noexcept
Build the hexdump header.
bool Drop(const StormByte::ByteSize &count) noexcept override
Discard unread bytes.
virtual bool ReadInternal(const StormByte::ByteSize &count, StormByte::Safe::Binary &outBuffer, Operation flag) noexcept
Shared Extract, Read and Peek into owned storage.
Ring(std::string_view sv) noexcept
Construct from a string view.
Operation
Kind of internal read operation.
Definition ring.hxx:472
Ring() noexcept
Construct an empty Ring.
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