ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
esphome::packet_interface::PacketBuffer Class Reference

A buffer class that efficiently represents a composite buffer with three sections: More...

#include <packet_buffer.h>

Public Member Functions

 PacketBuffer ()=default
 
 PacketBuffer (const PacketBuffer &)=delete
 
PacketBuffer & operator= (const PacketBuffer &)=delete
 
 PacketBuffer (PacketBuffer &&)=default
 
PacketBuffer & operator= (PacketBuffer &&)=default
 
 PacketBuffer (const uint8_t *data, size_t length)
 Construct from a single buffer.
 
 PacketBuffer (const std::vector< uint8_t > &vec)
 Construct from a vector.
 
 PacketBuffer (std::vector< uint8_t > &&)=delete
 
 PacketBuffer (const uint8_t *pre_data, size_t pre_length, const uint8_t *post_data, size_t post_length)
 Construct with pre and post buffers.
 
 PacketBuffer (const uint8_t *pre_data, size_t pre_length, const PacketBuffer *mid_buffer, const uint8_t *post_data, size_t post_length)
 Construct with pre, mid, and post buffers.
 
size_t size () const
 Get the total size of the buffer.
 
bool empty () const
 Check if the buffer is empty.
 
bool is_contiguous () const
 Check if all the data is held in the pre-buffer, so it can be read as one span.
 
std::span< const uint8_t > as_span () const
 Get the data as one span.
 
uint8_t operator[] (size_t index) const
 Access a byte at a specific position.
 
size_t copy_to (uint8_t *dest, size_t count, size_t offset=0) const
 Copy data from the buffer to a destination pointer.
 

Detailed Description

A buffer class that efficiently represents a composite buffer with three sections:

  • pre_buffer: A pointer to uint8_t data with a length
  • mid_buffer: A reference to another PacketBuffer (optional)
  • post_buffer: A pointer to uint8_t data with a length

This allows efficient zero-copy composition of buffers without allocating new memory.

WARNING: PacketBuffer holds raw pointers to data. The user must ensure that the data remains valid for the lifetime of the PacketBuffer instance. Storing PacketBuffer instances beyond the scope of the data they reference will lead to dangling pointers and undefined behavior, so to keep a packet, copy it with copy_to() into a buffer allocated in setup().

Definition at line 24 of file packet_buffer.h.

Constructor & Destructor Documentation

◆ PacketBuffer() [1/8]

esphome::packet_interface::PacketBuffer::PacketBuffer ( )
default

◆ PacketBuffer() [2/8]

esphome::packet_interface::PacketBuffer::PacketBuffer ( const PacketBuffer & )
delete

◆ PacketBuffer() [3/8]

esphome::packet_interface::PacketBuffer::PacketBuffer ( PacketBuffer && )
default

◆ PacketBuffer() [4/8]

esphome::packet_interface::PacketBuffer::PacketBuffer ( const uint8_t * data,
size_t length )
inline

Construct from a single buffer.

Definition at line 39 of file packet_buffer.h.

◆ PacketBuffer() [5/8]

esphome::packet_interface::PacketBuffer::PacketBuffer ( const std::vector< uint8_t > & vec)
inlineexplicit

Construct from a vector.

Definition at line 44 of file packet_buffer.h.

◆ PacketBuffer() [6/8]

esphome::packet_interface::PacketBuffer::PacketBuffer ( std::vector< uint8_t > && )
explicitdelete

◆ PacketBuffer() [7/8]

esphome::packet_interface::PacketBuffer::PacketBuffer ( const uint8_t * pre_data,
size_t pre_length,
const uint8_t * post_data,
size_t post_length )
inline

Construct with pre and post buffers.

Definition at line 51 of file packet_buffer.h.

◆ PacketBuffer() [8/8]

esphome::packet_interface::PacketBuffer::PacketBuffer ( const uint8_t * pre_data,
size_t pre_length,
const PacketBuffer * mid_buffer,
const uint8_t * post_data,
size_t post_length )
inline

Construct with pre, mid, and post buffers.

Definition at line 57 of file packet_buffer.h.

Member Function Documentation

◆ as_span()

std::span< const uint8_t > esphome::packet_interface::PacketBuffer::as_span ( ) const
inline

Get the data as one span.

Only valid when is_contiguous() is true.

Definition at line 89 of file packet_buffer.h.

◆ copy_to()

size_t esphome::packet_interface::PacketBuffer::copy_to ( uint8_t * dest,
size_t count,
size_t offset = 0 ) const
inline

Copy data from the buffer to a destination pointer.

Parameters
destDestination pointer
countNumber of bytes to copy
offsetOffset in the source buffer to start copying from
Returns
Number of bytes actually copied

Definition at line 120 of file packet_buffer.h.

◆ empty()

bool esphome::packet_interface::PacketBuffer::empty ( ) const
inline

Check if the buffer is empty.

Definition at line 79 of file packet_buffer.h.

◆ is_contiguous()

bool esphome::packet_interface::PacketBuffer::is_contiguous ( ) const
inline

Check if all the data is held in the pre-buffer, so it can be read as one span.

Definition at line 84 of file packet_buffer.h.

◆ operator=() [1/2]

PacketBuffer & esphome::packet_interface::PacketBuffer::operator= ( const PacketBuffer & )
delete

◆ operator=() [2/2]

PacketBuffer & esphome::packet_interface::PacketBuffer::operator= ( PacketBuffer && )
default

◆ operator[]()

uint8_t esphome::packet_interface::PacketBuffer::operator[] ( size_t index) const
inline

Access a byte at a specific position.

Parameters
indexThe position to access
Returns
The byte at the specified position, or 0 past the end

Definition at line 96 of file packet_buffer.h.

◆ size()

size_t esphome::packet_interface::PacketBuffer::size ( ) const
inline

Get the total size of the buffer.

Definition at line 68 of file packet_buffer.h.


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