StormByte-Buffer 2.0.0
C++26 buffer module of the StormByte suite
 
Loading...
Searching...
No Matches
shared_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
46
47#include <condition_variable>
48#include <mutex>
49#include <string_view>
50
55namespace StormByte::Buffer {
97 public:
107 SharedFIFO() noexcept = default;
108
113 inline SharedFIFO(const StormByte::BinaryData& data) : FIFO(data) {}
114
119 inline SharedFIFO(StormByte::BinaryData&& data) noexcept : FIFO(std::move(data)) {}
120
126 template<StormByte::Type::ByteInputRange R>
128 inline SharedFIFO(const R& r) noexcept : FIFO(r) {}
129
135 template<StormByte::Type::ByteInputRange Rr>
136 inline SharedFIFO(Rr&& r) noexcept : FIFO(std::forward<Rr>(r)) {}
137
142 inline SharedFIFO(std::string_view sv) noexcept : FIFO(sv) {}
143
148 inline SharedFIFO(const char* s) noexcept : FIFO(s) {}
149
154 inline SharedFIFO(const FIFO& other) : FIFO(other) {}
155
160 inline SharedFIFO(FIFO&& other) noexcept : FIFO(std::move(other)) {}
161
165 SharedFIFO(const SharedFIFO&) = delete;
166
171
173 virtual ~SharedFIFO() noexcept;
174
180 SharedFIFO& operator=(const FIFO& other);
181
187 SharedFIFO& operator=(FIFO&& other) noexcept;
188
192 SharedFIFO& operator=(const SharedFIFO&) = delete;
193
197 SharedFIFO& operator=(SharedFIFO&&) = delete;
198
212 bool operator==(const SharedFIFO& other) const noexcept;
213
219 inline bool operator!=(const SharedFIFO& other) const noexcept {
220 return !(*this == other);
221 }
222
234 virtual StormByte::ByteSize Available() const noexcept override;
235
241 virtual const StormByte::BinaryData& Data() const noexcept override;
242
250 virtual bool Empty() const noexcept override;
251
256 virtual bool EoF() const noexcept override;
257
262 bool HasError() const noexcept;
263
269 virtual bool IsReadable() const noexcept override;
270
276 virtual bool IsWritable() const noexcept override;
277
283 virtual StormByte::ByteSize Size() const noexcept override;
284
296 void Clean() noexcept override;
297
303 virtual void Clear() noexcept override;
304
311 virtual void Close() noexcept override;
312
320 virtual bool Drop(const StormByte::ByteSize& count) noexcept override;
321
329 virtual void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept override;
330
336 virtual void SetError() noexcept override;
337
352 virtual StormByte::String::String HexDump(const StormByte::ByteSize& columns = 0,
353 const StormByte::ByteSize& byte_limit = 0) const noexcept override;
354
357 protected:
358 // Closed/error state is owned by the base FIFO class.
359 // SharedFIFO only protects access and notifies waiters.
360
361 private:
362 mutable std::mutex m_mutex;
363 mutable std::condition_variable_any m_cv;
364
369 std::ostringstream HexDumpHeader() const noexcept override;
370
378 virtual bool ReadInternal(const StormByte::ByteSize& count, StormByte::BinaryData& outBuffer,
379 const Operation& flag) noexcept override;
380
388 virtual bool ReadInternal(const StormByte::ByteSize& count, WriteOnly& outBuffer,
389 const Operation& flag) noexcept override;
390
399 void Wait(const StormByte::ByteSize& n, std::unique_lock<std::mutex>& lock) const;
400
407 virtual bool WriteInternal(const StormByte::ByteSize& count, const StormByte::BinaryData& src) noexcept override;
408
415 virtual bool WriteInternal(const StormByte::ByteSize& count, StormByte::BinaryData&& src) noexcept override;
416 };
417}
Byte-oriented FIFO buffer with grow-on-demand and close/error support.
Definition fifo.hxx:78
Operation
Kind of internal read operation.
Definition fifo.hxx:474
Thread-safe FIFO built on top of FIFO.
Definition shared_fifo.hxx:96
SharedFIFO(FIFO &&other) noexcept
Construct by moving a plain FIFO.
Definition shared_fifo.hxx:160
SharedFIFO(StormByte::BinaryData &&data) noexcept
Construct with initial data (move).
Definition shared_fifo.hxx:119
virtual StormByte::ByteSize Available() const noexcept override
Bytes available from the current read position (thread-safe).
SharedFIFO(SharedFIFO &&)=delete
Move constructor (deleted – mutex / condition_variable are not movable).
SharedFIFO(const char *s) noexcept
Construct from a null-terminated C string.
Definition shared_fifo.hxx:148
SharedFIFO(const FIFO &other)
Construct by copying a plain FIFO.
Definition shared_fifo.hxx:154
SharedFIFO(const R &r) noexcept
Construct from an input range (copy / convert).
Definition shared_fifo.hxx:128
virtual ~SharedFIFO() noexcept
Virtual destructor.
SharedFIFO(std::string_view sv) noexcept
Construct from a string view (no trailing NUL).
Definition shared_fifo.hxx:142
SharedFIFO(Rr &&r) noexcept
Construct from an rvalue range.
Definition shared_fifo.hxx:136
SharedFIFO(const SharedFIFO &)=delete
Copy constructor (deleted – synchronization primitives are not copyable).
SharedFIFO() noexcept=default
Default construct an empty SharedFIFO.
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