ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
esphome::TemplateLambda< T, Args > Class Template Reference

Lightweight wrapper for template platform lambdas (stateless function pointers only). More...

#include <template_lambda.h>

Public Member Functions

 TemplateLambda ()
 
void set (optional< T >(*f)(Args...))
 Set the lambda function pointer.
 
bool has_value () const
 Check if a lambda is set.
 
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.
 

Protected Attributes

optional< T >(* f_ )(Args...)
 

Detailed Description

template<typename T, typename... Args>
class esphome::TemplateLambda< T, Args >

Lightweight wrapper for template platform lambdas (stateless function pointers only).

This optimizes template platforms by storing only a function pointer (4 bytes on ESP32) instead of std::function (16-32 bytes).

IMPORTANT: This only supports stateless lambdas (no captures). The set_template() method is an internal API used by YAML codegen, not intended for external use.

Lambdas must return optional<T> to support the pattern: return {}; // Don't publish a value return 42.0; // Publish this value

operator() returns optional<T>, returning nullopt when no lambda is set (nullptr check).

Template Parameters
TThe return type (e.g., float for sensor values)
ArgsOptional arguments for the lambda

Definition at line 24 of file template_lambda.h.

Constructor & Destructor Documentation

◆ TemplateLambda()

template<typename T , typename... Args>
esphome::TemplateLambda< T, Args >::TemplateLambda ( )
inline

Definition at line 26 of file template_lambda.h.

Member Function Documentation

◆ call()

template<typename T , typename... Args>
optional< T > esphome::TemplateLambda< T, Args >::call ( Args &&... args)
inline

Alias for operator() for compatibility.

Definition at line 45 of file template_lambda.h.

◆ has_value()

template<typename T , typename... Args>
bool esphome::TemplateLambda< T, Args >::has_value ( ) const
inline

Check if a lambda is set.

Definition at line 35 of file template_lambda.h.

◆ operator()()

template<typename T , typename... Args>
optional< T > esphome::TemplateLambda< T, Args >::operator() ( Args &&... args)
inline

Call the lambda, returning nullopt if no lambda is set.

Definition at line 38 of file template_lambda.h.

◆ set()

template<typename T , typename... Args>
void esphome::TemplateLambda< T, Args >::set ( optional< T >(* )(Args...))
inline

Set the lambda function pointer.

INTERNAL API: Only for use by YAML codegen. Only stateless lambdas (no captures) are supported.

Definition at line 32 of file template_lambda.h.

Field Documentation

◆ f_

template<typename T , typename... Args>
optional< T >(* esphome::TemplateLambda< T, Args >::f_) (Args...)
protected

Definition at line 45 of file template_lambda.h.


The documentation for this class was generated from the following file: