StormByte C++ Library 0.0.9999
StormByte is a comprehensive, cross-platform C++ library aimed at easing system programming, configuration management, logging, and database handling tasks. This library provides a unified API that abstracts away the complexities and inconsistencies of different platforms (Windows, Linux).
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
StormByte::Buffer::Shared Class Reference

A thread-safe version of the Simple buffer. More...

#include <shared.hxx>

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

Public Member Functions

 Shared () noexcept
 Default constructor Initializes an empty shared buffer.
 
 Shared (const std::size_t &size)
 Constructor reserving initial size.
 
 Shared (const char *data, const std::size_t &length)
 Constructor.
 
 Shared (const std::string &data)
 Constructor.
 
 Shared (const Buffer::Data &data)
 Constructor.
 
 Shared (Buffer::Data &&data)
 Constructor.
 
 Shared (const std::span< const Byte > &data)
 Constructor.
 
 Shared (const Shared &other)
 Copy constructor.
 
 Shared (Shared &&other) noexcept
 Move constructor.
 
virtual ~Shared () noexcept override=default
 Destructor Cleans up the shared buffer.
 
Sharedoperator= (const Shared &other)
 Copy assignment operator.
 
Sharedoperator= (Shared &&other) noexcept
 Move assignment operator.
 
Sharedoperator<< (const Status &status)
 Sets the buffer status.
 
Sharedoperator<< (const Simple &buffer)
 Appends a buffer to the current buffer Thread-safe version of.
 
Sharedoperator<< (Simple &&buffer)
 Moves a buffer and appends to the current buffer Thread-safe version of.
 
Sharedoperator<< (const std::string &data)
 Appends a string to the current shared buffer Thread-safe version of.
 
Sharedoperator<< (const Buffer::Data &data)
 Appends a byte vector to the current shared buffer Thread-safe version of.
 
Sharedoperator<< (Buffer::Data &&data)
 Moves a byte vector and appends to the current shared buffer Thread-safe version of.
 
template<typename NumericType , typename = std::enable_if_t<std::is_arithmetic_v<std::decay_t<NumericType>>>>
Sharedoperator<< (const NumericType &value)
 Appends a numeric value to the current simple buffer.
 
Sharedoperator>> (Shared &buffer)
 Appends current shared buffer to target shared buffer Thread-safe version of.
 
virtual size_t AvailableBytes () const noexcept
 Gets the available bytes to read from current position.
 
size_t Capacity () const noexcept override
 Retrieves the capacity of the shared buffer Thread-safe version of.
 
void Clear () noexcept override
 Clears the shared buffer Thread-safe version of.
 
Buffer::Data Data () const noexcept override
 Retrieves a copy of the buffer data Thread-safe version of.
 
void Discard (const std::size_t &length, const Read::Position &mode) noexcept override
 Discards data from the buffer Thread-safe version of.
 
bool End () const noexcept override
 Checks if the read position is at the end Thread-safe version of.
 
bool Empty () const noexcept override
 Checks if the shared buffer is empty Thread-safe version of.
 
ExpectedData< BufferOverflowExtract (const size_t &length) override
 Extracts a specific size of data, taking ownership of the read data and removing it from the shared buffer.
 
Read::Status ExtractInto (const size_t &length, Shared &output) noexcept
 Extracts a specific size of data and moves it directly into the provided buffer.
 
bool HasEnoughData (const std::size_t &length) const override
 Checks if the shared buffer has enough data starting from the current read position Thread-safe version of.
 
std::string HexData (const std::size_t &column_size=16) const override
 Retrieves the stored value as a hexadecimal string Thread-safe version of.
 
bool IsEoF () const noexcept override
 Checks if the buffer is at the end of the file/data in case its write end have been closed.
 
bool IsReadable () const noexcept
 Checks if the buffer is readable.
 
bool IsWritable () const noexcept
 Checks if the buffer is writable.
 
void Lock ()
 Locks the shared buffer for exclusive access Prevents other threads from reading or writing to the buffer until Unlock() is called.
 
ExpectedByte< BufferOverflowPeek () const override
 Peeks at the next byte in the shared buffer without advancing the read position Thread-safe version of.
 
std::size_t Position () const noexcept override
 Retrieves the read position Thread-safe version of.
 
ExpectedData< BufferOverflowRead (const size_t &length) const override
 Reads a specific size of data starting from the current read position.
 
void Reserve (const std::size_t &size) override
 Reserves shared buffer size Thread-safe version of.
 
void Seek (const std::ptrdiff_t &position, const Read::Position &mode) const override
 Moves the read pointer within the shared buffer based on the specified position and mode Thread-safe version of.
 
std::size_t Size () const noexcept override
 Retrieves the length of the shared buffer Thread-safe version of.
 
enum Status Status () const noexcept
 Gets the buffer status.
 
void Unlock ()
 Unlocks the shared buffer, releasing exclusive access Allows other threads to access the buffer after it has been locked using Lock().
 
Write::Status Write (const Simple &buffer) override
 Writes a simple buffer to the current shared buffer Thread-safe version of.
 
Write::Status Write (Simple &&buffer) override
 Moves a simple buffer and writes it to the current shared buffer Thread-safe version of.
 
Write::Status Write (const std::string &data) override
 Writes a string to the current shared buffer Thread-safe version of.
 
Write::Status Write (const Buffer::Data &data) override
 Writes a byte vector to the current shared buffer Thread-safe version of.
 
Write::Status Write (Buffer::Data &&data) override
 Moves a byte vector and writes it to the current shared buffer Thread-safe version of.
 
- Public Member Functions inherited from StormByte::Buffer::Simple
 Simple () noexcept
 Default constructor Initializes an empty simple buffer.
 
 Simple (const std::size_t &size)
 Constructor reserving initial size.
 
 Simple (const char *data, const std::size_t &length)
 Constructor.
 
 Simple (const std::string &data)
 Constructor.
 
 Simple (const Data &data)
 Constructor.
 
 Simple (Data &&data)
 Constructor.
 
 Simple (const std::span< const Byte > &data)
 Constructor.
 
 Simple (const Simple &other)=default
 Copy constructor.
 
 Simple (Simple &&other) noexcept=default
 Move constructor.
 
virtual ~Simple () noexcept=default
 Destructor Cleans up the simple buffer.
 
Simpleoperator= (const Simple &other)=default
 Copy assignment operator.
 
Simpleoperator= (Simple &&other) noexcept=default
 Move assignment operator.
 
template<typename NumericType , typename = std::enable_if_t<std::is_arithmetic_v<std::decay_t<NumericType>>>>
Simpleoperator<< (const NumericType &value)
 Appends a numeric value to the current simple buffer.
 
virtual Simpleoperator>> (Simple &buffer)
 Appends current simple buffer to target simple buffer.
 
virtual Read::Status ExtractInto (const size_t &length, Simple &output) noexcept
 Extracts a specific size of data and moves it directly into the provided buffer.
 
Read::Status Process (const std::size_t &length, Processor function, Simple &output) noexcept
 Extracts, processes, and stores the results in the provided buffer.
 
const std::span< const Byte > Span () const noexcept
 Retrieves a const view (span) to the stored value.
 
std::span< Byte > Span () noexcept
 Retrieves a view (span) to the stored value.
 

Protected Member Functions

Read::Status Wait (const std::size_t length) const noexcept
 Waits for a specific amount of data to become available in the buffer.
 

Protected Attributes

std::shared_mutex m_data_mutex
 Mutex for thread safety.
 
std::atomic< enum Statusm_status
 Buffer status.
 
- Protected Attributes inherited from StormByte::Buffer::Simple
std::vector< std::byte > m_data
 Stored value.
 
std::size_t m_position
 Read position.
 
std::size_t m_minimum_chunk_size
 Minimum chunk size for buffer operations.
 

Detailed Description

A thread-safe version of the Simple buffer.

Forward declaration of the Shared class.

The Shared class extends the Simple buffer to provide thread-safe operations for storing and manipulating byte data. It ensures data integrity in multi-threaded environments by using a std::shared_mutex for synchronization.

Key Features:

This class is ideal for scenarios where multiple threads need to read from or write to a shared buffer while maintaining data integrity and avoiding race conditions.

Constructor & Destructor Documentation

◆ Shared() [1/8]

StormByte::Buffer::Shared::Shared ( const std::size_t &  size)
explicit

Constructor reserving initial size.

Parameters
sizeSize of the buffer to reserve.

◆ Shared() [2/8]

StormByte::Buffer::Shared::Shared ( const char data,
const std::size_t &  length 
)

Constructor.

Parameters
dataPointer to the data to set.
lengthLength of the data.

◆ Shared() [3/8]

StormByte::Buffer::Shared::Shared ( const std::string &  data)

Constructor.

Parameters
dataString to set as buffer content.

◆ Shared() [4/8]

StormByte::Buffer::Shared::Shared ( const Buffer::Data &  data)

Constructor.

Parameters
dataVector of bytes to set as buffer content.

◆ Shared() [5/8]

StormByte::Buffer::Shared::Shared ( Buffer::Data &&  data)

Constructor.

Parameters
dataVector of bytes to move into the buffer.

◆ Shared() [6/8]

StormByte::Buffer::Shared::Shared ( const std::span< const Byte > &  data)

Constructor.

Parameters
dataSpan of bytes to set as buffer content.

◆ Shared() [7/8]

StormByte::Buffer::Shared::Shared ( const Shared other)

Copy constructor.

Parameters
otherShared buffer to copy from.

◆ Shared() [8/8]

StormByte::Buffer::Shared::Shared ( Shared &&  other)
noexcept

Move constructor.

Parameters
otherShared buffer to move from.

Member Function Documentation

◆ AvailableBytes()

virtual size_t StormByte::Buffer::Shared::AvailableBytes ( ) const
virtualnoexcept

Gets the available bytes to read from current position.

Returns
Number of bytes available to read.

Reimplemented from StormByte::Buffer::Simple.

◆ Capacity()

size_t StormByte::Buffer::Shared::Capacity ( ) const
overridevirtualnoexcept

Retrieves the capacity of the shared buffer Thread-safe version of.

See also
Simple::Capacity.

Reimplemented from StormByte::Buffer::Simple.

◆ Clear()

void StormByte::Buffer::Shared::Clear ( )
overridevirtualnoexcept

Clears the shared buffer Thread-safe version of.

See also
Simple::Clear.

Reimplemented from StormByte::Buffer::Simple.

◆ Data()

Buffer::Data StormByte::Buffer::Shared::Data ( ) const
overridevirtualnoexcept

Retrieves a copy of the buffer data Thread-safe version of.

See also
Simple::Data.

Reimplemented from StormByte::Buffer::Simple.

◆ Discard()

void StormByte::Buffer::Shared::Discard ( const std::size_t &  length,
const Read::Position &  mode 
)
overridevirtualnoexcept

Discards data from the buffer Thread-safe version of.

See also
Simple::Discard.

Reimplemented from StormByte::Buffer::Simple.

◆ Empty()

bool StormByte::Buffer::Shared::Empty ( ) const
overridevirtualnoexcept

Checks if the shared buffer is empty Thread-safe version of.

See also
Simple::Empty.

Reimplemented from StormByte::Buffer::Simple.

◆ End()

bool StormByte::Buffer::Shared::End ( ) const
overridevirtualnoexcept

Checks if the read position is at the end Thread-safe version of.

See also
Simple::End.

Reimplemented from StormByte::Buffer::Simple.

◆ Extract()

ExpectedData< BufferOverflow > StormByte::Buffer::Shared::Extract ( const size_t length)
overridevirtual

Extracts a specific size of data, taking ownership of the read data and removing it from the shared buffer.

Unlike Simple::Extract, this method waits for the requested data to become available while the buffer is IsReadable. If the buffer becomes unreadable (e.g., due to EoF or an error), it returns a BufferOverflow error.

Parameters
lengthLength of the data to read and remove from the shared buffer.
Returns
ExpectedDataType containing the requested data, or an Unexpected with a BufferOverflow error if insufficient data exists and the buffer is no longer readable.
See also
Simple::Extract

Reimplemented from StormByte::Buffer::Simple.

◆ ExtractInto()

Read::Status StormByte::Buffer::Shared::ExtractInto ( const size_t length,
Shared output 
)
noexcept

Extracts a specific size of data and moves it directly into the provided buffer.

Unlike Simple::ExtractInto, this method waits for the requested data to become available while the buffer is IsReadable. If the buffer becomes unreadable (e.g., due to EoF or an error), it returns Read::Status::Error.

Parameters
lengthLength of the data to extract.
outputBuffer where the extracted data will be moved.
Returns
Read::Status indicating the success or failure of the operation.
See also
Simple::ExtractInto

◆ HasEnoughData()

bool StormByte::Buffer::Shared::HasEnoughData ( const std::size_t &  length) const
overridevirtual

Checks if the shared buffer has enough data starting from the current read position Thread-safe version of.

See also
Simple::HasEnoughData.

Reimplemented from StormByte::Buffer::Simple.

◆ HexData()

std::string StormByte::Buffer::Shared::HexData ( const std::size_t &  column_size = 16) const
overridevirtual

Retrieves the stored value as a hexadecimal string Thread-safe version of.

See also
Simple::HexData.

Reimplemented from StormByte::Buffer::Simple.

◆ IsEoF()

bool StormByte::Buffer::Shared::IsEoF ( ) const
overridevirtualnoexcept

Checks if the buffer is at the end of the file/data in case its write end have been closed.

Returns
True if the buffer is at the end and write closed, false otherwise.

Reimplemented from StormByte::Buffer::Simple.

◆ IsReadable()

bool StormByte::Buffer::Shared::IsReadable ( ) const
noexcept

Checks if the buffer is readable.

A buffer is considered readable if it is not in an Closed or Error state.

Returns
True if the buffer is readable, false otherwise.

◆ IsWritable()

bool StormByte::Buffer::Shared::IsWritable ( ) const
noexcept

Checks if the buffer is writable.

A buffer is considered writable if it is not in an Closed or Error state.

Returns
True if the buffer is writable, false otherwise.

◆ operator<<() [1/7]

Shared & StormByte::Buffer::Shared::operator<< ( Buffer::Data &&  data)
virtual

Moves a byte vector and appends to the current shared buffer Thread-safe version of.

See also
Simple::operator<<.

Reimplemented from StormByte::Buffer::Simple.

◆ operator<<() [2/7]

Shared & StormByte::Buffer::Shared::operator<< ( const Buffer::Data &  data)
virtual

Appends a byte vector to the current shared buffer Thread-safe version of.

See also
Simple::operator<<.

Reimplemented from StormByte::Buffer::Simple.

◆ operator<<() [3/7]

template<typename NumericType , typename = std::enable_if_t<std::is_arithmetic_v<std::decay_t<NumericType>>>>
Shared & StormByte::Buffer::Shared::operator<< ( const NumericType value)
inline

Appends a numeric value to the current simple buffer.

This templated method allows appending numeric values (e.g., integers, floating-point numbers) to the buffer. The numeric value is serialized into its binary representation and appended to the buffer.

Template Parameters
NumericTypeThe type of the numeric value to append.
Parameters
valueThe numeric value to append.
Returns
Reference to the updated simple buffer.

◆ operator<<() [4/7]

Shared & StormByte::Buffer::Shared::operator<< ( const Simple buffer)
virtual

Appends a buffer to the current buffer Thread-safe version of.

See also
Simple::operator<<.

Reimplemented from StormByte::Buffer::Simple.

◆ operator<<() [5/7]

Shared & StormByte::Buffer::Shared::operator<< ( const Status status)

Sets the buffer status.

See also
Buffer::Status

◆ operator<<() [6/7]

Shared & StormByte::Buffer::Shared::operator<< ( const std::string &  data)
virtual

Appends a string to the current shared buffer Thread-safe version of.

See also
Simple::operator<<.

Reimplemented from StormByte::Buffer::Simple.

◆ operator<<() [7/7]

Shared & StormByte::Buffer::Shared::operator<< ( Simple &&  buffer)
virtual

Moves a buffer and appends to the current buffer Thread-safe version of.

See also
Simple::operator<<.

Reimplemented from StormByte::Buffer::Simple.

◆ operator=() [1/2]

Shared & StormByte::Buffer::Shared::operator= ( const Shared other)

Copy assignment operator.

Parameters
otherShared buffer to copy from.
Returns
Reference to the updated shared buffer.

◆ operator=() [2/2]

Shared & StormByte::Buffer::Shared::operator= ( Shared &&  other)
noexcept

Move assignment operator.

Parameters
otherShared buffer to move from.
Returns
Reference to the updated shared buffer.

◆ operator>>()

Shared & StormByte::Buffer::Shared::operator>> ( Shared buffer)

Appends current shared buffer to target shared buffer Thread-safe version of.

See also
Simple::operator>>.

◆ Peek()

ExpectedByte< BufferOverflow > StormByte::Buffer::Shared::Peek ( ) const
overridevirtual

Peeks at the next byte in the shared buffer without advancing the read position Thread-safe version of.

See also
Simple::Peek.

Reimplemented from StormByte::Buffer::Simple.

◆ Position()

std::size_t StormByte::Buffer::Shared::Position ( ) const
overridevirtualnoexcept

Retrieves the read position Thread-safe version of.

See also
Simple::Position.

Reimplemented from StormByte::Buffer::Simple.

◆ Read()

ExpectedData< BufferOverflow > StormByte::Buffer::Shared::Read ( const size_t length) const
overridevirtual

Reads a specific size of data starting from the current read position.

Unlike Simple::Read, this method waits for the requested data to become available while the buffer is IsReadable. If the buffer becomes unreadable (e.g., due to EoF or an error), it returns a BufferOverflow error.

Parameters
lengthLength of the data to read.
Returns
ExpectedDataType containing a copy of the requested data, or an Unexpected with a BufferOverflow error if insufficient data exists and the buffer is no longer readable.
See also
Simple::Read

Reimplemented from StormByte::Buffer::Simple.

◆ Reserve()

void StormByte::Buffer::Shared::Reserve ( const std::size_t &  size)
overridevirtual

Reserves shared buffer size Thread-safe version of.

See also
Simple::Reserve.

Reimplemented from StormByte::Buffer::Simple.

◆ Seek()

void StormByte::Buffer::Shared::Seek ( const std::ptrdiff_t &  position,
const Read::Position &  mode 
) const
overridevirtual

Moves the read pointer within the shared buffer based on the specified position and mode Thread-safe version of.

See also
Simple::Seek.

Reimplemented from StormByte::Buffer::Simple.

◆ Size()

std::size_t StormByte::Buffer::Shared::Size ( ) const
overridevirtualnoexcept

Retrieves the length of the shared buffer Thread-safe version of.

See also
Simple::Size.

Reimplemented from StormByte::Buffer::Simple.

◆ Wait()

Read::Status StormByte::Buffer::Shared::Wait ( const std::size_t  length) const
protectednoexcept

Waits for a specific amount of data to become available in the buffer.

This function blocks until the requested amount of data is available in the buffer or until the buffer is marked as EoF (End of File) or Error.

Behavior:

  • If the required data becomes available, the function returns Read::Status::Success.
  • If the buffer is marked as EoF or encounters an error while waiting, the function returns Read::Status::Error.
Parameters
lengthThe number of bytes to wait for.
Returns
Read::Status indicating the result of the wait operation.

◆ Write() [1/5]

Write::Status StormByte::Buffer::Shared::Write ( Buffer::Data &&  data)
overridevirtual

Moves a byte vector and writes it to the current shared buffer Thread-safe version of.

See also
Simple::Write.
Parameters
dataByte vector to write.
Returns
Write::Status of the operation.

Reimplemented from StormByte::Buffer::Simple.

◆ Write() [2/5]

Write::Status StormByte::Buffer::Shared::Write ( const Buffer::Data &  data)
overridevirtual

Writes a byte vector to the current shared buffer Thread-safe version of.

See also
Simple::Write.
Parameters
dataByte vector to write.
Returns
Write::Status of the operation.

Reimplemented from StormByte::Buffer::Simple.

◆ Write() [3/5]

Write::Status StormByte::Buffer::Shared::Write ( const Simple buffer)
overridevirtual

Writes a simple buffer to the current shared buffer Thread-safe version of.

See also
Simple::Write.
Parameters
bufferSimple buffer to write.
Returns
Write::Status of the operation.

Reimplemented from StormByte::Buffer::Simple.

◆ Write() [4/5]

Write::Status StormByte::Buffer::Shared::Write ( const std::string &  data)
overridevirtual

Writes a string to the current shared buffer Thread-safe version of.

See also
Simple::Write.
Parameters
dataString to write.
Returns
Write::Status of the operation.

Reimplemented from StormByte::Buffer::Simple.

◆ Write() [5/5]

Write::Status StormByte::Buffer::Shared::Write ( Simple &&  buffer)
overridevirtual

Moves a simple buffer and writes it to the current shared buffer Thread-safe version of.

See also
Simple::Write.
Parameters
bufferSimple buffer to write.
Returns
Write::Status of the operation.

Reimplemented from StormByte::Buffer::Simple.


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