ESPHome 2025.5.0
Loading...
Searching...
No Matches
esphome::api::ProtoSize Class Reference

#include <api_pb2_size.h>

Static Public Member Functions

static uint32_t varint (uint32_t value)
 ProtoSize class for Protocol Buffer serialization size calculation.
 
static uint32_t varint (uint64_t value)
 Calculates the size in bytes needed to encode a uint64_t value as a varint.
 
static uint32_t varint (int32_t value)
 Calculates the size in bytes needed to encode an int32_t value as a varint.
 
static uint32_t varint (int64_t value)
 Calculates the size in bytes needed to encode an int64_t value as a varint.
 
static uint32_t field (uint32_t field_id, uint32_t type)
 Calculates the size in bytes needed to encode a field ID and wire type.
 
static void add_int32_field (uint32_t &total_size, uint32_t field_id_size, int32_t value, bool force=false)
 Common parameters for all add_*_field methods.
 
static void add_uint32_field (uint32_t &total_size, uint32_t field_id_size, uint32_t value, bool force=false)
 Calculates and adds the size of a uint32 field to the total message size.
 
static void add_bool_field (uint32_t &total_size, uint32_t field_id_size, bool value, bool force=false)
 Calculates and adds the size of a boolean field to the total message size.
 
template<uint32_t NumBytes>
static void add_fixed_field (uint32_t &total_size, uint32_t field_id_size, bool is_nonzero, bool force=false)
 Calculates and adds the size of a fixed field to the total message size.
 
static void add_enum_field (uint32_t &total_size, uint32_t field_id_size, uint32_t value, bool force=false)
 Calculates and adds the size of an enum field to the total message size.
 
static void add_sint32_field (uint32_t &total_size, uint32_t field_id_size, int32_t value, bool force=false)
 Calculates and adds the size of a sint32 field to the total message size.
 
static void add_int64_field (uint32_t &total_size, uint32_t field_id_size, int64_t value, bool force=false)
 Calculates and adds the size of an int64 field to the total message size.
 
static void add_uint64_field (uint32_t &total_size, uint32_t field_id_size, uint64_t value, bool force=false)
 Calculates and adds the size of a uint64 field to the total message size.
 
static void add_sint64_field (uint32_t &total_size, uint32_t field_id_size, int64_t value, bool force=false)
 Calculates and adds the size of a sint64 field to the total message size.
 
static void add_string_field (uint32_t &total_size, uint32_t field_id_size, const std::string &str, bool force=false)
 Calculates and adds the size of a string/bytes field to the total message size.
 
static void add_message_field (uint32_t &total_size, uint32_t field_id_size, uint32_t nested_size, bool force=false)
 Calculates and adds the size of a nested message field to the total message size.
 
template<typename MessageType >
static void add_message_object (uint32_t &total_size, uint32_t field_id_size, const MessageType &message, bool force=false)
 Calculates and adds the size of a nested message field to the total message size.
 
template<typename MessageType >
static void add_repeated_message (uint32_t &total_size, uint32_t field_id_size, const std::vector< MessageType > &messages)
 Calculates and adds the sizes of all messages in a repeated field to the total message size.
 

Detailed Description

Definition at line 10 of file api_pb2_size.h.

Member Function Documentation

◆ add_bool_field()

static void esphome::api::ProtoSize::add_bool_field ( uint32_t & total_size,
uint32_t field_id_size,
bool value,
bool force = false )
inlinestatic

Calculates and adds the size of a boolean field to the total message size.

Definition at line 177 of file api_pb2_size.h.

◆ add_enum_field()

static void esphome::api::ProtoSize::add_enum_field ( uint32_t & total_size,
uint32_t field_id_size,
uint32_t value,
bool force = false )
inlinestatic

Calculates and adds the size of an enum field to the total message size.

Enum fields are encoded as uint32 varints.

Definition at line 212 of file api_pb2_size.h.

◆ add_fixed_field()

template<uint32_t NumBytes>
static void esphome::api::ProtoSize::add_fixed_field ( uint32_t & total_size,
uint32_t field_id_size,
bool is_nonzero,
bool force = false )
inlinestatic

Calculates and adds the size of a fixed field to the total message size.

Fixed fields always take exactly N bytes (4 for fixed32/float, 8 for fixed64/double).

Template Parameters
NumBytesThe number of bytes for this fixed field (4 or 8)
Parameters
is_nonzeroWhether the value is non-zero

Definition at line 196 of file api_pb2_size.h.

◆ add_int32_field()

static void esphome::api::ProtoSize::add_int32_field ( uint32_t & total_size,
uint32_t field_id_size,
int32_t value,
bool force = false )
inlinestatic

Common parameters for all add_*_field methods.

All add_*_field methods follow these common patterns:

Parameters
total_sizeReference to the total message size to update
field_id_sizePre-calculated size of the field ID in bytes
valueThe value to calculate size for (type varies)
forceWhether to calculate size even if the value is default/zero/empty

Each method follows this implementation pattern:

  1. Skip calculation if value is default (0, false, empty) and not forced
  2. Calculate the size based on the field's encoding rules
  3. Add the field_id_size + calculated value size to total_size

Calculates and adds the size of an int32 field to the total message size

Definition at line 144 of file api_pb2_size.h.

◆ add_int64_field()

static void esphome::api::ProtoSize::add_int64_field ( uint32_t & total_size,
uint32_t field_id_size,
int64_t value,
bool force = false )
inlinestatic

Calculates and adds the size of an int64 field to the total message size.

Definition at line 241 of file api_pb2_size.h.

◆ add_message_field()

static void esphome::api::ProtoSize::add_message_field ( uint32_t & total_size,
uint32_t field_id_size,
uint32_t nested_size,
bool force = false )
inlinestatic

Calculates and adds the size of a nested message field to the total message size.

This helper function directly updates the total_size reference if the nested size is greater than zero or force is true.

Parameters
nested_sizeThe pre-calculated size of the nested message

Definition at line 304 of file api_pb2_size.h.

◆ add_message_object()

template<typename MessageType >
static void esphome::api::ProtoSize::add_message_object ( uint32_t & total_size,
uint32_t field_id_size,
const MessageType & message,
bool force = false )
inlinestatic

Calculates and adds the size of a nested message field to the total message size.

This templated version directly takes a message object, calculates its size internally, and updates the total_size reference. This eliminates the need for a temporary variable at the call site.

Template Parameters
MessageTypeThe type of the nested message (inferred from parameter)
Parameters
messageThe nested message object

Definition at line 327 of file api_pb2_size.h.

◆ add_repeated_message()

template<typename MessageType >
static void esphome::api::ProtoSize::add_repeated_message ( uint32_t & total_size,
uint32_t field_id_size,
const std::vector< MessageType > & messages )
inlinestatic

Calculates and adds the sizes of all messages in a repeated field to the total message size.

This helper processes a vector of message objects, calculating the size for each message and adding it to the total size.

Template Parameters
MessageTypeThe type of the nested messages in the vector
Parameters
messagesVector of message objects

Definition at line 346 of file api_pb2_size.h.

◆ add_sint32_field()

static void esphome::api::ProtoSize::add_sint32_field ( uint32_t & total_size,
uint32_t field_id_size,
int32_t value,
bool force = false )
inlinestatic

Calculates and adds the size of a sint32 field to the total message size.

Sint32 fields use ZigZag encoding, which is more efficient for negative values.

Definition at line 227 of file api_pb2_size.h.

◆ add_sint64_field()

static void esphome::api::ProtoSize::add_sint64_field ( uint32_t & total_size,
uint32_t field_id_size,
int64_t value,
bool force = false )
inlinestatic

Calculates and adds the size of a sint64 field to the total message size.

Sint64 fields use ZigZag encoding, which is more efficient for negative values.

Definition at line 270 of file api_pb2_size.h.

◆ add_string_field()

static void esphome::api::ProtoSize::add_string_field ( uint32_t & total_size,
uint32_t field_id_size,
const std::string & str,
bool force = false )
inlinestatic

Calculates and adds the size of a string/bytes field to the total message size.

Definition at line 284 of file api_pb2_size.h.

◆ add_uint32_field()

static void esphome::api::ProtoSize::add_uint32_field ( uint32_t & total_size,
uint32_t field_id_size,
uint32_t value,
bool force = false )
inlinestatic

Calculates and adds the size of a uint32 field to the total message size.

Definition at line 163 of file api_pb2_size.h.

◆ add_uint64_field()

static void esphome::api::ProtoSize::add_uint64_field ( uint32_t & total_size,
uint32_t field_id_size,
uint64_t value,
bool force = false )
inlinestatic

Calculates and adds the size of a uint64 field to the total message size.

Definition at line 254 of file api_pb2_size.h.

◆ field()

static uint32_t esphome::api::ProtoSize::field ( uint32_t field_id,
uint32_t type )
inlinestatic

Calculates the size in bytes needed to encode a field ID and wire type.

Parameters
field_idThe field identifier
typeThe wire type value (from the WireType enum in the protobuf spec)
Returns
The number of bytes needed to encode the field ID and wire type

Definition at line 120 of file api_pb2_size.h.

◆ varint() [1/4]

static uint32_t esphome::api::ProtoSize::varint ( int32_t value)
inlinestatic

Calculates the size in bytes needed to encode an int32_t value as a varint.

Special handling is needed for negative values, which are sign-extended to 64 bits in Protocol Buffers, resulting in a 10-byte varint.

Parameters
valueThe int32_t value to calculate size for
Returns
The number of bytes needed to encode the value

Definition at line 90 of file api_pb2_size.h.

◆ varint() [2/4]

static uint32_t esphome::api::ProtoSize::varint ( int64_t value)
inlinestatic

Calculates the size in bytes needed to encode an int64_t value as a varint.

Parameters
valueThe int64_t value to calculate size for
Returns
The number of bytes needed to encode the value

Definition at line 106 of file api_pb2_size.h.

◆ varint() [3/4]

static uint32_t esphome::api::ProtoSize::varint ( uint32_t value)
inlinestatic

ProtoSize class for Protocol Buffer serialization size calculation.

This class provides static methods to calculate the exact byte counts needed for encoding various Protocol Buffer field types. All methods are designed to be efficient for the common case where many fields have default values.

Implements Protocol Buffer encoding size calculation according to: https://protobuf.dev/programming-guides/encoding/

Key features:

  • Early-return optimization for zero/default values
  • Direct total_size updates to avoid unnecessary additions
  • Specialized handling for different field types according to protobuf spec
  • Templated helpers for repeated fields and messages

Calculates the size in bytes needed to encode a uint32_t value as a varint

Parameters
valueThe uint32_t value to calculate size for
Returns
The number of bytes needed to encode the value

Definition at line 35 of file api_pb2_size.h.

◆ varint() [4/4]

static uint32_t esphome::api::ProtoSize::varint ( uint64_t value)
inlinestatic

Calculates the size in bytes needed to encode a uint64_t value as a varint.

Parameters
valueThe uint64_t value to calculate size for
Returns
The number of bytes needed to encode the value

Definition at line 59 of file api_pb2_size.h.


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