diff --git a/esphome/core/base_automation.h b/esphome/core/base_automation.h index ba942e5e43..f1248e0035 100644 --- a/esphome/core/base_automation.h +++ b/esphome/core/base_automation.h @@ -7,6 +7,7 @@ #include "esphome/core/preferences.h" #include "esphome/core/scheduler.h" #include "esphome/core/application.h" +#include "esphome/core/helpers.h" #include @@ -14,7 +15,7 @@ namespace esphome { template class AndCondition : public Condition { public: - explicit AndCondition(const std::vector *> &conditions) : conditions_(conditions) {} + explicit AndCondition(std::initializer_list *> conditions) : conditions_(conditions) {} bool check(Ts... x) override { for (auto *condition : this->conditions_) { if (!condition->check(x...)) @@ -25,12 +26,12 @@ template class AndCondition : public Condition { } protected: - std::vector *> conditions_; + FixedVector *> conditions_; }; template class OrCondition : public Condition { public: - explicit OrCondition(const std::vector *> &conditions) : conditions_(conditions) {} + explicit OrCondition(std::initializer_list *> conditions) : conditions_(conditions) {} bool check(Ts... x) override { for (auto *condition : this->conditions_) { if (condition->check(x...)) @@ -41,7 +42,7 @@ template class OrCondition : public Condition { } protected: - std::vector *> conditions_; + FixedVector *> conditions_; }; template class NotCondition : public Condition { @@ -55,7 +56,7 @@ template class NotCondition : public Condition { template class XorCondition : public Condition { public: - explicit XorCondition(const std::vector *> &conditions) : conditions_(conditions) {} + explicit XorCondition(std::initializer_list *> conditions) : conditions_(conditions) {} bool check(Ts... x) override { size_t result = 0; for (auto *condition : this->conditions_) { @@ -66,7 +67,7 @@ template class XorCondition : public Condition { } protected: - std::vector *> conditions_; + FixedVector *> conditions_; }; template class LambdaCondition : public Condition { diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index e352c9c415..326718e974 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -197,6 +197,18 @@ template class FixedVector { public: FixedVector() = default; + /// Constructor from initializer list - allocates exact size needed + /// This enables brace initialization: FixedVector v = {1, 2, 3}; + FixedVector(std::initializer_list init_list) { + init(init_list.size()); + size_t idx = 0; + for (const auto &item : init_list) { + new (data_ + idx) T(item); + ++idx; + } + size_ = init_list.size(); + } + ~FixedVector() { cleanup_(); } // Disable copy operations (avoid accidental expensive copies)