ESPHome  2024.10.2
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
esphome::ByteBuffer Class Reference

A class modelled on the Java ByteBuffer class. More...

#include <bytebuffer.h>

Public Member Functions

 ByteBuffer ()
 
 ByteBuffer (size_t capacity, Endian endianness=LITTLE)
 Create a new Bytebuffer with the given capacity. More...
 
uint64_t get_uint (size_t length)
 
uint8_t get_uint8 ()
 Getters. More...
 
uint16_t get_uint16 ()
 
uint32_t get_uint24 ()
 
uint32_t get_uint32 ()
 
uint64_t get_uint64 ()
 
uint8_t get_int8 ()
 
int16_t get_int16 ()
 
uint32_t get_int24 ()
 
int32_t get_int32 ()
 
int64_t get_int64 ()
 
float get_float ()
 
double get_double ()
 
bool get_bool ()
 
std::vector< uint8_t > get_vector (size_t length)
 
void put_uint (uint64_t value, size_t length)
 
void put_uint8 (uint8_t value)
 Putters. More...
 
void put_uint16 (uint16_t value)
 
void put_uint24 (uint32_t value)
 
void put_uint32 (uint32_t value)
 
void put_uint64 (uint64_t value)
 
void put_int8 (int8_t value)
 
void put_int16 (int32_t value)
 
void put_int24 (int32_t value)
 
void put_int32 (int32_t value)
 
void put_int64 (int64_t value)
 
void put_float (float value)
 
void put_double (double value)
 
void put_bool (bool value)
 
void put_vector (const std::vector< uint8_t > &value)
 
size_t get_capacity () const
 
size_t get_position () const
 
size_t get_limit () const
 
size_t get_remaining () const
 
Endian get_endianness () const
 
void mark ()
 
void big_endian ()
 
void little_endian ()
 
void set_limit (size_t limit)
 
void set_position (size_t position)
 
void clear ()
 
void flip ()
 
std::vector< uint8_t > get_data ()
 
void rewind ()
 
void reset ()
 

Static Public Member Functions

static ByteBuffer wrap (std::vector< uint8_t > const &data, Endian endianness=LITTLE)
 Wrap an existing vector in a ByteBufffer. More...
 
static ByteBuffer wrap (const uint8_t *ptr, size_t len, Endian endianness=LITTLE)
 Wrap an existing array in a ByteBuffer. More...
 
static ByteBuffer wrap (uint8_t value)
 
static ByteBuffer wrap (uint16_t value, Endian endianness=LITTLE)
 
static ByteBuffer wrap (uint32_t value, Endian endianness=LITTLE)
 
static ByteBuffer wrap (uint64_t value, Endian endianness=LITTLE)
 
static ByteBuffer wrap (int8_t value)
 
static ByteBuffer wrap (int16_t value, Endian endianness=LITTLE)
 
static ByteBuffer wrap (int32_t value, Endian endianness=LITTLE)
 
static ByteBuffer wrap (int64_t value, Endian endianness=LITTLE)
 
static ByteBuffer wrap (float value, Endian endianness=LITTLE)
 
static ByteBuffer wrap (double value, Endian endianness=LITTLE)
 
static ByteBuffer wrap (bool value)
 

Protected Member Functions

 ByteBuffer (std::vector< uint8_t > const &data)
 

Protected Attributes

std::vector< uint8_t > data_
 
Endian endianness_ {LITTLE}
 
size_t position_ {0}
 
size_t mark_ {0}
 
size_t limit_ {0}
 

Detailed Description

A class modelled on the Java ByteBuffer class.

It wraps a vector of bytes and permits putting and getting items of various sizes, with an automatically incremented position.

There are three variables maintained pointing into the buffer:

capacity: the maximum amount of data that can be stored - set on construction and cannot be changed limit: the limit of the data currently available to get or put position: the current insert or extract position

0 <= position <= limit <= capacity

In addition a mark can be set to the current position with mark(). A subsequent call to reset() will restore the position to the mark.

The buffer can be marked to be little-endian (default) or big-endian. All subsequent operations will use that order.

The flip() operation will reset the position to 0 and limit to the current position. This is useful for reading data from a buffer after it has been written.

Definition at line 33 of file bytebuffer.h.

Constructor & Destructor Documentation

◆ ByteBuffer() [1/3]

esphome::ByteBuffer::ByteBuffer ( )
inline

Definition at line 36 of file bytebuffer.h.

◆ ByteBuffer() [2/3]

esphome::ByteBuffer::ByteBuffer ( size_t  capacity,
Endian  endianness = LITTLE 
)
inline

Create a new Bytebuffer with the given capacity.

Definition at line 40 of file bytebuffer.h.

◆ ByteBuffer() [3/3]

esphome::ByteBuffer::ByteBuffer ( std::vector< uint8_t > const &  data)
inlineprotected

Definition at line 136 of file bytebuffer.h.

Member Function Documentation

◆ big_endian()

void esphome::ByteBuffer::big_endian ( )
inline

Definition at line 122 of file bytebuffer.h.

◆ clear()

void esphome::ByteBuffer::clear ( )

Definition at line 74 of file bytebuffer.cpp.

◆ flip()

void esphome::ByteBuffer::flip ( )

Definition at line 78 of file bytebuffer.cpp.

◆ get_bool()

bool esphome::ByteBuffer::get_bool ( )
inline

Definition at line 92 of file bytebuffer.h.

◆ get_capacity()

size_t esphome::ByteBuffer::get_capacity ( ) const
inline

Definition at line 116 of file bytebuffer.h.

◆ get_data()

std::vector<uint8_t> esphome::ByteBuffer::get_data ( )
inline

Definition at line 131 of file bytebuffer.h.

◆ get_double()

double esphome::ByteBuffer::get_double ( )

Definition at line 118 of file bytebuffer.cpp.

◆ get_endianness()

Endian esphome::ByteBuffer::get_endianness ( ) const
inline

Definition at line 120 of file bytebuffer.h.

◆ get_float()

float esphome::ByteBuffer::get_float ( )

Definition at line 114 of file bytebuffer.cpp.

◆ get_int16()

int16_t esphome::ByteBuffer::get_int16 ( )
inline

Definition at line 83 of file bytebuffer.h.

◆ get_int24()

uint32_t esphome::ByteBuffer::get_int24 ( )

Definition at line 107 of file bytebuffer.cpp.

◆ get_int32()

int32_t esphome::ByteBuffer::get_int32 ( )
inline

Definition at line 85 of file bytebuffer.h.

◆ get_int64()

int64_t esphome::ByteBuffer::get_int64 ( )
inline

Definition at line 86 of file bytebuffer.h.

◆ get_int8()

uint8_t esphome::ByteBuffer::get_int8 ( )
inline

Definition at line 82 of file bytebuffer.h.

◆ get_limit()

size_t esphome::ByteBuffer::get_limit ( ) const
inline

Definition at line 118 of file bytebuffer.h.

◆ get_position()

size_t esphome::ByteBuffer::get_position ( ) const
inline

Definition at line 117 of file bytebuffer.h.

◆ get_remaining()

size_t esphome::ByteBuffer::get_remaining ( ) const
inline

Definition at line 119 of file bytebuffer.h.

◆ get_uint()

uint64_t esphome::ByteBuffer::get_uint ( size_t  length)

Definition at line 88 of file bytebuffer.cpp.

◆ get_uint16()

uint16_t esphome::ByteBuffer::get_uint16 ( )
inline

Definition at line 74 of file bytebuffer.h.

◆ get_uint24()

uint32_t esphome::ByteBuffer::get_uint24 ( )
inline

Definition at line 76 of file bytebuffer.h.

◆ get_uint32()

uint32_t esphome::ByteBuffer::get_uint32 ( )
inline

Definition at line 78 of file bytebuffer.h.

◆ get_uint64()

uint64_t esphome::ByteBuffer::get_uint64 ( )
inline

Definition at line 80 of file bytebuffer.h.

◆ get_uint8()

uint8_t esphome::ByteBuffer::get_uint8 ( )

Getters.

Definition at line 84 of file bytebuffer.cpp.

◆ get_vector()

std::vector< uint8_t > esphome::ByteBuffer::get_vector ( size_t  length)

Definition at line 123 of file bytebuffer.cpp.

◆ little_endian()

void esphome::ByteBuffer::little_endian ( )
inline

Definition at line 123 of file bytebuffer.h.

◆ mark()

void esphome::ByteBuffer::mark ( )
inline

Definition at line 121 of file bytebuffer.h.

◆ put_bool()

void esphome::ByteBuffer::put_bool ( bool  value)
inline

Definition at line 113 of file bytebuffer.h.

◆ put_double()

void esphome::ByteBuffer::put_double ( double  value)

Definition at line 157 of file bytebuffer.cpp.

◆ put_float()

void esphome::ByteBuffer::put_float ( float  value)

Definition at line 152 of file bytebuffer.cpp.

◆ put_int16()

void esphome::ByteBuffer::put_int16 ( int32_t  value)
inline

Definition at line 106 of file bytebuffer.h.

◆ put_int24()

void esphome::ByteBuffer::put_int24 ( int32_t  value)
inline

Definition at line 107 of file bytebuffer.h.

◆ put_int32()

void esphome::ByteBuffer::put_int32 ( int32_t  value)
inline

Definition at line 108 of file bytebuffer.h.

◆ put_int64()

void esphome::ByteBuffer::put_int64 ( int64_t  value)
inline

Definition at line 109 of file bytebuffer.h.

◆ put_int8()

void esphome::ByteBuffer::put_int8 ( int8_t  value)
inline

Definition at line 105 of file bytebuffer.h.

◆ put_uint()

void esphome::ByteBuffer::put_uint ( uint64_t  value,
size_t  length 
)

Definition at line 136 of file bytebuffer.cpp.

◆ put_uint16()

void esphome::ByteBuffer::put_uint16 ( uint16_t  value)
inline

Definition at line 100 of file bytebuffer.h.

◆ put_uint24()

void esphome::ByteBuffer::put_uint24 ( uint32_t  value)
inline

Definition at line 101 of file bytebuffer.h.

◆ put_uint32()

void esphome::ByteBuffer::put_uint32 ( uint32_t  value)
inline

Definition at line 102 of file bytebuffer.h.

◆ put_uint64()

void esphome::ByteBuffer::put_uint64 ( uint64_t  value)
inline

Definition at line 103 of file bytebuffer.h.

◆ put_uint8()

void esphome::ByteBuffer::put_uint8 ( uint8_t  value)

Putters.

Definition at line 131 of file bytebuffer.cpp.

◆ put_vector()

void esphome::ByteBuffer::put_vector ( const std::vector< uint8_t > &  value)

Definition at line 162 of file bytebuffer.cpp.

◆ reset()

void esphome::ByteBuffer::reset ( )
inline

Definition at line 133 of file bytebuffer.h.

◆ rewind()

void esphome::ByteBuffer::rewind ( )
inline

Definition at line 132 of file bytebuffer.h.

◆ set_limit()

void esphome::ByteBuffer::set_limit ( size_t  limit)

Definition at line 66 of file bytebuffer.cpp.

◆ set_position()

void esphome::ByteBuffer::set_position ( size_t  position)

Definition at line 70 of file bytebuffer.cpp.

◆ wrap() [1/13]

ByteBuffer esphome::ByteBuffer::wrap ( std::vector< uint8_t > const &  data,
Endian  endianness = LITTLE 
)
static

Wrap an existing vector in a ByteBufffer.

Definition at line 18 of file bytebuffer.cpp.

◆ wrap() [2/13]

ByteBuffer esphome::ByteBuffer::wrap ( const uint8_t *  ptr,
size_t  len,
Endian  endianness = LITTLE 
)
static

Wrap an existing array in a ByteBuffer.

Note that this will create a copy of the data.

Definition at line 10 of file bytebuffer.cpp.

◆ wrap() [3/13]

ByteBuffer esphome::ByteBuffer::wrap ( uint8_t  value)
static

Definition at line 24 of file bytebuffer.cpp.

◆ wrap() [4/13]

ByteBuffer esphome::ByteBuffer::wrap ( uint16_t  value,
Endian  endianness = LITTLE 
)
static

Definition at line 31 of file bytebuffer.cpp.

◆ wrap() [5/13]

ByteBuffer esphome::ByteBuffer::wrap ( uint32_t  value,
Endian  endianness = LITTLE 
)
static

Definition at line 38 of file bytebuffer.cpp.

◆ wrap() [6/13]

ByteBuffer esphome::ByteBuffer::wrap ( uint64_t  value,
Endian  endianness = LITTLE 
)
static

Definition at line 45 of file bytebuffer.cpp.

◆ wrap() [7/13]

static ByteBuffer esphome::ByteBuffer::wrap ( int8_t  value)
inlinestatic

Definition at line 55 of file bytebuffer.h.

◆ wrap() [8/13]

static ByteBuffer esphome::ByteBuffer::wrap ( int16_t  value,
Endian  endianness = LITTLE 
)
inlinestatic

Definition at line 56 of file bytebuffer.h.

◆ wrap() [9/13]

static ByteBuffer esphome::ByteBuffer::wrap ( int32_t  value,
Endian  endianness = LITTLE 
)
inlinestatic

Definition at line 59 of file bytebuffer.h.

◆ wrap() [10/13]

static ByteBuffer esphome::ByteBuffer::wrap ( int64_t  value,
Endian  endianness = LITTLE 
)
inlinestatic

Definition at line 62 of file bytebuffer.h.

◆ wrap() [11/13]

ByteBuffer esphome::ByteBuffer::wrap ( float  value,
Endian  endianness = LITTLE 
)
static

Definition at line 52 of file bytebuffer.cpp.

◆ wrap() [12/13]

ByteBuffer esphome::ByteBuffer::wrap ( double  value,
Endian  endianness = LITTLE 
)
static

Definition at line 59 of file bytebuffer.cpp.

◆ wrap() [13/13]

static ByteBuffer esphome::ByteBuffer::wrap ( bool  value)
inlinestatic

Definition at line 67 of file bytebuffer.h.

Field Documentation

◆ data_

std::vector<uint8_t> esphome::ByteBuffer::data_
protected

Definition at line 137 of file bytebuffer.h.

◆ endianness_

Endian esphome::ByteBuffer::endianness_ {LITTLE}
protected

Definition at line 138 of file bytebuffer.h.

◆ limit_

size_t esphome::ByteBuffer::limit_ {0}
protected

Definition at line 141 of file bytebuffer.h.

◆ mark_

size_t esphome::ByteBuffer::mark_ {0}
protected

Definition at line 140 of file bytebuffer.h.

◆ position_

size_t esphome::ByteBuffer::position_ {0}
protected

Definition at line 139 of file bytebuffer.h.


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