StormByte C++ Library: Buffer module 0.0.9999
StormByte-Buffer is a StormByte library module for handling buffers
Loading...
Searching...
No Matches
shared_fifo.hxx
1#pragma once
2
3#include <StormByte/buffer/fifo.hxx>
4
5#include <condition_variable>
6#include <mutex>
7
15namespace StormByte::Buffer {
52 class STORMBYTE_BUFFER_PUBLIC SharedFIFO: public FIFO {
53 public:
59 SharedFIFO() noexcept = default;
60
65 inline SharedFIFO(const DataType& data): FIFO(data) {}
66
71 inline SharedFIFO(DataType&& data) noexcept: FIFO(std::move(data)) {}
72
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>)
85 inline SharedFIFO(const R& r) noexcept: FIFO(r), m_closed(false),
86 m_error(false), m_error_message() {}
87
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() {}
98
102 inline SharedFIFO(std::string_view sv) noexcept: FIFO(sv), m_closed(false),
103 m_error(false), m_error_message() {}
104
109 inline SharedFIFO(const FIFO& other): FIFO(other) {}
110
115 inline SharedFIFO(FIFO&& other) noexcept: FIFO(std::move(other)) {}
116
129 SharedFIFO(const SharedFIFO&) = delete;
130
135 SharedFIFO(SharedFIFO&& other) noexcept = default;
136
140 virtual ~SharedFIFO() noexcept = default;
141
147 SharedFIFO& operator=(const FIFO& other);
148
154 SharedFIFO& operator=(FIFO&& other) noexcept;
155
164 SharedFIFO& operator=(const SharedFIFO&) = delete;
165
172 SharedFIFO& operator=(SharedFIFO&&) noexcept;
173
181 bool operator==(const SharedFIFO& other) const noexcept;
182
188 inline bool operator!=(const SharedFIFO& other) const noexcept {
189 return !(*this == other);
190 }
191
196 virtual std::size_t AvailableBytes() const noexcept override;
197
202 void Clean() noexcept override;
203
208 virtual void Clear() noexcept override;
209
215 inline virtual const DataType& Data() const noexcept override {
216 return m_buffer;
217 }
218
225 virtual void Close() noexcept;
226
233 virtual bool Drop(const std::size_t& count) noexcept override;
234
242 virtual bool Empty() const noexcept override;
243
250 virtual bool EoF() const noexcept override;
251
256 bool HasError() const noexcept;
257
275 virtual std::string HexDump(const std::size_t& collumns = 0, const std::size_t& byte_limit = 0) const noexcept override;
276
285 inline virtual bool IsReadable() const noexcept override { return !m_error; }
286
293 inline virtual bool IsWritable() const noexcept override { return !m_closed && !m_error; }
294
304 virtual void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept override;
305
312 virtual void SetError() noexcept;
313
319 virtual std::size_t Size() const noexcept override;
320
321 protected:
322 bool m_closed {false};
323 bool m_error {false};
324 std::string m_error_message;
325
326 private:
327 mutable std::mutex m_mutex;
328 mutable std::condition_variable_any m_cv;
329
334 std::ostringstream HexDumpHeader() const noexcept override;
335
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;
356
367 void Wait(const std::size_t& n, std::unique_lock<std::mutex>& lock) const;
368
375 virtual bool WriteInternal(const std::size_t& count, const DataType& src) noexcept override;
376
383 virtual bool WriteInternal(const std::size_t& count, DataType&& src) noexcept override;
384 };
385}
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