3#include <StormByte/buffer/fifo.hxx>
5#include <condition_variable>
15namespace StormByte::Buffer {
81 template<std::ranges::input_range R>
82 requires (!std::is_class_v<std::remove_cv_t<std::ranges::range_value_t<R>>>) &&
83 requires(std::ranges::range_value_t<R> v) {
static_cast<std::byte
>(v); } &&
84 (!std::same_as<std::remove_cvref_t<R>, DataType>)
86 m_error(
false), m_error_message() {}
93 template<std::ranges::input_range Rr>
94 requires (!std::is_class_v<std::remove_cv_t<std::ranges::range_value_t<Rr>>>) &&
95 requires(std::ranges::range_value_t<Rr> v) {
static_cast<std::byte
>(v); }
96 inline SharedFIFO(Rr&& r)
noexcept:
FIFO(std::forward<Rr>(r)), m_closed(
false),
97 m_error(
false), m_error_message() {}
103 m_error(
false), m_error_message() {}
188 inline
bool operator!=(const
SharedFIFO& other) const noexcept {
189 return !(*
this == other);
202 void Clean() noexcept override;
208 virtual
void Clear() noexcept override;
215 inline virtual const DataType& Data() const noexcept
override {
233 virtual
bool Drop(const std::
size_t& count) noexcept override;
242 virtual
bool Empty() const noexcept override;
250 virtual
bool EoF() const noexcept override;
256 bool HasError() const noexcept;
275 virtual std::
string HexDump(const std::
size_t& collumns = 0, const std::
size_t& byte_limit = 0) const noexcept override;
285 inline virtual
bool IsReadable() const noexcept
override {
return !m_error; }
293 inline virtual bool IsWritable() const noexcept
override {
return !m_closed && !m_error; }
304 virtual void Seek(
const std::ptrdiff_t& offset,
const Position& mode)
const noexcept override;
319 virtual std::
size_t Size() const noexcept override;
322 bool m_closed {
false};
323 bool m_error {
false};
327 mutable std::mutex m_mutex;
328 mutable std::condition_variable_any m_cv;
334 std::ostringstream HexDumpHeader() const noexcept override;
345 virtual
bool ReadInternal(const std::
size_t& count, DataType& outBuffer, const
Operation& flag) noexcept override;
355 virtual
bool ReadInternal(const std::
size_t& count,
WriteOnly& outBuffer, const
Operation& flag) noexcept override;
367 void Wait(const std::
size_t& n, std::unique_lock<std::mutex>& lock) const;
375 virtual
bool WriteInternal(const std::
size_t& count, const DataType& src) noexcept override;
383 virtual
bool WriteInternal(const std::
size_t& count, DataType&& src) noexcept override;
Byte-oriented FIFO buffer with grow-on-demand.
Definition fifo.hxx:40
Operation
Enumeration of read operation types.
Definition fifo.hxx:451
Thread-safe FIFO built on top of FIFO.
Definition shared_fifo.hxx:52
SharedFIFO(FIFO &&other) noexcept
Construct a SharedFIFO by moving from a FIFO.
Definition shared_fifo.hxx:115
virtual std::size_t AvailableBytes() const noexcept override
Get the number of bytes available for reading.
std::string m_error_message
Optional error message associated with the error state.
Definition shared_fifo.hxx:324
SharedFIFO(const R &r) noexcept
Construct FIFO from an input range.
Definition shared_fifo.hxx:85
SharedFIFO(DataType &&data) noexcept
Construct a SharedFIFO with initial data using move semantics.
Definition shared_fifo.hxx:71
virtual void Close() noexcept
Thread-safe close for further writes.
SharedFIFO(const FIFO &other)
Construct a SharedFIFO by copying or moving from a FIFO.
Definition shared_fifo.hxx:109
virtual void Seek(const std::ptrdiff_t &offset, const Position &mode) const noexcept override
Move the read position for non-destructive reads.
virtual ~SharedFIFO() noexcept=default
Virtual destructor.
virtual bool IsWritable() const noexcept override
Check if the buffer is writable (not closed and not in error state).
Definition shared_fifo.hxx:293
virtual void SetError() noexcept
Thread-safe error state setting.
SharedFIFO(std::string_view sv) noexcept
Construct FIFO from a string view (does not include terminating NUL).
Definition shared_fifo.hxx:102
SharedFIFO(SharedFIFO &&other) noexcept=default
Move constructor.
SharedFIFO(Rr &&r) noexcept
Construct FIFO from an rvalue range (moves when DataType rvalue).
Definition shared_fifo.hxx:96
SharedFIFO(const SharedFIFO &)=delete
Copy constructors are deleted.
SharedFIFO() noexcept=default
Construct a SharedFIFO with optional initial capacity.
Generic class providing a buffer that can be written to but not read from.
Definition generic.hxx:408