StormByte C++ Library: Buffer module 1.2.0
StormByte-Buffer is the buffer module of the StormByte C++ 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 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#include <StormByte/type_traits.hxx>
24
25#include <condition_variable>
26#include <mutex>
27
32namespace StormByte::Buffer {
74 public:
84 SharedFIFO() noexcept = default;
85
90 inline SharedFIFO(const DataType& data) : FIFO(data) {}
91
96 inline SharedFIFO(DataType&& data) noexcept : FIFO(std::move(data)) {}
97
103 template<StormByte::Type::ByteInputRange R>
104 requires (!StormByte::Type::SameAs<R, DataType>)
105 inline SharedFIFO(const R& r) noexcept : FIFO(r) {}
106
112 template<StormByte::Type::ByteInputRange Rr>
113 inline SharedFIFO(Rr&& r) noexcept : FIFO(std::forward<Rr>(r)) {}
114
119 inline SharedFIFO(std::string_view sv) noexcept : FIFO(sv) {}
120
125 inline SharedFIFO(const char* s) noexcept : FIFO(s) {}
126
131 inline SharedFIFO(const FIFO& other) : FIFO(other) {}
132
137 inline SharedFIFO(FIFO&& other) noexcept : FIFO(std::move(other)) {}
138
142 SharedFIFO(const SharedFIFO&) = delete;
143
148
150 virtual ~SharedFIFO() noexcept;
151
157 SharedFIFO& operator=(const FIFO& other);
158
164 SharedFIFO& operator=(FIFO&& other) noexcept;
165
169 SharedFIFO& operator=(const SharedFIFO&) = delete;
170
174 SharedFIFO& operator=(SharedFIFO&&) = delete;
175
189 bool operator==(const SharedFIFO& other) const noexcept;
190
196 inline bool operator!=(const SharedFIFO& other) const noexcept {
197 return !(*this == other);
198 }
199
211 virtual std::size_t AvailableBytes() const noexcept override;
212
218 virtual const DataType& Data() const noexcept override;
219
227 virtual bool Empty() const noexcept override;
228
233 virtual bool EoF() const noexcept override;
234
239 bool HasError() const noexcept;
240
246 virtual bool IsReadable() const noexcept override;
247
253 virtual bool IsWritable() const noexcept override;
254
260 virtual std::size_t Size() const noexcept override;
261
273 void Clean() noexcept override;
274
280 virtual void Clear() noexcept override;
281
288 virtual void Close() noexcept override;
289
297 virtual bool Drop(const std::size_t& count) noexcept override;
298
306 virtual void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept override;
307
313 virtual void SetError() noexcept override;
314
329 virtual std::string HexDump(const std::size_t& columns = 0,
330 const std::size_t& byte_limit = 0) const noexcept override;
331
334 protected:
335 // Closed/error state is owned by the base FIFO class.
336 // SharedFIFO only protects access and notifies waiters.
337
338 private:
339 mutable std::mutex m_mutex;
340 mutable std::condition_variable_any m_cv;
341
346 std::ostringstream HexDumpHeader() const noexcept override;
347
355 virtual bool ReadInternal(const std::size_t& count, DataType& outBuffer,
356 const Operation& flag) noexcept override;
357
365 virtual bool ReadInternal(const std::size_t& count, WriteOnly& outBuffer,
366 const Operation& flag) noexcept override;
367
376 void Wait(const std::size_t& n, std::unique_lock<std::mutex>& lock) const;
377
384 virtual bool WriteInternal(const std::size_t& count, const DataType& src) noexcept override;
385
392 virtual bool WriteInternal(const std::size_t& count, DataType&& src) noexcept override;
393 };
394}
Byte-oriented FIFO buffer with grow-on-demand and close/error support.
Definition fifo.hxx:58
Operation
Kind of internal read operation.
Definition fifo.hxx:454
Thread-safe FIFO built on top of FIFO.
Definition shared_fifo.hxx:73
SharedFIFO(FIFO &&other) noexcept
Construct by moving a plain FIFO.
Definition shared_fifo.hxx:137
virtual std::size_t AvailableBytes() 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(DataType &&data) noexcept
Construct with initial data (move).
Definition shared_fifo.hxx:96
SharedFIFO(const char *s) noexcept
Construct from a null-terminated C string.
Definition shared_fifo.hxx:125
SharedFIFO(const FIFO &other)
Construct by copying a plain FIFO.
Definition shared_fifo.hxx:131
SharedFIFO(const R &r) noexcept
Construct from an input range (copy / convert).
Definition shared_fifo.hxx:105
virtual ~SharedFIFO() noexcept
Virtual destructor.
SharedFIFO(std::string_view sv) noexcept
Construct from a string view (no trailing NUL).
Definition shared_fifo.hxx:119
SharedFIFO(Rr &&r) noexcept
Construct from an rvalue range.
Definition shared_fifo.hxx:113
SharedFIFO(const SharedFIFO &)=delete
Copy constructor (deleted – synchronization primitives are not copyable).
SharedFIFO() noexcept=default
Default construct an empty SharedFIFO.
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