1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-01 15:41:52 +00:00

Merge branch 'more_flexible_template' into integration

This commit is contained in:
J. Nick Koston
2025-10-29 15:06:38 -05:00

View File

@@ -32,7 +32,7 @@ template<typename T, typename... Args> class TemplateLambda {
template<typename F> template<typename F>
requires std::invocable<F, Args...> && std::convertible_to < F, optional<T>(*) requires std::invocable<F, Args...> && std::convertible_to < F, optional<T>(*)
(Args...) > void set(F f) { (Args...) > void set(F f) {
this->reset(); this->reset_();
this->type_ = STATELESS_LAMBDA; this->type_ = STATELESS_LAMBDA;
this->stateless_f_ = f; // Implicit conversion to function pointer this->stateless_f_ = f; // Implicit conversion to function pointer
} }
@@ -42,12 +42,12 @@ template<typename T, typename... Args> class TemplateLambda {
requires std::invocable<F, Args...> && requires std::invocable<F, Args...> &&
(!std::convertible_to<F, optional<T> (*)(Args...)>) &&std::convertible_to<std::invoke_result_t<F, Args...>, (!std::convertible_to<F, optional<T> (*)(Args...)>) &&std::convertible_to<std::invoke_result_t<F, Args...>,
optional<T>> void set(F &&f) { optional<T>> void set(F &&f) {
this->reset(); this->reset_();
this->type_ = LAMBDA; this->type_ = LAMBDA;
this->f_ = new std::function<optional<T>(Args...)>(std::forward<F>(f)); this->f_ = new std::function<optional<T>(Args...)>(std::forward<F>(f));
} }
~TemplateLambda() { this->reset(); } ~TemplateLambda() { this->reset_(); }
// Copy constructor // Copy constructor
TemplateLambda(const TemplateLambda &) = delete; TemplateLambda(const TemplateLambda &) = delete;
@@ -66,7 +66,7 @@ template<typename T, typename... Args> class TemplateLambda {
TemplateLambda &operator=(TemplateLambda &&other) noexcept { TemplateLambda &operator=(TemplateLambda &&other) noexcept {
if (this != &other) { if (this != &other) {
this->reset(); this->reset_();
this->type_ = other.type_; this->type_ = other.type_;
if (type_ == LAMBDA) { if (type_ == LAMBDA) {
this->f_ = other.f_; this->f_ = other.f_;
@@ -97,7 +97,7 @@ template<typename T, typename... Args> class TemplateLambda {
optional<T> call(Args... args) { return (*this)(args...); } optional<T> call(Args... args) { return (*this)(args...); }
protected: protected:
void reset() { void reset_() {
if (this->type_ == LAMBDA) { if (this->type_ == LAMBDA) {
delete this->f_; delete this->f_;
this->f_ = nullptr; this->f_ = nullptr;