mirror of
https://github.com/esphome/esphome.git
synced 2025-11-07 18:41:53 +00:00
simplify
This commit is contained in:
@@ -609,35 +609,26 @@ void ClimateDeviceRestoreState::apply(Climate *climate) {
|
|||||||
climate->publish_state();
|
climate->publish_state();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T1, typename T2> bool set_alternative(optional<T1> &dst, optional<T2> &alt, const T1 &src) {
|
// Generic template to set one value while clearing its alternative (mutual exclusion)
|
||||||
bool is_changed = alt.has_value();
|
// Handles both optional<T> and const char* types automatically using compile-time type detection
|
||||||
alt.reset();
|
template<typename T1, typename T2, typename T3> bool set_alternative(T1 &dst, T2 &alt, T3 src) {
|
||||||
if (is_changed || dst != src) {
|
bool is_changed = false;
|
||||||
dst = src;
|
|
||||||
is_changed = true;
|
|
||||||
}
|
|
||||||
return is_changed;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Overload for optional + const char* pointer
|
// Clear the alternative based on its type (pointer or optional)
|
||||||
template<typename T> bool set_alternative(optional<T> &dst, const char *&alt, const T &src) {
|
if constexpr (std::is_pointer_v<std::remove_reference_t<T2>>) {
|
||||||
bool is_changed = (alt != nullptr);
|
is_changed = (alt != nullptr);
|
||||||
alt = nullptr;
|
alt = nullptr;
|
||||||
if (is_changed || dst != src) {
|
} else {
|
||||||
dst = src;
|
is_changed = alt.has_value();
|
||||||
is_changed = true;
|
|
||||||
}
|
|
||||||
return is_changed;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Overload for const char* pointer + optional
|
|
||||||
template<typename T> bool set_alternative(const char *&dst, optional<T> &alt, const char *src) {
|
|
||||||
bool is_changed = alt.has_value();
|
|
||||||
alt.reset();
|
alt.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the destination value
|
||||||
if (is_changed || dst != src) {
|
if (is_changed || dst != src) {
|
||||||
dst = src;
|
dst = src;
|
||||||
is_changed = true;
|
is_changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return is_changed;
|
return is_changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user