From 1652ea8b97d939e7ce5ab3cda335adc74780f2ae Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 26 Oct 2025 12:14:01 -0700 Subject: [PATCH] overkill --- esphome/core/automation.h | 54 +++++++++++++-------------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/esphome/core/automation.h b/esphome/core/automation.h index 82dce303d2..d357c6f58e 100644 --- a/esphome/core/automation.h +++ b/esphome/core/automation.h @@ -47,36 +47,24 @@ template class TemplatableValue { // Copy constructor TemplatableValue(const TemplatableValue &other) : type_(other.type_) { - switch (type_) { - case VALUE: - new (&this->value_) T(other.value_); - break; - case LAMBDA: - this->f_ = new std::function(*other.f_); - break; - case STATELESS_LAMBDA: - this->stateless_f_ = other.stateless_f_; - break; - case NONE: - break; + if (type_ == VALUE) { + new (&this->value_) T(other.value_); + } else if (type_ == LAMBDA) { + this->f_ = new std::function(*other.f_); + } else if (type_ == STATELESS_LAMBDA) { + this->stateless_f_ = other.stateless_f_; } } // Move constructor TemplatableValue(TemplatableValue &&other) noexcept : type_(other.type_) { - switch (type_) { - case VALUE: - new (&this->value_) T(std::move(other.value_)); - break; - case LAMBDA: - this->f_ = other.f_; - other.f_ = nullptr; - break; - case STATELESS_LAMBDA: - this->stateless_f_ = other.stateless_f_; - break; - case NONE: - break; + if (type_ == VALUE) { + new (&this->value_) T(std::move(other.value_)); + } else if (type_ == LAMBDA) { + this->f_ = other.f_; + other.f_ = nullptr; + } else if (type_ == STATELESS_LAMBDA) { + this->stateless_f_ = other.stateless_f_; } other.type_ = NONE; } @@ -99,18 +87,12 @@ template class TemplatableValue { } ~TemplatableValue() { - switch (type_) { - case VALUE: - this->value_.~T(); - break; - case LAMBDA: - delete this->f_; - break; - case STATELESS_LAMBDA: - case NONE: - // No cleanup needed (function pointer or empty, not heap-allocated) - break; + if (type_ == VALUE) { + this->value_.~T(); + } else if (type_ == LAMBDA) { + delete this->f_; } + // STATELESS_LAMBDA/NONE: no cleanup needed (function pointer or empty, not heap-allocated) } bool has_value() { return this->type_ != NONE; }