ESPHome 2025.8.0-dev
Loading...
Searching...
No Matches
esphome::api::ProtoSize Class Reference

#include <proto.h>

Public Member Functions

 ProtoSize ()=default
 ProtoSize class for Protocol Buffer serialization size calculation.
 
uint32_t get_size () const
 
void add_int32 (uint32_t field_id_size, int32_t value)
 Common parameters for all add_*_field methods.
 
void add_int32_force (uint32_t field_id_size, int32_t value)
 Calculates and adds the size of an int32 field to the total message size (force version)
 
void add_uint32 (uint32_t field_id_size, uint32_t value)
 Calculates and adds the size of a uint32 field to the total message size.
 
void add_uint32_force (uint32_t field_id_size, uint32_t value)
 Calculates and adds the size of a uint32 field to the total message size (force version)
 
void add_bool (uint32_t field_id_size, bool value)
 Calculates and adds the size of a boolean field to the total message size.
 
void add_bool_force (uint32_t field_id_size, bool value)
 Calculates and adds the size of a boolean field to the total message size (force version)
 
void add_float (uint32_t field_id_size, float value)
 Calculates and adds the size of a float field to the total message size.
 
void add_fixed32 (uint32_t field_id_size, uint32_t value)
 Calculates and adds the size of a fixed32 field to the total message size.
 
void add_sfixed32 (uint32_t field_id_size, int32_t value)
 Calculates and adds the size of a sfixed32 field to the total message size.
 
void add_sint32 (uint32_t field_id_size, int32_t value)
 Calculates and adds the size of a sint32 field to the total message size.
 
void add_sint32_force (uint32_t field_id_size, int32_t value)
 Calculates and adds the size of a sint32 field to the total message size (force version)
 
void add_int64 (uint32_t field_id_size, int64_t value)
 Calculates and adds the size of an int64 field to the total message size.
 
void add_int64_force (uint32_t field_id_size, int64_t value)
 Calculates and adds the size of an int64 field to the total message size (force version)
 
void add_uint64 (uint32_t field_id_size, uint64_t value)
 Calculates and adds the size of a uint64 field to the total message size.
 
void add_uint64_force (uint32_t field_id_size, uint64_t value)
 Calculates and adds the size of a uint64 field to the total message size (force version)
 
void add_length (uint32_t field_id_size, size_t len)
 Calculates and adds the size of a length-delimited field (string/bytes) to the total message size.
 
void add_length_force (uint32_t field_id_size, size_t len)
 Calculates and adds the size of a length-delimited field (string/bytes) to the total message size (repeated field version)
 
void add_precalculated_size (uint32_t size)
 Adds a pre-calculated size directly to the total.
 
void add_message_field (uint32_t field_id_size, uint32_t nested_size)
 Calculates and adds the size of a nested message field to the total message size.
 
void add_message_field_force (uint32_t field_id_size, uint32_t nested_size)
 Calculates and adds the size of a nested message field to the total message size (force version)
 
void add_message_object (uint32_t field_id_size, const ProtoMessage &message)
 Calculates and adds the size of a nested message field to the total message size.
 
void add_message_object_force (uint32_t field_id_size, const ProtoMessage &message)
 Calculates and adds the size of a nested message field to the total message size (force version)
 
template<typename MessageType >
void add_repeated_message (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.
 

Static Public Member Functions

static constexpr uint32_t varint (uint32_t value)
 Calculates the size in bytes needed to encode a uint32_t value as a varint.
 
static constexpr uint32_t varint (uint64_t value)
 Calculates the size in bytes needed to encode a uint64_t value as a varint.
 
static constexpr uint32_t varint (int32_t value)
 Calculates the size in bytes needed to encode an int32_t value as a varint.
 
static constexpr uint32_t varint (int64_t value)
 Calculates the size in bytes needed to encode an int64_t value as a varint.
 
static constexpr uint32_t field (uint32_t field_id, uint32_t type)
 Calculates the size in bytes needed to encode a field ID and wire type.
 

Detailed Description

Definition at line 362 of file proto.h.

Constructor & Destructor Documentation

◆ ProtoSize()

esphome::api::ProtoSize::ProtoSize ( )
default

ProtoSize class for Protocol Buffer serialization size calculation.

This class provides methods to calculate the exact byte counts needed for encoding various Protocol Buffer field types. The class now uses an object-based approach to reduce parameter passing overhead while keeping varint calculation methods static for external use.

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

Key features:

  • Object-based approach reduces flash usage by eliminating parameter passing
  • Early-return optimization for zero/default values
  • Static varint methods for external callers
  • Specialized handling for different field types according to protobuf spec

Member Function Documentation

◆ add_bool()

void esphome::api::ProtoSize::add_bool ( uint32_t field_id_size,
bool value )
inline

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

Definition at line 537 of file proto.h.

◆ add_bool_force()

void esphome::api::ProtoSize::add_bool_force ( uint32_t field_id_size,
bool value )
inline

Calculates and adds the size of a boolean field to the total message size (force version)

Definition at line 547 of file proto.h.

◆ add_fixed32()

void esphome::api::ProtoSize::add_fixed32 ( uint32_t field_id_size,
uint32_t value )
inline

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

Definition at line 568 of file proto.h.

◆ add_float()

void esphome::api::ProtoSize::add_float ( uint32_t field_id_size,
float value )
inline

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

Definition at line 556 of file proto.h.

◆ add_int32()

void esphome::api::ProtoSize::add_int32 ( uint32_t field_id_size,
int32_t value )
inline

Common parameters for all add_*_field methods.

All add_*_field methods follow these common patterns:

  • Parameters
    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:

Skip calculation if value is default (0, false, empty) and not forced

  1. Calculate the size based on the field's encoding rules
  2. 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 502 of file proto.h.

◆ add_int32_force()

void esphome::api::ProtoSize::add_int32_force ( uint32_t field_id_size,
int32_t value )
inline

Calculates and adds the size of an int32 field to the total message size (force version)

Definition at line 511 of file proto.h.

◆ add_int64()

void esphome::api::ProtoSize::add_int64 ( uint32_t field_id_size,
int64_t value )
inline

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

Definition at line 614 of file proto.h.

◆ add_int64_force()

void esphome::api::ProtoSize::add_int64_force ( uint32_t field_id_size,
int64_t value )
inline

Calculates and adds the size of an int64 field to the total message size (force version)

Definition at line 623 of file proto.h.

◆ add_length()

void esphome::api::ProtoSize::add_length ( uint32_t field_id_size,
size_t len )
inline

Calculates and adds the size of a length-delimited field (string/bytes) to the total message size.

Definition at line 651 of file proto.h.

◆ add_length_force()

void esphome::api::ProtoSize::add_length_force ( uint32_t field_id_size,
size_t len )
inline

Calculates and adds the size of a length-delimited field (string/bytes) to the total message size (repeated field version)

Definition at line 661 of file proto.h.

◆ add_message_field()

void esphome::api::ProtoSize::add_message_field ( uint32_t field_id_size,
uint32_t nested_size )
inline

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.

Parameters
nested_sizeThe pre-calculated size of the nested message

Definition at line 685 of file proto.h.

◆ add_message_field_force()

void esphome::api::ProtoSize::add_message_field_force ( uint32_t field_id_size,
uint32_t nested_size )
inline

Calculates and adds the size of a nested message field to the total message size (force version)

Parameters
nested_sizeThe pre-calculated size of the nested message

Definition at line 696 of file proto.h.

◆ add_message_object()

void esphome::api::ProtoSize::add_message_object ( uint32_t field_id_size,
const ProtoMessage & message )
inline

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

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

Parameters
messageThe nested message object

Definition at line 711 of file proto.h.

◆ add_message_object_force()

void esphome::api::ProtoSize::add_message_object_force ( uint32_t field_id_size,
const ProtoMessage & message )
inline

Calculates and adds the size of a nested message field to the total message size (force version)

Parameters
messageThe nested message object

Definition at line 726 of file proto.h.

◆ add_precalculated_size()

void esphome::api::ProtoSize::add_precalculated_size ( uint32_t size)
inline

Adds a pre-calculated size directly to the total.

This is used when we can calculate the total size by multiplying the number of elements by the bytes per element (for repeated fixed-size types like float, fixed32, etc.)

Parameters
sizeThe pre-calculated total size to add

Definition at line 675 of file proto.h.

◆ add_repeated_message()

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

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 746 of file proto.h.

◆ add_sfixed32()

void esphome::api::ProtoSize::add_sfixed32 ( uint32_t field_id_size,
int32_t value )
inline

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

Definition at line 580 of file proto.h.

◆ add_sint32()

void esphome::api::ProtoSize::add_sint32 ( uint32_t field_id_size,
int32_t value )
inline

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 594 of file proto.h.

◆ add_sint32_force()

void esphome::api::ProtoSize::add_sint32_force ( uint32_t field_id_size,
int32_t value )
inline

Calculates and adds the size of a sint32 field to the total message size (force version)

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

Definition at line 605 of file proto.h.

◆ add_uint32()

void esphome::api::ProtoSize::add_uint32 ( uint32_t field_id_size,
uint32_t value )
inline

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

Definition at line 520 of file proto.h.

◆ add_uint32_force()

void esphome::api::ProtoSize::add_uint32_force ( uint32_t field_id_size,
uint32_t value )
inline

Calculates and adds the size of a uint32 field to the total message size (force version)

Definition at line 529 of file proto.h.

◆ add_uint64()

void esphome::api::ProtoSize::add_uint64 ( uint32_t field_id_size,
uint64_t value )
inline

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

Definition at line 631 of file proto.h.

◆ add_uint64_force()

void esphome::api::ProtoSize::add_uint64_force ( uint32_t field_id_size,
uint64_t value )
inline

Calculates and adds the size of a uint64 field to the total message size (force version)

Definition at line 640 of file proto.h.

◆ field()

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

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 480 of file proto.h.

◆ get_size()

uint32_t esphome::api::ProtoSize::get_size ( ) const
inline

Definition at line 387 of file proto.h.

◆ varint() [1/4]

static constexpr uint32_t esphome::api::ProtoSize::varint ( int32_t value)
inlinestaticconstexpr

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 450 of file proto.h.

◆ varint() [2/4]

static constexpr uint32_t esphome::api::ProtoSize::varint ( int64_t value)
inlinestaticconstexpr

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 466 of file proto.h.

◆ varint() [3/4]

static constexpr uint32_t esphome::api::ProtoSize::varint ( uint32_t value)
inlinestaticconstexpr

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 395 of file proto.h.

◆ varint() [4/4]

static constexpr uint32_t esphome::api::ProtoSize::varint ( uint64_t value)
inlinestaticconstexpr

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 419 of file proto.h.


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