ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
template_lambda.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace esphome {
6
24template<typename T, typename... Args> class TemplateLambda {
25 public:
26 TemplateLambda() : f_(nullptr) {}
27
32 void set(optional<T> (*f)(Args...)) { this->f_ = f; }
33
35 bool has_value() const { return this->f_ != nullptr; }
36
38 optional<T> operator()(Args &&...args) {
39 if (this->f_ == nullptr)
40 return nullopt;
41 return this->f_(std::forward<Args>(args)...);
42 }
43
45 optional<T> call(Args &&...args) { return (*this)(std::forward<Args>(args)...); }
46
47 protected:
48 optional<T> (*f_)(Args...); // Function pointer (4 bytes on ESP32)
49};
50
51} // namespace esphome
Lightweight wrapper for template platform lambdas (stateless function pointers only).
optional< T > operator()(Args &&...args)
Call the lambda, returning nullopt if no lambda is set.
optional< T > call(Args &&...args)
Alias for operator() for compatibility.
optional< T >(* f_)(Args...)
bool has_value() const
Check if a lambda is set.
void set(optional< T >(*f)(Args...))
Set the lambda function pointer.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const nullopt_t nullopt((nullopt_t::init()))