StormByte-Buffer 4.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
44#include <StormByte/type_traits/safe.hxx>
45
47#include <StormByte/safe/condition_variable_any.hxx>
48#include <StormByte/safe/mutex.hxx>
49#include <StormByte/safe/unique_lock.hxx>
50#include <StormByte/type_traits.hxx>
51
52#include <string_view>
53
58namespace StormByte {
63 namespace Buffer {
92 public:
97
101 SharedFIFO() noexcept;
102
107 SharedFIFO(const StormByte::Safe::Binary& data);
108
113 SharedFIFO(StormByte::Safe::Binary&& data) noexcept;
114
120 template<StormByte::Type::ByteInputRange R>
121 requires (!StormByte::Type::SameAs<R, StormByte::Safe::Binary>)
123
129 template<StormByte::Type::ByteInputRange Rr>
130 STORMBYTE_FORCE_INLINE SharedFIFO(Rr&& r) noexcept: SharedFIFO(DataConvert(std::forward<Rr>(r))) {}
131
136 SharedFIFO(std::string_view sv) noexcept;
137
142 SharedFIFO(const char* s) noexcept;
143
148 SharedFIFO(const FIFO& other);
149
154 SharedFIFO(FIFO&& other) noexcept;
155
159 SharedFIFO(const SharedFIFO&) = delete;
160
165
169 virtual ~SharedFIFO() noexcept;
170
176 SharedFIFO& operator=(const FIFO& other);
177
183 SharedFIFO& operator=(FIFO&& other) noexcept;
184
188 SharedFIFO& operator=(const SharedFIFO&) = delete;
189
193 SharedFIFO& operator=(SharedFIFO&&) = delete;
194
198
203
209 bool operator==(const SharedFIFO& other) const noexcept;
210
216 inline bool operator!=(const SharedFIFO& other) const noexcept {
217 return !(*this == other);
218 }
219
223
228
233 virtual StormByte::ByteSize Available() const noexcept override;
234
240 virtual const StormByte::Safe::Binary& Data() const noexcept override;
241
248 virtual bool Empty() const noexcept override;
249
254 virtual bool EoF() const noexcept override;
255
260 bool HasError() const noexcept;
261
267 virtual bool IsReadable() const noexcept override;
268
274 virtual bool IsWritable() const noexcept override;
275
281 virtual StormByte::ByteSize Size() const noexcept override;
282
286
291
296 void Clean() noexcept override;
297
303 virtual void Clear() noexcept override;
304
309 virtual void Close() noexcept override;
310
317 virtual bool Drop(const StormByte::ByteSize& count) noexcept override;
318
326 virtual void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept override;
327
332 virtual void SetError() noexcept override;
333
337
342
349 virtual StormByte::Safe::String HexDump(const StormByte::ByteSize& columns = 0,
350 const StormByte::ByteSize& byte_limit = 0) const noexcept override;
351
355
356 private:
357 mutable StormByte::Safe::Mutex m_mutex;
358 mutable StormByte::Safe::ConditionVariableAny m_cv;
359
364 StormByte::Safe::String HexDumpHeader() const noexcept override;
365
373 virtual bool ReadInternal(const StormByte::ByteSize& count, StormByte::Safe::Binary& outBuffer,
374 const Operation& flag) noexcept override;
375
383 virtual bool ReadInternal(const StormByte::ByteSize& count, WriteOnly& outBuffer,
384 const Operation& flag) noexcept override;
385
393 void Wait(const StormByte::ByteSize& n, StormByte::Safe::UniqueLock& lock) const;
394
401 virtual bool WriteInternal(const StormByte::ByteSize& count, const StormByte::Safe::Binary& src) noexcept override;
402
409 virtual bool WriteInternal(const StormByte::ByteSize& count, StormByte::Safe::Binary&& src) noexcept override;
410 };
411 }
412}
413
FIFO() noexcept=default
Default construct an empty FIFO.
Operation
Kind of internal read operation.
Definition fifo.hxx:503
static STORMBYTE_FORCE_INLINE Safe::Binary DataConvert(const Src &src) noexcept
Convert an lvalue input range to StormByte::Safe::Binary.
Definition generic.hxx:156
bool HasError() const noexcept
Whether the buffer is in a permanent error state.
STORMBYTE_FORCE_INLINE SharedFIFO(Rr &&r) noexcept
Construct from an rvalue range.
Definition shared_fifo.hxx:130
virtual bool Empty() const noexcept override
Whether the underlying storage is empty.
SharedFIFO(FIFO &&other) noexcept
Construct by moving a plain FIFO.
virtual const StormByte::Safe::Binary & Data() const noexcept override
Access the internal storage.
virtual void SetError() noexcept override
Enter the permanent error state and notify waiters.
virtual StormByte::ByteSize Available() const noexcept override
Bytes available from the current read position.
SharedFIFO(SharedFIFO &&)=delete
Move constructor.
SharedFIFO() noexcept
Default construct an empty SharedFIFO.
virtual void Close() noexcept override
Close for further writes and notify waiters.
SharedFIFO(const char *s) noexcept
Construct from a C string.
virtual bool IsReadable() const noexcept override
Whether the buffer can still be read.
SharedFIFO(const FIFO &other)
Construct by copying a plain FIFO.
virtual StormByte::ByteSize Size() const noexcept override
Total number of bytes stored.
virtual void Seek(const std::ptrdiff_t &offset, const Position &mode) const noexcept override
Move the logical read position and notify waiters.
virtual bool IsWritable() const noexcept override
Whether the buffer accepts writes.
virtual ~SharedFIFO() noexcept
Destroy the SharedFIFO.
virtual StormByte::Safe::String HexDump(const StormByte::ByteSize &columns=0, const StormByte::ByteSize &byte_limit=0) const noexcept override
Hexdump of unread contents.
SharedFIFO(std::string_view sv) noexcept
Construct from a string view.
void Clean() noexcept override
Discard data from the start up to the read position.
virtual bool Drop(const StormByte::ByteSize &count) noexcept override
Discard unread bytes and notify waiters.
SharedFIFO(const SharedFIFO &)=delete
Copy constructor.
virtual void Clear() noexcept override
Clear stored bytes.
virtual bool EoF() const noexcept override
End-of-stream condition.
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_FORCE_INLINE
#define STORMBYTE_BUFFER_PUBLIC
Definition visibility.h:54