mirror of
https://github.com/esphome/esphome.git
synced 2025-08-03 08:57:47 +00:00
fixes
This commit is contained in:
parent
6b0391a1f9
commit
79dbeef0c9
@ -128,20 +128,21 @@ class ProtoSize {
|
||||
* All add_*_field methods follow these common patterns:
|
||||
*
|
||||
* @param total_size Reference to the total message size to update
|
||||
* @param field_id_size Pre-calculated size of the field ID in bytes
|
||||
* @param precalced_field_id_size Pre-calculated size of the field ID in bytes
|
||||
* @param value The value to calculate size for (type varies)
|
||||
* @param force Whether 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
|
||||
* 3. Add the precalced_field_id_size + calculated value size to total_size
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Calculates and adds the size of an int32 field to the total message size
|
||||
*/
|
||||
static inline void add_int32_field(uint32_t &total_size, uint32_t field_id_size, int32_t value, bool force = false) {
|
||||
static inline void add_int32_field(uint32_t &total_size, uint32_t precalced_field_id_size, int32_t value,
|
||||
bool force = false) {
|
||||
// Skip calculation if value is zero and not forced
|
||||
if (value == 0 && !force) {
|
||||
return; // No need to update total_size
|
||||
@ -150,17 +151,17 @@ class ProtoSize {
|
||||
// Calculate and directly add to total_size
|
||||
if (value < 0) {
|
||||
// Negative values are encoded as 10-byte varints in protobuf
|
||||
total_size += field_id_size + 10;
|
||||
total_size += precalced_field_id_size + 10;
|
||||
} else {
|
||||
// For non-negative values, use the standard varint size
|
||||
total_size += field_id_size + varint(static_cast<uint32_t>(value));
|
||||
total_size += precalced_field_id_size + varint(static_cast<uint32_t>(value));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates and adds the size of a uint32 field to the total message size
|
||||
*/
|
||||
static inline void add_uint32_field(uint32_t &total_size, uint32_t field_id_size, uint32_t value,
|
||||
static inline void add_uint32_field(uint32_t &total_size, uint32_t precalced_field_id_size, uint32_t value,
|
||||
bool force = false) {
|
||||
// Skip calculation if value is zero and not forced
|
||||
if (value == 0 && !force) {
|
||||
@ -168,20 +169,21 @@ class ProtoSize {
|
||||
}
|
||||
|
||||
// Calculate and directly add to total_size
|
||||
total_size += field_id_size + varint(value);
|
||||
total_size += precalced_field_id_size + varint(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates and adds the size of a boolean field to the total message size
|
||||
*/
|
||||
static inline void add_bool_field(uint32_t &total_size, uint32_t field_id_size, bool value, bool force = false) {
|
||||
static inline void add_bool_field(uint32_t &total_size, uint32_t precalced_field_id_size, bool value,
|
||||
bool force = false) {
|
||||
// Skip calculation if value is false and not forced
|
||||
if (!value && !force) {
|
||||
return; // No need to update total_size
|
||||
}
|
||||
|
||||
// Boolean fields always use 1 byte when true
|
||||
total_size += field_id_size + 1;
|
||||
total_size += precalced_field_id_size + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -193,7 +195,7 @@ class ProtoSize {
|
||||
* @param is_nonzero Whether the value is non-zero
|
||||
*/
|
||||
template<uint32_t NumBytes>
|
||||
static inline void add_fixed_field(uint32_t &total_size, uint32_t field_id_size, bool is_nonzero,
|
||||
static inline void add_fixed_field(uint32_t &total_size, uint32_t precalced_field_id_size, bool is_nonzero,
|
||||
bool force = false) {
|
||||
// Skip calculation if value is zero and not forced
|
||||
if (!is_nonzero && !force) {
|
||||
@ -201,7 +203,7 @@ class ProtoSize {
|
||||
}
|
||||
|
||||
// Fixed fields always take exactly NumBytes
|
||||
total_size += field_id_size + NumBytes;
|
||||
total_size += precalced_field_id_size + NumBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -209,14 +211,15 @@ class ProtoSize {
|
||||
*
|
||||
* Enum fields are encoded as uint32 varints.
|
||||
*/
|
||||
static inline void add_enum_field(uint32_t &total_size, uint32_t field_id_size, uint32_t value, bool force = false) {
|
||||
static inline void add_enum_field(uint32_t &total_size, uint32_t precalced_field_id_size, uint32_t value,
|
||||
bool force = false) {
|
||||
// Skip calculation if value is zero and not forced
|
||||
if (value == 0 && !force) {
|
||||
return; // No need to update total_size
|
||||
}
|
||||
|
||||
// Enums are encoded as uint32
|
||||
total_size += field_id_size + varint(value);
|
||||
total_size += precalced_field_id_size + varint(value);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -224,7 +227,8 @@ class ProtoSize {
|
||||
*
|
||||
* Sint32 fields use ZigZag encoding, which is more efficient for negative values.
|
||||
*/
|
||||
static inline void add_sint32_field(uint32_t &total_size, uint32_t field_id_size, int32_t value, bool force = false) {
|
||||
static inline void add_sint32_field(uint32_t &total_size, uint32_t precalced_field_id_size, int32_t value,
|
||||
bool force = false) {
|
||||
// Skip calculation if value is zero and not forced
|
||||
if (value == 0 && !force) {
|
||||
return; // No need to update total_size
|
||||
@ -232,26 +236,27 @@ class ProtoSize {
|
||||
|
||||
// ZigZag encoding for sint32: (n << 1) ^ (n >> 31)
|
||||
uint32_t zigzag = (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
|
||||
total_size += field_id_size + varint(zigzag);
|
||||
total_size += precalced_field_id_size + varint(zigzag);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates and adds the size of an int64 field to the total message size
|
||||
*/
|
||||
static inline void add_int64_field(uint32_t &total_size, uint32_t field_id_size, int64_t value, bool force = false) {
|
||||
static inline void add_int64_field(uint32_t &total_size, uint32_t precalced_field_id_size, int64_t value,
|
||||
bool force = false) {
|
||||
// Skip calculation if value is zero and not forced
|
||||
if (value == 0 && !force) {
|
||||
return; // No need to update total_size
|
||||
}
|
||||
|
||||
// Calculate and directly add to total_size
|
||||
total_size += field_id_size + varint(value);
|
||||
total_size += precalced_field_id_size + varint(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates and adds the size of a uint64 field to the total message size
|
||||
*/
|
||||
static inline void add_uint64_field(uint32_t &total_size, uint32_t field_id_size, uint64_t value,
|
||||
static inline void add_uint64_field(uint32_t &total_size, uint32_t precalced_field_id_size, uint64_t value,
|
||||
bool force = false) {
|
||||
// Skip calculation if value is zero and not forced
|
||||
if (value == 0 && !force) {
|
||||
@ -259,7 +264,7 @@ class ProtoSize {
|
||||
}
|
||||
|
||||
// Calculate and directly add to total_size
|
||||
total_size += field_id_size + varint(value);
|
||||
total_size += precalced_field_id_size + varint(value);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -267,7 +272,8 @@ class ProtoSize {
|
||||
*
|
||||
* Sint64 fields use ZigZag encoding, which is more efficient for negative values.
|
||||
*/
|
||||
static inline void add_sint64_field(uint32_t &total_size, uint32_t field_id_size, int64_t value, bool force = false) {
|
||||
static inline void add_sint64_field(uint32_t &total_size, uint32_t precalced_field_id_size, int64_t value,
|
||||
bool force = false) {
|
||||
// Skip calculation if value is zero and not forced
|
||||
if (value == 0 && !force) {
|
||||
return; // No need to update total_size
|
||||
@ -275,13 +281,13 @@ class ProtoSize {
|
||||
|
||||
// ZigZag encoding for sint64: (n << 1) ^ (n >> 63)
|
||||
uint64_t zigzag = (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
|
||||
total_size += field_id_size + varint(zigzag);
|
||||
total_size += precalced_field_id_size + varint(zigzag);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates and adds the size of a string/bytes field to the total message size
|
||||
*/
|
||||
static inline void add_string_field(uint32_t &total_size, uint32_t field_id_size, const std::string &str,
|
||||
static inline void add_string_field(uint32_t &total_size, uint32_t precalced_field_id_size, const std::string &str,
|
||||
bool force = false) {
|
||||
// Skip calculation if string is empty and not forced
|
||||
if (str.empty() && !force) {
|
||||
@ -290,7 +296,7 @@ class ProtoSize {
|
||||
|
||||
// Calculate and directly add to total_size
|
||||
const uint32_t str_size = static_cast<uint32_t>(str.size());
|
||||
total_size += field_id_size + varint(str_size) + str_size;
|
||||
total_size += precalced_field_id_size + varint(str_size) + str_size;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -301,7 +307,7 @@ class ProtoSize {
|
||||
*
|
||||
* @param nested_size The pre-calculated size of the nested message
|
||||
*/
|
||||
static inline void add_message_field(uint32_t &total_size, uint32_t field_id_size, uint32_t nested_size,
|
||||
static inline void add_message_field(uint32_t &total_size, uint32_t precalced_field_id_size, uint32_t nested_size,
|
||||
bool force = false) {
|
||||
// Skip calculation if nested message is empty and not forced
|
||||
if (nested_size == 0 && !force) {
|
||||
@ -310,7 +316,7 @@ class ProtoSize {
|
||||
|
||||
// Calculate and directly add to total_size
|
||||
// Field ID + length varint + nested message content
|
||||
total_size += field_id_size + varint(nested_size) + nested_size;
|
||||
total_size += precalced_field_id_size + varint(nested_size) + nested_size;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -322,13 +328,13 @@ class ProtoSize {
|
||||
*
|
||||
* @param message The nested message object
|
||||
*/
|
||||
static inline void add_message_object(uint32_t &total_size, uint32_t field_id_size, const ProtoMessage &message,
|
||||
bool force = false) {
|
||||
static inline void add_message_object(uint32_t &total_size, uint32_t precalced_field_id_size,
|
||||
const ProtoMessage &message, bool force = false) {
|
||||
uint32_t nested_size = 0;
|
||||
message.calculate_size(nested_size);
|
||||
|
||||
// Use the base implementation with the calculated nested_size
|
||||
add_message_field(total_size, field_id_size, nested_size, force);
|
||||
add_message_field(total_size, precalced_field_id_size, nested_size, force);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -341,7 +347,7 @@ class ProtoSize {
|
||||
* @param messages Vector of message objects
|
||||
*/
|
||||
template<typename MessageType>
|
||||
static inline void add_repeated_message(uint32_t &total_size, uint32_t field_id_size,
|
||||
static inline void add_repeated_message(uint32_t &total_size, uint32_t precalced_field_id_size,
|
||||
const std::vector<MessageType> &messages) {
|
||||
// Skip if the vector is empty
|
||||
if (messages.empty()) {
|
||||
@ -350,7 +356,7 @@ class ProtoSize {
|
||||
|
||||
// For repeated fields, always use force=true
|
||||
for (const auto &message : messages) {
|
||||
add_message_object(total_size, field_id_size, message, true);
|
||||
add_message_object(total_size, precalced_field_id_size, message, true);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -162,69 +162,69 @@ void encode_bytes_field(ProtoWriteBuffer &buffer, const void *field_ptr, uint8_t
|
||||
}
|
||||
|
||||
// Type-specific size calculation functions
|
||||
void size_string_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_string_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *str = static_cast<const std::string *>(field_ptr);
|
||||
ProtoSize::add_string_field(total_size, precalced_field_tag_size, *str, force);
|
||||
ProtoSize::add_string_field(total_size, precalced_field_id_size, *str, force);
|
||||
}
|
||||
|
||||
void size_fixed32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_fixed32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *val = static_cast<const uint32_t *>(field_ptr);
|
||||
ProtoSize::add_fixed_field<4>(total_size, precalced_field_tag_size, *val != 0 || force, force);
|
||||
ProtoSize::add_fixed_field<4>(total_size, precalced_field_id_size, *val != 0 || force, force);
|
||||
}
|
||||
|
||||
void size_bool_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_bool_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *val = static_cast<const bool *>(field_ptr);
|
||||
ProtoSize::add_bool_field(total_size, precalced_field_tag_size, *val, force);
|
||||
ProtoSize::add_bool_field(total_size, precalced_field_id_size, *val, force);
|
||||
}
|
||||
|
||||
void size_float_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_float_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *val = static_cast<const float *>(field_ptr);
|
||||
ProtoSize::add_fixed_field<4>(total_size, precalced_field_tag_size, *val != 0.0f || force, force);
|
||||
ProtoSize::add_fixed_field<4>(total_size, precalced_field_id_size, *val != 0.0f || force, force);
|
||||
}
|
||||
|
||||
void size_int32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_int32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *val = static_cast<const int32_t *>(field_ptr);
|
||||
ProtoSize::add_int32_field(total_size, precalced_field_tag_size, *val, force);
|
||||
ProtoSize::add_int32_field(total_size, precalced_field_id_size, *val, force);
|
||||
}
|
||||
|
||||
void size_uint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_uint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *val = static_cast<const uint32_t *>(field_ptr);
|
||||
ProtoSize::add_uint32_field(total_size, precalced_field_tag_size, *val, force);
|
||||
ProtoSize::add_uint32_field(total_size, precalced_field_id_size, *val, force);
|
||||
}
|
||||
|
||||
void size_int64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_int64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *val = static_cast<const int64_t *>(field_ptr);
|
||||
ProtoSize::add_int64_field(total_size, precalced_field_tag_size, *val, force);
|
||||
ProtoSize::add_int64_field(total_size, precalced_field_id_size, *val, force);
|
||||
}
|
||||
|
||||
void size_uint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_uint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *val = static_cast<const uint64_t *>(field_ptr);
|
||||
ProtoSize::add_uint64_field(total_size, precalced_field_tag_size, *val, force);
|
||||
ProtoSize::add_uint64_field(total_size, precalced_field_id_size, *val, force);
|
||||
}
|
||||
|
||||
void size_sint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_sint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *val = static_cast<const int32_t *>(field_ptr);
|
||||
ProtoSize::add_sint32_field(total_size, precalced_field_tag_size, *val, force);
|
||||
ProtoSize::add_sint32_field(total_size, precalced_field_id_size, *val, force);
|
||||
}
|
||||
|
||||
void size_sint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_sint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *val = static_cast<const int64_t *>(field_ptr);
|
||||
ProtoSize::add_sint64_field(total_size, precalced_field_tag_size, *val, force);
|
||||
ProtoSize::add_sint64_field(total_size, precalced_field_id_size, *val, force);
|
||||
}
|
||||
|
||||
void size_fixed64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_fixed64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *val = static_cast<const uint64_t *>(field_ptr);
|
||||
ProtoSize::add_fixed_field<8>(total_size, precalced_field_tag_size, *val != 0 || force, force);
|
||||
ProtoSize::add_fixed_field<8>(total_size, precalced_field_id_size, *val != 0 || force, force);
|
||||
}
|
||||
|
||||
void size_double_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_double_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *val = static_cast<const double *>(field_ptr);
|
||||
ProtoSize::add_fixed_field<8>(total_size, precalced_field_tag_size, *val != 0.0 || force, force);
|
||||
ProtoSize::add_fixed_field<8>(total_size, precalced_field_id_size, *val != 0.0 || force, force);
|
||||
}
|
||||
|
||||
void size_bytes_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
void size_bytes_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *str = static_cast<const std::string *>(field_ptr);
|
||||
ProtoSize::add_string_field(total_size, precalced_field_tag_size, *str, force);
|
||||
ProtoSize::add_string_field(total_size, precalced_field_id_size, *str, force);
|
||||
}
|
||||
|
||||
// Type-specific decode functions
|
||||
@ -401,91 +401,91 @@ void encode_repeated_double_field(ProtoWriteBuffer &buffer, const void *field_pt
|
||||
// Template repeated field functions moved to header
|
||||
|
||||
// Repeated field size calculation functions
|
||||
void size_repeated_string_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
void size_repeated_string_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<std::string> *>(field_ptr);
|
||||
for (const auto &item : *vec) {
|
||||
ProtoSize::add_string_field(total_size, precalced_field_tag_size, item, true);
|
||||
ProtoSize::add_string_field(total_size, precalced_field_id_size, item, true);
|
||||
}
|
||||
}
|
||||
|
||||
void size_repeated_bool_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
void size_repeated_bool_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<bool> *>(field_ptr);
|
||||
for (bool val : *vec) {
|
||||
ProtoSize::add_bool_field(total_size, precalced_field_tag_size, val, true);
|
||||
ProtoSize::add_bool_field(total_size, precalced_field_id_size, val, true);
|
||||
}
|
||||
}
|
||||
|
||||
void size_repeated_uint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
void size_repeated_uint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<uint32_t> *>(field_ptr);
|
||||
for (const auto &val : *vec) {
|
||||
ProtoSize::add_uint32_field(total_size, precalced_field_tag_size, val, true);
|
||||
ProtoSize::add_uint32_field(total_size, precalced_field_id_size, val, true);
|
||||
}
|
||||
}
|
||||
|
||||
void size_repeated_int32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
void size_repeated_int32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<int32_t> *>(field_ptr);
|
||||
for (const auto &val : *vec) {
|
||||
ProtoSize::add_int32_field(total_size, precalced_field_tag_size, val, true);
|
||||
ProtoSize::add_int32_field(total_size, precalced_field_id_size, val, true);
|
||||
}
|
||||
}
|
||||
|
||||
void size_repeated_uint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
void size_repeated_uint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<uint64_t> *>(field_ptr);
|
||||
for (const auto &val : *vec) {
|
||||
ProtoSize::add_uint64_field(total_size, precalced_field_tag_size, val, true);
|
||||
ProtoSize::add_uint64_field(total_size, precalced_field_id_size, val, true);
|
||||
}
|
||||
}
|
||||
|
||||
void size_repeated_int64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
void size_repeated_int64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<int64_t> *>(field_ptr);
|
||||
for (const auto &val : *vec) {
|
||||
ProtoSize::add_int64_field(total_size, precalced_field_tag_size, val, true);
|
||||
ProtoSize::add_int64_field(total_size, precalced_field_id_size, val, true);
|
||||
}
|
||||
}
|
||||
|
||||
void size_repeated_sint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
void size_repeated_sint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<int32_t> *>(field_ptr);
|
||||
for (const auto &val : *vec) {
|
||||
ProtoSize::add_sint32_field(total_size, precalced_field_tag_size, val, true);
|
||||
ProtoSize::add_sint32_field(total_size, precalced_field_id_size, val, true);
|
||||
}
|
||||
}
|
||||
|
||||
void size_repeated_sint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
void size_repeated_sint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<int64_t> *>(field_ptr);
|
||||
for (const auto &val : *vec) {
|
||||
ProtoSize::add_sint64_field(total_size, precalced_field_tag_size, val, true);
|
||||
ProtoSize::add_sint64_field(total_size, precalced_field_id_size, val, true);
|
||||
}
|
||||
}
|
||||
|
||||
void size_repeated_fixed32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
void size_repeated_fixed32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<uint32_t> *>(field_ptr);
|
||||
size_t count = vec->size();
|
||||
if (count > 0) {
|
||||
total_size += count * (precalced_field_tag_size + 4); // field tag + 4 bytes per item
|
||||
total_size += count * (precalced_field_id_size + 4); // field tag + 4 bytes per item
|
||||
}
|
||||
}
|
||||
|
||||
void size_repeated_fixed64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
void size_repeated_fixed64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<uint64_t> *>(field_ptr);
|
||||
size_t count = vec->size();
|
||||
if (count > 0) {
|
||||
total_size += count * (precalced_field_tag_size + 8); // field tag + 8 bytes per item
|
||||
total_size += count * (precalced_field_id_size + 8); // field tag + 8 bytes per item
|
||||
}
|
||||
}
|
||||
|
||||
void size_repeated_float_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
void size_repeated_float_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<float> *>(field_ptr);
|
||||
size_t count = vec->size();
|
||||
if (count > 0) {
|
||||
total_size += count * (precalced_field_tag_size + 4); // field tag + 4 bytes per item
|
||||
total_size += count * (precalced_field_id_size + 4); // field tag + 4 bytes per item
|
||||
}
|
||||
}
|
||||
|
||||
void size_repeated_double_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
void size_repeated_double_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<double> *>(field_ptr);
|
||||
size_t count = vec->size();
|
||||
if (count > 0) {
|
||||
total_size += count * (precalced_field_tag_size + 8); // field tag + 8 bytes per item
|
||||
total_size += count * (precalced_field_id_size + 8); // field tag + 8 bytes per item
|
||||
}
|
||||
}
|
||||
|
||||
@ -516,13 +516,13 @@ void calculate_size_from_metadata(uint32_t &total_size, const void *obj, const F
|
||||
// Calculate size for regular fields
|
||||
for (size_t i = 0; i < field_count; i++) {
|
||||
const void *field_addr = base + fields[i].offset;
|
||||
fields[i].sizer(total_size, field_addr, fields[i].precalced_field_tag_size, fields[i].force_encode);
|
||||
fields[i].sizer(total_size, field_addr, fields[i].precalced_field_id_size, fields[i].force_encode);
|
||||
}
|
||||
|
||||
// Calculate size for repeated fields
|
||||
for (size_t i = 0; i < repeated_count; i++) {
|
||||
const void *field_addr = base + repeated_fields[i].offset;
|
||||
repeated_fields[i].sizer(total_size, field_addr, repeated_fields[i].precalced_field_tag_size);
|
||||
repeated_fields[i].sizer(total_size, field_addr, repeated_fields[i].precalced_field_id_size);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ class ProtoWriteBuffer;
|
||||
|
||||
// Function pointer types for encoding and size calculation
|
||||
using EncodeFunc = void (*)(ProtoWriteBuffer &, const void *field_ptr, uint8_t field_num);
|
||||
using SizeFunc = void (*)(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
using SizeFunc = void (*)(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
|
||||
// Macro to calculate field offset without triggering -Winvalid-offsetof
|
||||
// This uses the same approach as offsetof but with explicit reinterpret_cast
|
||||
@ -27,15 +27,15 @@ using SizeFunc = void (*)(uint32_t &total_size, const void *field_ptr, uint8_t p
|
||||
|
||||
// Function pointer types for repeated fields
|
||||
using RepeatedEncodeFunc = void (*)(ProtoWriteBuffer &, const void *field_ptr, uint8_t field_num);
|
||||
using RepeatedSizeFunc = void (*)(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
using RepeatedSizeFunc = void (*)(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
|
||||
// Special metadata for repeated fields
|
||||
struct RepeatedFieldMeta {
|
||||
uint8_t field_num;
|
||||
uint16_t offset;
|
||||
RepeatedEncodeFunc encoder; // Encoder for the entire vector
|
||||
RepeatedSizeFunc sizer; // Sizer for the entire vector
|
||||
uint8_t precalced_field_tag_size; // Pre-calculated size of field tag in bytes
|
||||
RepeatedEncodeFunc encoder; // Encoder for the entire vector
|
||||
RepeatedSizeFunc sizer; // Sizer for the entire vector
|
||||
uint8_t precalced_field_id_size; // Pre-calculated size of field tag in bytes
|
||||
};
|
||||
|
||||
/// Representation of a VarInt - in ProtoBuf should be 64bit but we only use 32bit
|
||||
@ -217,13 +217,13 @@ using Decode64BitFunc = bool (*)(void *field_ptr, Proto64Bit value);
|
||||
|
||||
// Metadata structure describing each field
|
||||
struct FieldMeta {
|
||||
uint8_t field_num; // Protobuf field number (1-255)
|
||||
uint16_t offset; // offset of field in class
|
||||
EncodeFunc encoder; // Function to encode this field type
|
||||
SizeFunc sizer; // Function to calculate size for this field type
|
||||
bool force_encode; // If true, encode even if value is default/empty
|
||||
uint8_t wire_type; // Wire type (0=varint, 2=length, 5=32bit, 1=64bit)
|
||||
uint8_t precalced_field_tag_size; // Pre-calculated size of field tag in bytes
|
||||
uint8_t field_num; // Protobuf field number (1-255)
|
||||
uint16_t offset; // offset of field in class
|
||||
EncodeFunc encoder; // Function to encode this field type
|
||||
SizeFunc sizer; // Function to calculate size for this field type
|
||||
bool force_encode; // If true, encode even if value is default/empty
|
||||
uint8_t wire_type; // Wire type (0=varint, 2=length, 5=32bit, 1=64bit)
|
||||
uint8_t precalced_field_id_size; // Pre-calculated size of field tag in bytes
|
||||
union {
|
||||
DecodeVarintFunc decode_varint;
|
||||
DecodeLengthFunc decode_length;
|
||||
@ -512,25 +512,25 @@ bool decode_bytes_field(void *field_ptr, ProtoLengthDelimited value);
|
||||
template<typename EnumType> bool decode_enum_field(void *field_ptr, ProtoVarInt value);
|
||||
|
||||
// Type-specific size calculation functions
|
||||
void size_string_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_fixed32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_bool_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_float_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_int32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_uint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_int64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_uint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_sint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_sint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_fixed64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_double_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_bytes_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_string_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
void size_fixed32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
void size_bool_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
void size_float_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
void size_int32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
void size_uint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
void size_int64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
void size_uint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
void size_sint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
void size_sint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
void size_fixed64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
void size_double_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
void size_bytes_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
|
||||
// Template enum field functions
|
||||
template<typename EnumType> void encode_enum_field(ProtoWriteBuffer &buffer, const void *field_ptr, uint8_t field_num);
|
||||
|
||||
template<typename EnumType>
|
||||
void size_enum_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force);
|
||||
void size_enum_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force);
|
||||
|
||||
// Repeated field handling functions
|
||||
void encode_repeated_string_field(ProtoWriteBuffer &buffer, const void *field_ptr, uint8_t field_num);
|
||||
@ -553,24 +553,24 @@ template<typename MessageType>
|
||||
void encode_repeated_message_field(ProtoWriteBuffer &buffer, const void *field_ptr, uint8_t field_num);
|
||||
|
||||
// Size calculation for repeated fields
|
||||
void size_repeated_string_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_bool_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_uint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_int32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_uint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_int64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_sint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_sint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_fixed32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_fixed64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_float_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_double_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_string_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
void size_repeated_bool_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
void size_repeated_uint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
void size_repeated_int32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
void size_repeated_uint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
void size_repeated_int64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
void size_repeated_sint32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
void size_repeated_sint64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
void size_repeated_fixed32_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
void size_repeated_fixed64_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
void size_repeated_float_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
void size_repeated_double_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
|
||||
template<typename EnumType>
|
||||
void size_repeated_enum_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_enum_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
|
||||
template<typename MessageType>
|
||||
void size_repeated_message_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size);
|
||||
void size_repeated_message_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size);
|
||||
|
||||
// Core shared functions
|
||||
void encode_from_metadata(ProtoWriteBuffer buffer, const void *obj, const FieldMeta *fields, size_t field_count,
|
||||
|
@ -14,9 +14,9 @@ inline void encode_enum_field(ProtoWriteBuffer &buffer, const void *field_ptr, u
|
||||
}
|
||||
|
||||
template<typename EnumType>
|
||||
inline void size_enum_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size, bool force) {
|
||||
inline void size_enum_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size, bool force) {
|
||||
const auto *val = static_cast<const EnumType *>(field_ptr);
|
||||
ProtoSize::add_enum_field(total_size, precalced_field_tag_size, static_cast<uint32_t>(*val), force);
|
||||
ProtoSize::add_enum_field(total_size, precalced_field_id_size, static_cast<uint32_t>(*val), force);
|
||||
}
|
||||
|
||||
template<typename EnumType> inline bool decode_enum_field(void *field_ptr, ProtoVarInt value) {
|
||||
@ -44,17 +44,17 @@ inline void encode_repeated_message_field(ProtoWriteBuffer &buffer, const void *
|
||||
|
||||
// Template size functions for repeated fields (must be in header for instantiation)
|
||||
template<typename EnumType>
|
||||
inline void size_repeated_enum_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
inline void size_repeated_enum_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<EnumType> *>(field_ptr);
|
||||
for (const auto &val : *vec) {
|
||||
ProtoSize::add_enum_field(total_size, precalced_field_tag_size, static_cast<uint32_t>(val), true);
|
||||
ProtoSize::add_enum_field(total_size, precalced_field_id_size, static_cast<uint32_t>(val), true);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename MessageType>
|
||||
inline void size_repeated_message_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_tag_size) {
|
||||
inline void size_repeated_message_field(uint32_t &total_size, const void *field_ptr, uint8_t precalced_field_id_size) {
|
||||
const auto *vec = static_cast<const std::vector<MessageType> *>(field_ptr);
|
||||
ProtoSize::add_repeated_message<MessageType>(total_size, precalced_field_tag_size, *vec);
|
||||
ProtoSize::add_repeated_message<MessageType>(total_size, precalced_field_id_size, *vec);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
@ -1242,7 +1242,7 @@ def build_message_type(
|
||||
if encoder and sizer and decoder:
|
||||
# Calculate pre-calculated field tag size
|
||||
field_tag_size = ti.calculate_field_id_size()
|
||||
# Format: {field_num, offset, encoder, sizer, force_encode, wire_type, precalced_field_tag_size, {decoder}}
|
||||
# Format: {field_num, offset, encoder, sizer, force_encode, wire_type, precalced_field_id_size, {decoder}}
|
||||
decoder_field = (
|
||||
f".decode_varint = {decoder}"
|
||||
if wire_type == 0
|
||||
|
Loading…
x
Reference in New Issue
Block a user