StormByte C++ Library: Buffer module 1.2.0
StormByte-Buffer is the buffer module of the StormByte C++ 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 is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 3
8 * or later, as published by the Free Software Foundation.
9 *
10 * StormByte-Buffer is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with StormByte-Buffer. If not, see
17 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
18 */
19
20#pragma once
21
23
24#include <memory>
25
30namespace StormByte::Buffer {
54 friend class Producer;
55
56 public:
66 inline Consumer(const Consumer& other) noexcept : m_buffer(other.m_buffer) {}
67
72 inline Consumer(Consumer&& other) noexcept : m_buffer(std::move(other.m_buffer)) {}
73
77 ~Consumer() noexcept override;
78
84 inline Consumer& operator=(const Consumer& other) noexcept {
85 if (this != &other) m_buffer = other.m_buffer;
86 return *this;
87 }
88
94 inline Consumer& operator=(Consumer&& other) noexcept {
95 if (this != &other) m_buffer = std::move(other.m_buffer);
96 return *this;
97 }
98
111 inline bool operator==(const Consumer& other) const noexcept {
112 return m_buffer.get() == other.m_buffer.get();
113 }
114
120 inline bool operator!=(const Consumer& other) const noexcept {
121 return !(*this == other);
122 }
123
135 std::size_t AvailableBytes() const noexcept override;
136
142 const DataType& Data() const noexcept override;
143
150 bool Empty() const noexcept override;
151
156 bool EoF() const noexcept override;
157
162 bool IsReadable() const noexcept override;
163
170 inline bool IsWritable() const noexcept {
171 return m_buffer->IsWritable();
172 }
173
178 inline bool HasError() const noexcept {
179 return m_buffer->HasError();
180 }
181
186 std::size_t Size() const noexcept override;
187
198 void Clean() noexcept override;
199
204 void Clear() noexcept override;
205
211 inline void Close() noexcept {
212 m_buffer->Close();
213 }
214
220 bool Drop(const std::size_t& count) noexcept override;
221
227 void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept override;
228
242 bool Extract(const std::size_t& count, DataType& out) noexcept override;
243
250 bool Extract(const std::size_t& count, WriteOnly& out) noexcept override;
251
256 void ExtractUntilEoF(DataType& out) noexcept override;
257
262 void ExtractUntilEoF(WriteOnly& out) noexcept override;
263
277 bool Read(const std::size_t& count, DataType& out) const noexcept override;
278
285 bool Read(const std::size_t& count, WriteOnly& out) const noexcept override;
286
291 void ReadUntilEoF(DataType& out) const noexcept override;
292
297 void ReadUntilEoF(WriteOnly& out) const noexcept override;
298
312 bool Peek(const std::size_t& count, DataType& out) const noexcept override;
313
320 bool Peek(const std::size_t& count, WriteOnly& out) const noexcept override;
321
324 private:
325 std::shared_ptr<Ring> m_buffer;
326
331 inline explicit Consumer(std::shared_ptr<Ring> buffer) noexcept
332 : m_buffer(std::move(buffer)) {}
333 };
334}
Read-oriented handle over a shared Ring.
Definition consumer.hxx:53
bool Peek(const std::size_t &count, DataType &out) const noexcept override
Peek bytes into a DataType without advancing the read position.
bool Peek(const std::size_t &count, WriteOnly &out) const noexcept override
Peek bytes into a WriteOnly without advancing the read position.
bool Read(const std::size_t &count, DataType &out) const noexcept override
Non-destructive read into a DataType (advances logical position).
bool Drop(const std::size_t &count) noexcept override
Discard count bytes from the current read position.
Consumer & operator=(Consumer &&other) noexcept
Move assignment.
Definition consumer.hxx:94
Consumer(const Consumer &other) noexcept
Copy constructor.
Definition consumer.hxx:66
bool operator!=(const Consumer &other) const noexcept
Inequality comparison.
Definition consumer.hxx:120
void ReadUntilEoF(WriteOnly &out) const noexcept override
Read all remaining bytes until EoF into a WriteOnly.
~Consumer() noexcept override
Destructor.
void ExtractUntilEoF(DataType &out) noexcept override
Extract all remaining bytes until EoF into a DataType.
std::size_t Size() const noexcept override
Total number of bytes stored in the shared Ring.
bool Read(const std::size_t &count, WriteOnly &out) const noexcept override
Non-destructive read into a WriteOnly (advances logical position).
bool HasError() const noexcept
Whether the shared Ring is in a permanent error state.
Definition consumer.hxx:178
void ExtractUntilEoF(WriteOnly &out) noexcept override
Extract all remaining bytes until EoF into a WriteOnly.
void Seek(const std::ptrdiff_t &offset, const Position &mode) const noexcept override
Move the logical read position for non-destructive reads.
bool operator==(const Consumer &other) const noexcept
Equality comparison.
Definition consumer.hxx:111
bool Extract(const std::size_t &count, WriteOnly &out) noexcept override
Extract bytes into a WriteOnly sink (consumes data from the Ring).
std::size_t AvailableBytes() const noexcept override
Number of bytes available for reading from the current position.
void ReadUntilEoF(DataType &out) const noexcept override
Read all remaining bytes until EoF into a DataType.
Consumer(Consumer &&other) noexcept
Move constructor.
Definition consumer.hxx:72
bool Extract(const std::size_t &count, DataType &out) noexcept override
Extract bytes into a DataType (consumes data from the Ring).
Write-only handle over a shared Ring.
Definition producer.hxx:41
Pure interface for a buffer that can be read but not written.
Definition generic.hxx:164
Pure interface for a buffer that can be written but not read.
Definition generic.hxx:420
Buffer module of the StormByte suite.
std::vector< std::byte > DataType
Primary byte storage type used throughout the buffer API.
Definition typedefs.hxx:62
Position
Forward declaration of the WriteOnly interface.
Definition typedefs.hxx:50
#define STORMBYTE_BUFFER_PUBLIC
Definition visibility.h:28