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).
|
A thread-safe version of the Simple
buffer.
More...
#include <shared.hxx>
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. | |
Shared & | operator= (const Shared &other) |
Copy assignment operator. | |
Shared & | operator= (Shared &&other) noexcept |
Move assignment operator. | |
Shared & | operator<< (const Status &status) |
Sets the buffer status. | |
Shared & | operator<< (const Simple &buffer) |
Appends a buffer to the current buffer Thread-safe version of. | |
Shared & | operator<< (Simple &&buffer) |
Moves a buffer and appends to the current buffer Thread-safe version of. | |
Shared & | operator<< (const std::string &data) |
Appends a string to the current shared buffer Thread-safe version of. | |
Shared & | operator<< (const Buffer::Data &data) |
Appends a byte vector to the current shared buffer Thread-safe version of. | |
Shared & | operator<< (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>>>> | |
Shared & | operator<< (const NumericType &value) |
Appends a numeric value to the current simple buffer. | |
Shared & | operator>> (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< BufferOverflow > | Extract (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< BufferOverflow > | Peek () 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< BufferOverflow > | Read (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. | |
![]() | |
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. | |
Simple & | operator= (const Simple &other)=default |
Copy assignment operator. | |
Simple & | operator= (Simple &&other) noexcept=default |
Move assignment operator. | |
template<typename NumericType , typename = std::enable_if_t<std::is_arithmetic_v<std::decay_t<NumericType>>>> | |
Simple & | operator<< (const NumericType &value) |
Appends a numeric value to the current simple buffer. | |
virtual Simple & | operator>> (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 Status > | m_status |
Buffer status. | |
![]() | |
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. | |
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:
std::shared_mutex
. Reads allow concurrent access, while writes are exclusive.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.
|
explicit |
Constructor reserving initial size.
size | Size of the buffer to reserve. |
Constructor.
data | Pointer to the data to set. |
length | Length of the data. |
StormByte::Buffer::Shared::Shared | ( | const std::string & | data | ) |
Constructor.
data | String to set as buffer content. |
StormByte::Buffer::Shared::Shared | ( | const Buffer::Data & | data | ) |
Constructor.
data | Vector of bytes to set as buffer content. |
StormByte::Buffer::Shared::Shared | ( | Buffer::Data && | data | ) |
Constructor.
data | Vector of bytes to move into the buffer. |
Constructor.
data | Span of bytes to set as buffer content. |
Copy constructor.
other | Shared buffer to copy from. |
|
noexcept |
Move constructor.
other | Shared buffer to move from. |
Gets the available bytes to read from current position.
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtualnoexcept |
Retrieves the capacity of the shared buffer Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtualnoexcept |
Clears the shared buffer Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtualnoexcept |
Retrieves a copy of the buffer data Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtualnoexcept |
Discards data from the buffer Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtualnoexcept |
Checks if the shared buffer is empty Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtualnoexcept |
Checks if the read position is at the end Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
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.
length | Length of the data to read and remove from the shared buffer. |
ExpectedDataType
containing the requested data, or an Unexpected
with a BufferOverflow
error if insufficient data exists and the buffer is no longer readable. Reimplemented from StormByte::Buffer::Simple.
|
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
.
length | Length of the data to extract. |
output | Buffer where the extracted data will be moved. |
Read::Status
indicating the success or failure of the operation. Checks if the shared buffer has enough data starting from the current read position Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtual |
Retrieves the stored value as a hexadecimal string Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtualnoexcept |
Checks if the buffer is at the end of the file/data in case its write end have been closed.
Reimplemented from StormByte::Buffer::Simple.
|
noexcept |
Checks if the buffer is readable.
A buffer is considered readable if it is not in an Closed
or Error
state.
|
noexcept |
Checks if the buffer is writable.
A buffer is considered writable if it is not in an Closed
or Error
state.
|
virtual |
Moves a byte vector and appends to the current shared buffer Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
Appends a byte vector to the current shared buffer Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
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.
NumericType | The type of the numeric value to append. |
value | The numeric value to append. |
Appends a buffer to the current buffer Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
Sets the buffer status.
Appends a string to the current shared buffer Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
Moves a buffer and appends to the current buffer Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
Copy assignment operator.
other | Shared buffer to copy from. |
Move assignment operator.
other | Shared buffer to move from. |
Appends current shared buffer to target shared buffer Thread-safe version of.
|
overridevirtual |
Peeks at the next byte in the shared buffer without advancing the read position Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtualnoexcept |
Retrieves the read position Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
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.
length | Length of the data to read. |
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. Reimplemented from StormByte::Buffer::Simple.
Reserves shared buffer size Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtual |
Moves the read pointer within the shared buffer based on the specified position and mode Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtualnoexcept |
Retrieves the length of the shared buffer Thread-safe version of.
Reimplemented from StormByte::Buffer::Simple.
|
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:
Read::Status::Success
.EoF
or encounters an error while waiting, the function returns Read::Status::Error
.length | The number of bytes to wait for. |
Read::Status
indicating the result of the wait operation.
|
overridevirtual |
Moves a byte vector and writes it to the current shared buffer Thread-safe version of.
data | Byte vector to write. |
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtual |
Writes a byte vector to the current shared buffer Thread-safe version of.
data | Byte vector to write. |
Reimplemented from StormByte::Buffer::Simple.
Writes a simple buffer to the current shared buffer Thread-safe version of.
buffer | Simple buffer to write. |
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtual |
Writes a string to the current shared buffer Thread-safe version of.
data | String to write. |
Reimplemented from StormByte::Buffer::Simple.
|
overridevirtual |
Moves a simple buffer and writes it to the current shared buffer Thread-safe version of.
buffer | Simple buffer to write. |
Reimplemented from StormByte::Buffer::Simple.