StormByte-Buffer 2.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
47
48#include <condition_variable>
49#include <deque>
50#include <shared_mutex>
51#include <span>
52#include <sstream>
53#include <utility>
54
59namespace StormByte::Buffer {
84 public:
93 Ring() noexcept = default;
94
99 inline explicit Ring(const StormByte::BinaryData& data) noexcept
100 : m_buffer(data.begin(), data.end()) {}
101
106 inline explicit Ring(StormByte::BinaryData&& data) noexcept
107 : m_buffer(std::make_move_iterator(data.begin()),
108 std::make_move_iterator(data.end())) {}
109
115 template<Type::ByteInputRange R>
117 inline explicit Ring(const R& r) noexcept {
118 auto converted = DataConvert(r);
119 m_buffer.assign(converted.begin(), converted.end());
120 }
121
127 template<Type::ByteInputRange Rr>
128 inline explicit Ring(Rr&& r) noexcept {
129 auto converted = DataConvert(std::forward<Rr>(r));
130 m_buffer.assign(std::make_move_iterator(converted.begin()),
131 std::make_move_iterator(converted.end()));
132 }
133
138 inline explicit Ring(std::string_view sv) noexcept {
139 auto converted = DataConvert(sv);
140 m_buffer.assign(converted.begin(), converted.end());
141 }
142
147 inline explicit Ring(const char* s) noexcept
148 : Ring(s ? std::string_view(s) : std::string_view{}) {}
149
153 Ring(const Ring&) = delete;
154
159 Ring(Ring&& other) noexcept;
160
164 virtual ~Ring() noexcept;
165
169 Ring& operator=(const Ring&) = delete;
170
176 Ring& operator=(Ring&& other) noexcept;
177
190 bool operator==(const Ring& other) const noexcept;
191
197 inline bool operator!=(const Ring& other) const noexcept {
198 return !(*this == other);
199 }
200
212 StormByte::ByteSize Available() const noexcept override;
213
218 const StormByte::BinaryData& 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
266 void Clean() noexcept override;
267
272 void Clear() noexcept override;
273
279 void Close() noexcept override;
280
286 bool Drop(const StormByte::ByteSize& count) noexcept override;
287
293 void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept override;
294
299 void SetError() noexcept override;
300
314 StormByte::String::String HexDump(const StormByte::ByteSize& columns = 16,
315 const StormByte::ByteSize& byte_limit = 0) const noexcept;
316
330 bool Peek(const StormByte::ByteSize& count, StormByte::BinaryData& outBuffer) const noexcept override;
331
338 bool Peek(const StormByte::ByteSize& count, WriteOnly& outBuffer) const noexcept override;
339
353 bool Read(const StormByte::ByteSize& count, StormByte::BinaryData& outBuffer) const noexcept override;
354
361 bool Read(const StormByte::ByteSize& count, WriteOnly& outBuffer) const noexcept override;
362
367 void ReadUntilEoF(StormByte::BinaryData& outBuffer) const noexcept override;
368
373 void ReadUntilEoF(WriteOnly& outBuffer) const noexcept override;
374
388 bool Extract(const StormByte::ByteSize& count, StormByte::BinaryData& outBuffer) noexcept override;
389
396 bool Extract(const StormByte::ByteSize& count, WriteOnly& outBuffer) noexcept override;
397
402 void ExtractUntilEoF(StormByte::BinaryData& outBuffer) noexcept override;
403
408 void ExtractUntilEoF(WriteOnly& outBuffer) noexcept override;
409
423 bool Write(const StormByte::ByteSize& count, const StormByte::BinaryData& data) noexcept override;
424
431 bool Write(const StormByte::ByteSize& count, StormByte::BinaryData&& data) noexcept override;
432
439 bool Write(const StormByte::ByteSize& count, const ReadOnly& data) noexcept override;
440
447 bool Write(const StormByte::ByteSize& count, ReadOnly&& data) noexcept override;
448
450 using WriteOnly::Write;
451
454 protected:
458 enum class Operation {
459 Extract,
460 Read,
461 Peek
462 };
463
471 static StormByte::String::String FormatHexLines(std::span<const std::byte> data,
472 StormByte::ByteSize start_offset,
473 StormByte::ByteSize columns) noexcept;
474
479 virtual std::ostringstream HexDumpHeader() const noexcept;
480
493 virtual bool ReadInternal(const StormByte::ByteSize& count, StormByte::BinaryData& outBuffer, Operation flag) noexcept;
494
502 virtual bool ReadInternal(const StormByte::ByteSize& count, WriteOnly& outBuffer, Operation flag) noexcept;
503
509 virtual void ReadUntilEoFInternal(StormByte::BinaryData& outBuffer, Operation flag) noexcept;
510
516 virtual void ReadUntilEoFInternal(WriteOnly& outBuffer, Operation flag) noexcept;
517
524 virtual bool WriteInternal(const StormByte::ByteSize& count, const StormByte::BinaryData& src) noexcept;
525
532 virtual bool WriteInternal(const StormByte::ByteSize& count, StormByte::BinaryData&& src) noexcept;
533
536 private:
537 std::deque<std::byte> m_buffer;
538 mutable StormByte::ByteSize m_position_offset{0};
539 bool m_closed{false};
540 bool m_error{false};
541 StormByte::String::String m_error_message;
542
543 mutable StormByte::BinaryData m_data_cache;
544 mutable std::shared_mutex m_mutex;
545 mutable std::condition_variable_any m_cv;
546
552 void Wait(const StormByte::ByteSize& n, std::unique_lock<std::shared_mutex>& lock) const;
553 };
554}
Buffer that can be read and not written.
Definition generic.hxx:228
Read and write interface.
Definition generic.hxx:872
High-performance, highly-concurrent thread-safe ring buffer.
Definition ring.hxx:83
virtual ~Ring() noexcept
Virtual destructor.
virtual std::ostringstream HexDumpHeader() const noexcept
Build the hexdump header (size / position / status).
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.
Ring(Ring &&other) noexcept
Move constructor.
Ring(const Ring &)=delete
Copy constructor (deleted – mutexes are not copyable).
Ring(StormByte::BinaryData &&data) noexcept
Construct with initial data (move elements from StormByte::BinaryData).
Definition ring.hxx:106
Ring() noexcept=default
Default construct an empty Ring.
Ring(const char *s) noexcept
Construct from a null-terminated C string.
Definition ring.hxx:147
StormByte::ByteSize Available() const noexcept override
Bytes available from the current read position.
Ring(Rr &&r) noexcept
Construct from an rvalue range.
Definition ring.hxx:128
Ring(const R &r) noexcept
Construct from an input range (copy / convert).
Definition ring.hxx:117
Ring(std::string_view sv) noexcept
Construct from a string view (no trailing NUL).
Definition ring.hxx:138
Operation
Kind of internal read operation.
Definition ring.hxx:458
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