ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
esphome::CallbackManager< void(Ts...)> Class Template Reference

Helper class to allow having multiple subscribers to a callback. More...

#include <helpers.h>

Public Member Functions

 CallbackManager ()=default
 
 ~CallbackManager ()
 
 CallbackManager (const CallbackManager &)=delete
 
CallbackManageroperator= (const CallbackManager &)=delete
 
 CallbackManager (CallbackManager &&other) noexcept
 
CallbackManageroperator= (CallbackManager &&other) noexcept
 
template<typename F >
void add (F &&callback)
 Add any callable.
 
void ESPHOME_ALWAYS_INLINE call (Ts... args)
 Call all callbacks in this manager.
 
uint16_t size () const
 
void operator() (Ts... args)
 Call all callbacks in this manager.
 

Protected Member Functions

void add_ (CbType cb)
 Non-template core to avoid code duplication per lambda type.
 

Protected Attributes

CbTypedata_ {nullptr}
 
uint16_t size_ {0}
 
uint16_t capacity_ {0}
 

Detailed Description

template<typename... Ts>
class esphome::CallbackManager< void(Ts...)>

Helper class to allow having multiple subscribers to a callback.

Uses a trivial-copyable-specialized container instead of std::vector to avoid template bloat (_M_realloc_insert, exception-safe copies). Since Callback is trivially copyable (just {fn_ptr, ctx_ptr}), reallocation is a plain memcpy. Uses uint16_t for size/capacity (8 bytes on 32-bit vs 12 for std::vector). Grows to exact size on each add — callbacks are registered during setup() and most instances have only 1-2 callbacks, so slack capacity is wasteful.

Template Parameters
TsThe arguments for the callbacks, wrapped in void().

Definition at line 1835 of file helpers.h.

Constructor & Destructor Documentation

◆ CallbackManager() [1/3]

template<typename... Ts>
esphome::CallbackManager< void(Ts...)>::CallbackManager ( )
default

◆ ~CallbackManager()

template<typename... Ts>
esphome::CallbackManager< void(Ts...)>::~CallbackManager ( )
inline

Definition at line 1841 of file helpers.h.

◆ CallbackManager() [2/3]

template<typename... Ts>
esphome::CallbackManager< void(Ts...)>::CallbackManager ( const CallbackManager< void(Ts...)> & )
delete

◆ CallbackManager() [3/3]

template<typename... Ts>
esphome::CallbackManager< void(Ts...)>::CallbackManager ( CallbackManager< void(Ts...)> && other)
inlinenoexcept

Definition at line 1846 of file helpers.h.

Member Function Documentation

◆ add()

template<typename... Ts>
template<typename F >
void esphome::CallbackManager< void(Ts...)>::add ( F && callback)
inline

Add any callable.

Small trivially-copyable callables (like [this] lambdas) are stored inline without heap allocation or std::function.

Definition at line 1861 of file helpers.h.

◆ add_()

template<typename... Ts>
void esphome::CallbackManager< void(Ts...)>::add_ ( CbType cb)
inlineprotected

Non-template core to avoid code duplication per lambda type.

Inline fast path; cold growth path is in helpers.cpp via callback_manager_grow().

Definition at line 1880 of file helpers.h.

◆ call()

template<typename... Ts>
void ESPHOME_ALWAYS_INLINE esphome::CallbackManager< void(Ts...)>::call ( Ts... args)
inline

Call all callbacks in this manager.

Definition at line 1864 of file helpers.h.

◆ operator()()

template<typename... Ts>
void esphome::CallbackManager< void(Ts...)>::operator() ( Ts... args)
inline

Call all callbacks in this manager.

Definition at line 1874 of file helpers.h.

◆ operator=() [1/2]

template<typename... Ts>
CallbackManager & esphome::CallbackManager< void(Ts...)>::operator= ( CallbackManager< void(Ts...)> && other)
inlinenoexcept

Definition at line 1852 of file helpers.h.

◆ operator=() [2/2]

template<typename... Ts>
CallbackManager & esphome::CallbackManager< void(Ts...)>::operator= ( const CallbackManager< void(Ts...)> & )
delete

◆ size()

template<typename... Ts>
uint16_t esphome::CallbackManager< void(Ts...)>::size ( ) const
inline

Definition at line 1871 of file helpers.h.

Field Documentation

◆ capacity_

template<typename... Ts>
uint16_t esphome::CallbackManager< void(Ts...)>::capacity_ {0}
protected

Definition at line 1889 of file helpers.h.

◆ data_

template<typename... Ts>
CbType* esphome::CallbackManager< void(Ts...)>::data_ {nullptr}
protected

Definition at line 1887 of file helpers.h.

◆ size_

template<typename... Ts>
uint16_t esphome::CallbackManager< void(Ts...)>::size_ {0}
protected

Definition at line 1888 of file helpers.h.


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