idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
Event Loop Component

Type-safe event loop management. More...

Namespaces

namespace  idfxx
 

Classes

class  idfxx::event_base< IdEnum >
 Typed event base template. More...
 
struct  idfxx::event_type< IdEnum >
 Combines an event base with a specific event ID. More...
 
class  idfxx::event_loop
 Base class for event loops. More...
 
class  idfxx::event_loop::listener_handle
 Handle to a registered event listener. More...
 
class  idfxx::event_loop::unique_listener_handle
 RAII handle for event listener registration. More...
 
class  idfxx::user_event_loop
 User-created event loop with manual dispatch. More...
 

Macros

#define IDFXX_EVENT_DEFINE_BASE(name, id_enum)
 Defines an event base.
 

Typedefs

template<typename IdEnum >
using idfxx::event_callback = std::move_only_function< void(event_base< IdEnum > base, IdEnum id, void *event_data) const >
 Callback type for event listeners.
 

Functions

template<typename IdEnum >
 idfxx::event_type (event_base< IdEnum >, IdEnum) -> event_type< IdEnum >
 
constexpr event_type< IdEnum > idfxx::event_base< IdEnum >::operator() (IdEnum id) const
 Creates an event_type combining this base with a specific ID.
 
static std::unique_ptr< user_event_loopidfxx::event_loop::make_user (size_t queue_size=32)
 Creates a user event loop without a dedicated task.
 
static std::unique_ptr< event_loopidfxx::event_loop::make_user (task_config task, size_t queue_size=32)
 Creates a user event loop with a dedicated dispatch task.
 
template<typename IdEnum >
listener_handle idfxx::event_loop::listener_add (event_base< IdEnum > base, IdEnum id, event_callback< std::type_identity_t< IdEnum > > callback)
 Registers a typed listener for a specific event.
 
template<typename IdEnum >
listener_handle idfxx::event_loop::listener_add (event_base< IdEnum > base, event_callback< std::type_identity_t< IdEnum > > callback)
 Registers a typed listener for any event from a base.
 
template<typename IdEnum >
listener_handle idfxx::event_loop::listener_add (event_type< IdEnum > event, event_callback< std::type_identity_t< IdEnum > > callback)
 Registers a typed listener for a specific event.
 
void idfxx::event_loop::listener_remove (listener_handle handle)
 Removes a listener by handle.
 
template<typename IdEnum >
result< listener_handleidfxx::event_loop::try_listener_add (event_base< IdEnum > base, IdEnum id, event_callback< std::type_identity_t< IdEnum > > callback)
 Registers a typed listener for a specific event.
 
template<typename IdEnum >
result< listener_handleidfxx::event_loop::try_listener_add (event_base< IdEnum > base, event_callback< std::type_identity_t< IdEnum > > callback)
 Registers a typed listener for any event from a base.
 
template<typename IdEnum >
result< listener_handleidfxx::event_loop::try_listener_add (event_type< IdEnum > event, event_callback< std::type_identity_t< IdEnum > > callback)
 Registers a typed listener for a specific event.
 

Detailed Description

Type-safe event loop management.

Provides event loops for posting and subscribing to events with type-safe event bases and IDs.

Depends on Core Utilities for error handling.

Macro Definition Documentation

◆ IDFXX_EVENT_DEFINE_BASE

#define IDFXX_EVENT_DEFINE_BASE (   name,
  id_enum 
)
Value:
inline constexpr char _##name##_base[] = #name; \
inline constexpr ::idfxx::event_base<id_enum> name { \
_##name##_base \
}

Defines an event base.

Creates an event_base with the given name and enum type. The event base name string is automatically derived from the variable name.

Parameters
nameThe variable name for the event base.
id_enumThe enum class for event IDs within this base.
// Define event IDs
enum class app_event : int32_t { started, stopped };
// Define the event base (typically in a header)
IDFXX_EVENT_DEFINE_BASE(app_events, app_event);
// Use it
loop.post(app_events, app_event::started);
#define IDFXX_EVENT_DEFINE_BASE(name, id_enum)
Defines an event base.
Definition event.hpp:50

Definition at line 50 of file event.hpp.

Typedef Documentation

◆ event_callback

template<typename IdEnum >
using idfxx::event_callback = typedef std::move_only_function<void(event_base<IdEnum> base, IdEnum id, void* event_data) const>

Callback type for event listeners.

Invoked when an event matching the registered base and/or ID is dispatched.

Template Parameters
IdEnumThe event ID enum type.

Definition at line 186 of file event.hpp.

Function Documentation

◆ event_type()

template<typename IdEnum >
idfxx::event_type ( event_base< IdEnum >  ,
IdEnum   
) -> event_type< IdEnum >

◆ listener_add() [1/3]

template<typename IdEnum >
event_loop::listener_handle idfxx::event_loop::listener_add ( event_base< IdEnum >  base,
event_callback< std::type_identity_t< IdEnum > >  callback 
)

Registers a typed listener for any event from a base.

Template Parameters
IdEnumThe event ID enum type.
Parameters
baseThe event base.
callbackFunction called when any event from this base occurs. Takes ownership of the callback.
Returns
A listener handle for the registered listener.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

Definition at line 852 of file event.hpp.

References idfxx::event_loop::try_listener_add(), and idfxx::unwrap().

◆ listener_add() [2/3]

template<typename IdEnum >
event_loop::listener_handle idfxx::event_loop::listener_add ( event_base< IdEnum >  base,
IdEnum  id,
event_callback< std::type_identity_t< IdEnum > >  callback 
)

Registers a typed listener for a specific event.

Template Parameters
IdEnumThe event ID enum type.
Parameters
baseThe event base.
idThe specific event ID to listen for.
callbackFunction called when the event occurs. Takes ownership of the callback.
Returns
A listener handle for the registered listener.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

Definition at line 846 of file event.hpp.

References idfxx::event_loop::try_listener_add(), and idfxx::unwrap().

◆ listener_add() [3/3]

template<typename IdEnum >
event_loop::listener_handle idfxx::event_loop::listener_add ( event_type< IdEnum >  event,
event_callback< std::type_identity_t< IdEnum > >  callback 
)

Registers a typed listener for a specific event.

Template Parameters
IdEnumThe event ID enum type.
Parameters
eventThe event type (base + id).
callbackFunction called when the event occurs. Takes ownership of the callback.
Returns
A listener handle for the registered listener.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

Definition at line 858 of file event.hpp.

References idfxx::event_loop::try_listener_add(), and idfxx::unwrap().

◆ listener_remove()

void idfxx::event_loop::listener_remove ( listener_handle  handle)
inline

Removes a listener by handle.

Parameters
handleThe listener handle to remove.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

Definition at line 862 of file event.hpp.

References idfxx::event_loop::try_listener_remove(), and idfxx::unwrap().

◆ make_user() [1/2]

std::unique_ptr< user_event_loop > idfxx::event_loop::make_user ( size_t  queue_size = 32)
inlinestatic

Creates a user event loop without a dedicated task.

Events must be dispatched manually via user_event_loop::run().

Parameters
queue_sizeMaximum number of events in the queue.
Returns
A user event loop with manual dispatch support.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

Definition at line 834 of file event.hpp.

References idfxx::event_loop::try_make_user().

◆ make_user() [2/2]

std::unique_ptr< event_loop > idfxx::event_loop::make_user ( task_config  task,
size_t  queue_size = 32 
)
inlinestatic

Creates a user event loop with a dedicated dispatch task.

The task automatically dispatches events from the queue.

Parameters
taskConfiguration for the dedicated dispatch task.
queue_sizeMaximum number of events in the queue.
Returns
An event loop with automatic dispatch.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

Definition at line 839 of file event.hpp.

References idfxx::event_loop::try_make_user().

◆ operator()()

template<typename IdEnum >
constexpr event_type< IdEnum > idfxx::event_base< IdEnum >::operator() ( IdEnum  id) const
constexpr

Creates an event_type combining this base with a specific ID.

Parameters
idThe event ID.
Returns
An event_type pairing this base with the ID.
auto evt = events::wifi(WIFI_EVENT_STA_START);
loop.try_listener_add(evt, callback);

Definition at line 173 of file event.hpp.

◆ try_listener_add() [1/3]

template<typename IdEnum >
result< event_loop::listener_handle > idfxx::event_loop::try_listener_add ( event_base< IdEnum >  base,
event_callback< std::type_identity_t< IdEnum > >  callback 
)

Registers a typed listener for any event from a base.

Template Parameters
IdEnumThe event ID enum type.
Parameters
baseThe event base.
callbackFunction called when any event from this base occurs. Takes ownership of the callback.
Returns
A listener handle, or an error.

Definition at line 890 of file event.hpp.

References idfxx::event_base< IdEnum >::idf_base().

◆ try_listener_add() [2/3]

template<typename IdEnum >
result< event_loop::listener_handle > idfxx::event_loop::try_listener_add ( event_base< IdEnum >  base,
IdEnum  id,
event_callback< std::type_identity_t< IdEnum > >  callback 
)

Registers a typed listener for a specific event.

Template Parameters
IdEnumThe event ID enum type.
Parameters
baseThe event base.
idThe specific event ID to listen for.
callbackFunction called when the event occurs. Takes ownership of the callback.
Returns
A listener handle, or an error.

Definition at line 880 of file event.hpp.

References idfxx::event_base< IdEnum >::idf_base().

Referenced by idfxx::event_loop::listener_add(), idfxx::event_loop::listener_add(), idfxx::event_loop::listener_add(), and idfxx::event_loop::try_listener_add().

◆ try_listener_add() [3/3]

template<typename IdEnum >
result< event_loop::listener_handle > idfxx::event_loop::try_listener_add ( event_type< IdEnum >  event,
event_callback< std::type_identity_t< IdEnum > >  callback 
)

Registers a typed listener for a specific event.

Template Parameters
IdEnumThe event ID enum type.
Parameters
eventThe event type (base + id).
callbackFunction called when the event occurs. Takes ownership of the callback.
Returns
A listener handle, or an error.

Definition at line 896 of file event.hpp.

References idfxx::event_type< IdEnum >::base, idfxx::event_type< IdEnum >::id, and idfxx::event_loop::try_listener_add().