mirror of
https://github.com/esphome/esphome.git
synced 2026-02-20 16:35:37 -07:00
overkill
This commit is contained in:
@@ -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; }
|
||||
|
||||
Reference in New Issue
Block a user