StormByte C++ Library: Buffer module 0.0.9999
StormByte-Buffer is a StormByte library module for handling buffers
Loading...
Searching...
No Matches
Public Member Functions | Friends | List of all members
StormByte::Buffer::Consumer Class Referencefinal

Read-only interface for consuming data from a shared FIFO buffer. More...

#include <consumer.hxx>

Inheritance diagram for StormByte::Buffer::Consumer:
Inheritance graph
[legend]
Collaboration diagram for StormByte::Buffer::Consumer:
Collaboration graph
[legend]

Public Member Functions

 Consumer (const Consumer &other) noexcept
 Copy constructor.
 
 Consumer (Consumer &&other) noexcept
 Move constructor.
 
 ~Consumer () noexcept=default
 Destructor.
 
Consumeroperator= (const Consumer &other) noexcept
 Copy assignment operator.
 
Consumeroperator= (Consumer &&other) noexcept
 Move assignment operator.
 
bool operator== (const Consumer &other) const noexcept
 Equality comparison.
 
bool operator!= (const Consumer &other) const noexcept
 Inequality comparison.
 
std::size_t AvailableBytes () const noexcept override
 Gets available bytes for reading.
 
void Clean () noexcept override
 Clean buffer data from start to read position.
 
void Clear () noexcept override
 Clear all buffer contents.
 
void Close () noexcept
 Thread-safe close for further writes.
 
bool Drop (const std::size_t &count) noexcept override
 Drop bytes in the buffer.
 
bool Empty () const noexcept override
 Check if the buffer is empty.
 
bool EoF () const noexcept override
 Check if the reader has reached end-of-file.
 
bool Extract (const std::size_t &count, DataType &outBuffer) noexcept override
 Destructive read that removes data from the buffer into an existing vector.
 
bool Extract (const std::size_t &count, WriteOnly &outBuffer) noexcept
 Destructive read that removes data from the buffer into a FIFO.
 
void ExtractUntilEoF (DataType &outBuffer) noexcept override
 Read all bytes until end-of-file into an existing buffer.
 
void ExtractUntilEoF (WriteOnly &outBuffer) noexcept override
 Read all bytes until end-of-file into a WriteOnly buffer.
 
bool IsReadable () const noexcept override
 Check if the buffer is readable (not in error state).
 
bool IsWritable () const noexcept
 Check if the buffer is writable (not closed and not in error state).
 
bool HasError () const noexcept
 Check if the buffer is in an error state.
 
bool Read (const std::size_t &count, DataType &outBuffer) const noexcept override
 Read bytes into an existing buffer.
 
bool Read (const std::size_t &count, WriteOnly &outBuffer) const noexcept override
 Read bytes into a WriteOnly buffer.
 
void ReadUntilEoF (DataType &outBuffer) const noexcept override
 Read all bytes until end-of-file into an existing buffer.
 
void ReadUntilEoF (WriteOnly &outBuffer) const noexcept override
 Read all bytes until end-of-file into a WriteOnly buffer.
 
bool Peek (const std::size_t &count, DataType &outBuffer) const noexcept override
 Non-destructive peek at buffer data without advancing read position.
 
bool Peek (const std::size_t &count, WriteOnly &outBuffer) const noexcept override
 Non-destructive peek at buffer data without advancing read position.
 
void Seek (const std::ptrdiff_t &offset, const Position &mode) const noexcept
 Move the read position for non-destructive reads.
 
std::size_t Size () const noexcept override
 Get the current number of bytes stored in the buffer.
 
- Public Member Functions inherited from StormByte::Buffer::ReadOnly
 ReadOnly () noexcept
 Construct ReadOnly.
 
 ReadOnly (const DataType &data) noexcept
 Construct ReadOnly.
 
 ReadOnly (DataType &&data) noexcept
 Construct ReadOnly with initial data using move semantics.
 
 ReadOnly (const ReadOnly &) noexcept=default
 Copy construct deleted.
 
 ReadOnly (ReadOnly &&) noexcept=default
 Move construct deleted.
 
virtual ~ReadOnly () noexcept=default
 Virtual destructor.
 
ReadOnlyoperator= (const ReadOnly &)=default
 Copy assign deleted.
 
ReadOnlyoperator= (ReadOnly &&) noexcept=default
 Move assign deleted.
 
virtual const DataType & Data () const noexcept
 Access the internal data buffer.
 
bool Extract (DataType &outBuffer) noexcept
 Destructive read that removes all data from the buffer into an existing vector.
 
bool Extract (WriteOnly &outBuffer) noexcept
 Destructive read that removes all data from the buffer into a FIFO.
 
bool Read (DataType &outBuffer) const noexcept
 Read bytes into an existing buffer.
 
bool Read (WriteOnly &outBuffer) const noexcept
 Read bytes into a WriteOnly buffer.
 
- Public Member Functions inherited from StormByte::Buffer::Generic
 Generic () noexcept=default
 Construct Generic.
 
 Generic (const DataType &data) noexcept
 Construct Generic.
 
 Generic (DataType &&data) noexcept
 Construct Generic with initial data using move semantics.
 
 Generic (const Generic &) noexcept=default
 Copy construct deleted.
 
 Generic (Generic &&) noexcept=default
 Move construct deleted.
 
virtual ~Generic () noexcept=0
 Virtual destructor.
 
Genericoperator= (const Generic &other)=default
 Copy assign deleted.
 
Genericoperator= (Generic &&) noexcept=default
 Move assign deleted.
 

Friends

class Producer
 

Additional Inherited Members

- Static Protected Member Functions inherited from StormByte::Buffer::Generic
template<std::ranges::input_range Src>
requires (!std::is_class_v<std::remove_cv_t<std::ranges::range_value_t<Src>>>) && requires(std::ranges::range_value_t<Src> v) { static_cast<std::byte>(v); }
static DataType DataConvert (const Src &src) noexcept
 Convert various source types into the library DataType.
 
template<std::ranges::input_range Src>
requires (!std::is_class_v<std::remove_cv_t<std::ranges::range_value_t<Src>>>) && requires(std::ranges::range_value_t<Src> v) { static_cast<std::byte>(v); }
static DataType DataConvert (Src &&src) noexcept
 
static DataType DataConvert (std::string_view sv) noexcept
 Convert a std::string_view to DataType.
 
static DataType DataConvert (const char *s) noexcept
 Convert a null-terminated C string to DataType.
 
- Protected Attributes inherited from StormByte::Buffer::Generic
DataType m_buffer
 Internal buffer storage.
 

Detailed Description

Read-only interface for consuming data from a shared FIFO buffer.

Overview
Consumer provides a read-only interface to a SharedFIFO buffer. Multiple Consumer instances can share the same underlying buffer, allowing multiple consumers to read data concurrently in a thread-safe manner. Consumers can only be created through a Producer instance.
Thread safety
All read operations are thread-safe as they delegate to the underlying SharedFIFO which is fully thread-safe. Multiple consumers can safely read from the same buffer concurrently.
Blocking behavior
  • Read() blocks until the requested number of bytes are available or the buffer becomes unreadable (closed or error). If count is 0, returns all available data from the current read position without blocking.
  • Extract() blocks until the requested number of bytes are available or the buffer becomes unreadable (closed or error). If count is 0, returns all available data immediately and clears the buffer.
Producer-Consumer relationship
Consumer instances cannot be created directly. They must be obtained from a Producer using Producer::Consumer(). This ensures proper buffer sharing between producers and consumers.
See also
Producer

Constructor & Destructor Documentation

◆ Consumer() [1/2]

StormByte::Buffer::Consumer::Consumer ( const Consumer other)
inlinenoexcept

Copy constructor.

Parameters
otherSource Consumer to copy from.

Copies the Consumer instance, sharing the same underlying buffer. Both instances will read from the same SharedFIFO and share the read position state.

◆ Consumer() [2/2]

StormByte::Buffer::Consumer::Consumer ( Consumer &&  other)
inlinenoexcept

Move constructor.

Parameters
otherSource Consumer to move from.

Transfers ownership of the buffer from the moved-from Consumer.

Member Function Documentation

◆ AvailableBytes()

std::size_t StormByte::Buffer::Consumer::AvailableBytes ( ) const
inlineoverridevirtualnoexcept

Gets available bytes for reading.

Returns
Number of bytes available from the current read position.

Implements StormByte::Buffer::ReadOnly.

◆ Clean()

void StormByte::Buffer::Consumer::Clean ( )
inlineoverridevirtualnoexcept

Clean buffer data from start to read position.

See also
Size(), Empty()

Implements StormByte::Buffer::ReadOnly.

◆ Clear()

void StormByte::Buffer::Consumer::Clear ( )
inlineoverridevirtualnoexcept

Clear all buffer contents.

Removes all data from the buffer, resets head/tail/read positions, and restores capacity to the initial value requested in the constructor.

See also
Size(), Empty()

Implements StormByte::Buffer::ReadOnly.

◆ Close()

void StormByte::Buffer::Consumer::Close ( )
inlinenoexcept

Thread-safe close for further writes.

Marks buffer as closed, notifies all waiting threads. Subsequent writes are ignored. The buffer remains readable until all data is consumed.

See also
FIFO::Close(), IsWritable()

◆ Drop()

bool StormByte::Buffer::Consumer::Drop ( const std::size_t &  count)
inlineoverridevirtualnoexcept

Drop bytes in the buffer.

Parameters
countNumber of bytes to drop.
Returns
true if the bytes were successfully dropped, false otherwise.
See also
Read()

Implements StormByte::Buffer::ReadOnly.

◆ Empty()

bool StormByte::Buffer::Consumer::Empty ( ) const
inlineoverridevirtualnoexcept

Check if the buffer is empty.

Returns
true if the buffer contains no data, false otherwise.
See also
Size()

Implements StormByte::Buffer::ReadOnly.

◆ EoF()

bool StormByte::Buffer::Consumer::EoF ( ) const
inlineoverridevirtualnoexcept

Check if the reader has reached end-of-file.

Returns
true if buffer is closed or in error state and no bytes available.

Returns true when the buffer has been closed or set to error and there are no available bytes remaining.

Implements StormByte::Buffer::ReadOnly.

◆ Extract() [1/2]

bool StormByte::Buffer::Consumer::Extract ( const std::size_t &  count,
DataType &  outBuffer 
)
inlineoverridevirtualnoexcept

Destructive read that removes data from the buffer into an existing vector.

Parameters
countNumber of bytes to extract; 0 extracts all available.
outBufferVector to fill with extracted bytes; resized as needed.
Returns
bool indicating success or failure.
Note
For base class is the same than Read

Implements StormByte::Buffer::ReadOnly.

◆ Extract() [2/2]

bool StormByte::Buffer::Consumer::Extract ( const std::size_t &  count,
WriteOnly outBuffer 
)
inlinevirtualnoexcept

Destructive read that removes data from the buffer into a FIFO.

Parameters
countNumber of bytes to extract; 0 extracts all available.
outBufferWriteOnly to fill with extracted bytes; resized as needed.
Returns
bool indicating success or failure.

Implements StormByte::Buffer::ReadOnly.

◆ ExtractUntilEoF() [1/2]

void StormByte::Buffer::Consumer::ExtractUntilEoF ( DataType &  outBuffer)
inlineoverridevirtualnoexcept

Read all bytes until end-of-file into an existing buffer.

Parameters
outBufferVector to fill with read bytes; resized as needed.

Implements StormByte::Buffer::ReadOnly.

◆ ExtractUntilEoF() [2/2]

void StormByte::Buffer::Consumer::ExtractUntilEoF ( WriteOnly outBuffer)
inlineoverridevirtualnoexcept

Read all bytes until end-of-file into a WriteOnly buffer.

Parameters
outBufferWriteOnly to fill with read bytes; resized as needed.

Implements StormByte::Buffer::ReadOnly.

◆ HasError()

bool StormByte::Buffer::Consumer::HasError ( ) const
inlinenoexcept

Check if the buffer is in an error state.

Returns
true if the buffer is in error state, false otherwise.

◆ IsReadable()

bool StormByte::Buffer::Consumer::IsReadable ( ) const
inlineoverridevirtualnoexcept

Check if the buffer is readable (not in error state).

Returns
true if readable, false if buffer is in error state.

A buffer becomes unreadable when SetError() is called. Use in combination with AvailableBytes() to check if there is data pending to read.

See also
SetError(), IsWritable(), AvailableBytes(), EoF()

Implements StormByte::Buffer::ReadOnly.

◆ IsWritable()

bool StormByte::Buffer::Consumer::IsWritable ( ) const
inlinenoexcept

Check if the buffer is writable (not closed and not in error state).

Returns
true if writable, false if closed or in error state.

A buffer becomes unwritable when Close() or SetError() is called.

See also
Close(), SetError(), IsReadable()

◆ operator!=()

bool StormByte::Buffer::Consumer::operator!= ( const Consumer other) const
inlinenoexcept

Inequality comparison.

Returns
true if Consumers have different underlying buffers.

◆ operator=() [1/2]

Consumer & StormByte::Buffer::Consumer::operator= ( const Consumer other)
inlinenoexcept

Copy assignment operator.

Returns
Reference to this Consumer.

◆ operator=() [2/2]

Consumer & StormByte::Buffer::Consumer::operator= ( Consumer &&  other)
inlinenoexcept

Move assignment operator.

Parameters
otherSource Consumer to move from.
Returns
Reference to this Consumer.

◆ operator==()

bool StormByte::Buffer::Consumer::operator== ( const Consumer other) const
inlinenoexcept

Equality comparison.

Returns
true if both Consumers share the same underlying buffer.

◆ Peek() [1/2]

bool StormByte::Buffer::Consumer::Peek ( const std::size_t &  count,
DataType &  outBuffer 
) const
inlineoverridevirtualnoexcept

Non-destructive peek at buffer data without advancing read position.

Parameters
countNumber of bytes to peek; 0 peeks all available from read position.
Returns
bool indicating success or failure.

Similar to Read(), but does not advance the read position. Allows inspecting upcoming data without consuming it.

Semantics:

  • If count == 0: the call returns all available bytes. If no bytes are available, a ReadError is returned.
  • If count > 0: the call returns exactly count bytes when that many bytes are available. If zero bytes are available, or if count is greater than the number of available bytes, a ReadError is returned.
See also
Read(), Seek()

Implements StormByte::Buffer::ReadOnly.

◆ Peek() [2/2]

bool StormByte::Buffer::Consumer::Peek ( const std::size_t &  count,
WriteOnly outBuffer 
) const
inlineoverridevirtualnoexcept

Non-destructive peek at buffer data without advancing read position.

Parameters
countNumber of bytes to peek; 0 peeks all available from read position.
Returns
bool indicating success or failure.

Similar to Read(), but does not advance the read position. Allows inspecting upcoming data without consuming it.

Semantics:

  • If count == 0: the call returns all available bytes. If no bytes are available, a ReadError is returned.
  • If count > 0: the call returns exactly count bytes when that many bytes are available. If zero bytes are available, or if count is greater than the number of available bytes, a ReadError is returned.
See also
Read(), Seek()

Implements StormByte::Buffer::ReadOnly.

◆ Read() [1/2]

bool StormByte::Buffer::Consumer::Read ( const std::size_t &  count,
DataType &  outBuffer 
) const
inlineoverridevirtualnoexcept

Read bytes into an existing buffer.

Parameters
countNumber of bytes to read; 0 reads all available from read position.
outBufferVector to fill with read bytes; resized as needed.
Returns
bool indicating success or failure.

Implements StormByte::Buffer::ReadOnly.

◆ Read() [2/2]

bool StormByte::Buffer::Consumer::Read ( const std::size_t &  count,
WriteOnly outBuffer 
) const
inlineoverridevirtualnoexcept

Read bytes into a WriteOnly buffer.

Parameters
countNumber of bytes to read; 0 reads all available from read position.
outBufferWriteOnly to fill with read bytes; resized as needed.
Returns
bool indicating success or failure.

Implements StormByte::Buffer::ReadOnly.

◆ ReadUntilEoF() [1/2]

void StormByte::Buffer::Consumer::ReadUntilEoF ( DataType &  outBuffer) const
inlineoverridevirtualnoexcept

Read all bytes until end-of-file into an existing buffer.

Parameters
outBufferVector to fill with read bytes; resized as needed.

Implements StormByte::Buffer::ReadOnly.

◆ ReadUntilEoF() [2/2]

void StormByte::Buffer::Consumer::ReadUntilEoF ( WriteOnly outBuffer) const
inlineoverridevirtualnoexcept

Read all bytes until end-of-file into a WriteOnly buffer.

Parameters
outBufferWriteOnly to fill with read bytes; resized as needed.

Implements StormByte::Buffer::ReadOnly.

◆ Seek()

void StormByte::Buffer::Consumer::Seek ( const std::ptrdiff_t &  offset,
const Position &  mode 
) const
inlinevirtualnoexcept

Move the read position for non-destructive reads.

Parameters
positionThe offset value to apply.
modeUnused for base class; included for API consistency.

Changes where subsequent Read() operations will start reading from. Position is clamped to [0, Size()]. Does not affect stored data.

See also
Read(), Position If Position is set to Absolute and offset is negative the operation is noop

Implements StormByte::Buffer::ReadOnly.

◆ Size()

std::size_t StormByte::Buffer::Consumer::Size ( ) const
inlineoverridevirtualnoexcept

Get the current number of bytes stored in the buffer.

Returns
The total number of bytes available for reading.
See also
Capacity(), Empty()

Implements StormByte::Buffer::ReadOnly.


The documentation for this class was generated from the following file: