1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-31 07:03:55 +00:00
This commit is contained in:
J. Nick Koston
2025-10-26 12:14:01 -07:00
parent 48b45ba439
commit 1652ea8b97

View File

@@ -47,36 +47,24 @@ template<typename T, typename... X> 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<T(X...)>(*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<T(X...)>(*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<typename T, typename... X> 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; }