StormByte-Buffer 2.0.0
C++26 buffer module of the StormByte suite
 
Loading...
Searching...
No Matches
consumer.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
45
46#include <memory>
47
52namespace StormByte {
57 namespace Buffer {
58 class Producer;
59
85 friend class Producer;
86
87 public:
99 inline Consumer() noexcept : m_buffer(std::make_shared<Ring>()) {}
100
105 inline Consumer(const Consumer& other) noexcept : m_buffer(other.m_buffer) {}
106
111 inline Consumer(Consumer&& other) noexcept : m_buffer(std::move(other.m_buffer)) {}
112
116 ~Consumer() noexcept override;
117
123 inline Consumer& operator=(const Consumer& other) noexcept {
124 if (this != &other)
125 m_buffer = other.m_buffer;
126 return *this;
127 }
128
134 inline Consumer& operator=(Consumer&& other) noexcept {
135 if (this != &other)
136 m_buffer = std::move(other.m_buffer);
137 return *this;
138 }
139
152 inline bool operator==(const Consumer& other) const noexcept {
153 return m_buffer.get() == other.m_buffer.get();
154 }
155
161 inline bool operator!=(const Consumer& other) const noexcept {
162 return !(*this == other);
163 }
164
176 StormByte::ByteSize Available() const noexcept override;
177
183 const StormByte::BinaryData& Data() const noexcept override;
184
191 bool Empty() const noexcept override;
192
197 bool EoF() const noexcept override;
198
203 bool IsReadable() const noexcept override;
204
211 inline bool IsWritable() const noexcept {
212 return m_buffer->IsWritable();
213 }
214
219 inline bool HasError() const noexcept {
220 return m_buffer->HasError();
221 }
222
227 StormByte::ByteSize Size() const noexcept override;
228
236 class Producer Producer() const noexcept;
237
248 void Clean() noexcept override;
249
254 void Clear() noexcept override;
255
261 inline void Close() noexcept {
262 m_buffer->Close();
263 }
264
270 bool Drop(const StormByte::ByteSize& count) noexcept override;
271
277 void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept override;
278
292 bool Extract(const StormByte::ByteSize& count, StormByte::BinaryData& out) noexcept override;
293
300 bool Extract(const StormByte::ByteSize& count, WriteOnly& out) noexcept override;
301
306 void ExtractUntilEoF(StormByte::BinaryData& out) noexcept override;
307
312 void ExtractUntilEoF(WriteOnly& out) noexcept override;
313
327 bool Read(const StormByte::ByteSize& count, StormByte::BinaryData& out) const noexcept override;
328
335 bool Read(const StormByte::ByteSize& count, WriteOnly& out) const noexcept override;
336
341 void ReadUntilEoF(StormByte::BinaryData& out) const noexcept override;
342
347 void ReadUntilEoF(WriteOnly& out) const noexcept override;
348
362 bool Peek(const StormByte::ByteSize& count, StormByte::BinaryData& out) const noexcept override;
363
370 bool Peek(const StormByte::ByteSize& count, WriteOnly& out) const noexcept override;
371
374 private:
375 std::shared_ptr<Ring> m_buffer;
376
383 inline explicit Consumer(std::shared_ptr<Ring> buffer) noexcept
384 : m_buffer(std::move(buffer)) {}
385 };
386 }
387}
Read-oriented handle over a shared Ring.
Definition consumer.hxx:84
bool Read(const StormByte::ByteSize &count, StormByte::BinaryData &out) const noexcept override
Read bytes into a StormByte::BinaryData.
Consumer() noexcept
Empty Consumer.
Definition consumer.hxx:99
bool Drop(const StormByte::ByteSize &count) noexcept override
Discard count bytes from the current read position.
Consumer & operator=(Consumer &&other) noexcept
Move assignment.
Definition consumer.hxx:134
Consumer(const Consumer &other) noexcept
Copy constructor.
Definition consumer.hxx:105
bool operator!=(const Consumer &other) const noexcept
Inequality comparison.
Definition consumer.hxx:161
void ReadUntilEoF(WriteOnly &out) const noexcept override
Read until EoF into a WriteOnly store.
~Consumer() noexcept override
Destructor.
bool Peek(const StormByte::ByteSize &count, StormByte::BinaryData &out) const noexcept override
Peek bytes into a StormByte::BinaryData.
bool HasError() const noexcept
Whether the shared Ring is in a permanent error state.
Definition consumer.hxx:219
StormByte::ByteSize Size() const noexcept override
Total number of bytes stored in the shared Ring.
void ReadUntilEoF(StormByte::BinaryData &out) const noexcept override
Read until EoF into a StormByte::BinaryData.
void ExtractUntilEoF(WriteOnly &out) noexcept override
Extract until EoF into a WriteOnly store.
void Seek(const std::ptrdiff_t &offset, const Position &mode) const noexcept override
Move the logical read position for non-destructive reads.
bool Extract(const StormByte::ByteSize &count, StormByte::BinaryData &out) noexcept override
Extract bytes into a StormByte::BinaryData (consumes data from the Ring).
bool operator==(const Consumer &other) const noexcept
Equality comparison.
Definition consumer.hxx:152
bool Extract(const StormByte::ByteSize &count, WriteOnly &out) noexcept override
Extract bytes into a WriteOnly store.
StormByte::ByteSize Available() const noexcept override
Number of bytes available for reading from the current position.
void ExtractUntilEoF(StormByte::BinaryData &out) noexcept override
Extract until EoF into a StormByte::BinaryData.
Consumer(Consumer &&other) noexcept
Move constructor.
Definition consumer.hxx:111
bool Read(const StormByte::ByteSize &count, WriteOnly &out) const noexcept override
Read bytes into a WriteOnly store.
bool Peek(const StormByte::ByteSize &count, WriteOnly &out) const noexcept override
Peek bytes into a WriteOnly store.
Write-only handle over a shared Ring.
Definition producer.hxx:66
void Close() noexcept override
Close the shared Ring for further writes.
Buffer that can be read and not written.
Definition generic.hxx:228
High-performance, highly-concurrent thread-safe ring buffer.
Definition ring.hxx:83
Buffer that can be written and not read.
Definition generic.hxx:505
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