kmMqtt 0.0.1
MQTT Client Library
Loading...
Searching...
No Matches
kmMqtt::ByteBuffer Struct Reference

A fixed-capacity byte buffer with optional small buffer optimization (SBO). More...

#include <ByteBuffer.h>

Public Member Functions

 ByteBuffer () noexcept
 Constructs a ByteBuffer with default capacity using SBO if enabled.
 ByteBuffer (std::size_t capacity) noexcept
 Constructs a ByteBuffer with the specified capacity.
std::uint8_t readUint8 () const
 Reads a single byte from the buffer at the current read cursor position and advances the cursor.
std::uint16_t readUInt16 () const
 Reads a 16-bit unsigned integer from the buffer at the current read cursor position and advances the cursor by 2 bytes.
std::uint32_t readUInt32 () const
 Reads a 32-bit unsigned integer from the buffer at the current read cursor position and advances the cursor by 4 bytes.
void incrementReadCursor (std::size_t shiftVal) const noexcept
 Increments the read cursor by the specified value.
void resetReadCursor () const noexcept
 Resets the read cursor to the beginning of the buffer.
ByteBufferappend (std::uint16_t bytes)
 Appends a 2 bytes to the end of the buffer.
ByteBufferappend (std::uint32_t bytes)
 Appends 4 bytes to the end of the buffer.
ByteBufferappend (const std::uint8_t *bytes, std::size_t size)
 Appends a sequence of bytes to the end of the buffer.
ByteBufferappend (const ByteBuffer &byteBuffer)
 Appends the contents of another ByteBuffer to this buffer.
void removeFromBeginning (std::size_t count)
 Removes the specified number of bytes from the beginning of the buffer.
std::size_t size () const noexcept
 Get the size of the buffer (number of bytes currently stored).
std::size_t capacity () const noexcept
 Get the capacity of the buffer (maximum number of bytes it can hold).
std::size_t headroom () const noexcept
 Get the headroom of the buffer (remaining space available for writing).
const std::uint8_t * bytes () const noexcept
 Get a pointer to the underlying byte array.
std::size_t readCursor () const noexcept
 Get the current read cursor position.
std::size_t readHeadroom () const noexcept
 Get the remaining bytes available to read from the current read cursor position.
std::string toString () const noexcept
 Converts the buffer contents to a string of bits for debugging.

Detailed Description

A fixed-capacity byte buffer with optional small buffer optimization (SBO).

ByteBuffer provides a contiguous memory area for storing bytes, with a capacity defined at construction time. The buffer is not resizable after construction. If SBO is enabled and the requested capacity is small enough, the buffer uses stack memory; otherwise, it allocates on the heap.

Provides methods for appending and reading bytes and multi-byte values, as well as cursor management for sequential reading.

The buffer's capacity is fixed after construction and cannot be changed.

SBO (Small Buffer Optimization) is enabled if ENABLE_BYTEBUFFER_SBO is defined.

Constructor & Destructor Documentation

◆ ByteBuffer()

kmMqtt::ByteBuffer::ByteBuffer ( std::size_t capacity)
inlinenoexcept

Constructs a ByteBuffer with the specified capacity.

Parameters
capacityThe desired capacity of the buffer in bytes. Uses SBO if enabled and capacity is small enough.

Member Function Documentation

◆ append() [1/4]

ByteBuffer & kmMqtt::ByteBuffer::append ( const ByteBuffer & byteBuffer)
inline

Appends the contents of another ByteBuffer to this buffer.

Make sure there is enough headroom in target buffer before calling this method.

Parameters
byteBufferThe ByteBuffer whose contents to append.
Returns
Reference to the ByteBuffer for chaining.

◆ append() [2/4]

ByteBuffer & kmMqtt::ByteBuffer::append ( const std::uint8_t * bytes,
std::size_t size )
inline

Appends a sequence of bytes to the end of the buffer.

Parameters
bytesPointer to the bytes to append.
sizeNumber of bytes to append.
Returns
Reference to the ByteBuffer for chaining.

◆ append() [3/4]

ByteBuffer & kmMqtt::ByteBuffer::append ( std::uint16_t bytes)
inline

Appends a 2 bytes to the end of the buffer.

Parameters
bytesThe 2 bytes to append.
Returns
Reference to the ByteBuffer for chaining.

◆ append() [4/4]

ByteBuffer & kmMqtt::ByteBuffer::append ( std::uint32_t bytes)
inline

Appends 4 bytes to the end of the buffer.

Parameters
bytesThe 4 bytes to append.
Returns
Reference to the ByteBuffer for chaining.

◆ removeFromBeginning()

void kmMqtt::ByteBuffer::removeFromBeginning ( std::size_t count)
inline

Removes the specified number of bytes from the beginning of the buffer.

Parameters
countThe number of bytes to remove.
Exceptions
std::out_of_rangeif count is greater than the current size of the buffer.

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