Merge remote-tracking branch 'upstream/sensor_value_list_static_array' into integration

This commit is contained in:
J. Nick Koston
2026-03-27 16:16:58 -10:00
2 changed files with 16 additions and 6 deletions
+1 -6
View File
@@ -346,12 +346,7 @@ uint32_t get_loop_component_start_time();
template<size_t N> class ValueListFilter : public Filter {
protected:
explicit ValueListFilter(std::initializer_list<TemplatableValue<float>> values) {
size_t i = 0;
for (const auto &v : values) {
if (i >= N)
break;
this->values_[i++] = v;
}
init_array_from(this->values_, values);
}
/// Check if sensor value matches any configured value (with accuracy rounding)
+15
View File
@@ -500,6 +500,21 @@ template<typename T, size_t MAX_CAPACITY = std::numeric_limits<uint16_t>::max()>
index_type capacity_{0};
};
/// Initialize a std::array from an initializer_list. Uses memcpy for trivially copyable types (optimal codegen),
/// falls back to element-wise copy for non-trivially copyable types (e.g. TemplatableValue).
/// N is set by code generation; ESPHOME_DEBUG_ASSERT catches mismatches in debug/integration tests.
template<typename T, size_t N> inline void init_array_from(std::array<T, N> &dest, std::initializer_list<T> src) {
ESPHOME_DEBUG_ASSERT(src.size() == N);
if constexpr (std::is_trivially_copyable_v<T>) {
__builtin_memcpy(dest.data(), src.begin(), N * sizeof(T));
} else {
size_t i = 0;
for (const auto &v : src) {
dest[i++] = v;
}
}
}
/// Fixed-capacity vector - allocates once at runtime, never reallocates
/// This avoids std::vector template overhead (_M_realloc_insert, _M_default_append)
/// when size is known at initialization but not at compile time