idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
idfxx::event_loop Class Reference

Base class for event loops. More...

Inheritance diagram for idfxx::event_loop:
idfxx::user_event_loop

Classes

class  listener_handle
 Handle to a registered event listener. More...
 
struct  task_config
 Configuration for a dedicated event dispatch task. More...
 
class  unique_listener_handle
 RAII handle for event listener registration. More...
 

Public Member Functions

template<typename IdEnum >
listener_handle 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 listener_add (event_type< IdEnum > event, event_callback< std::type_identity_t< IdEnum > > callback)
 Registers a typed listener for a specific event.
 
template<typename IdEnum >
listener_handle 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_handletry_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_handletry_listener_add (event_type< IdEnum > event, event_callback< std::type_identity_t< IdEnum > > callback)
 Registers a typed listener for a specific event.
 
template<typename IdEnum >
result< listener_handletry_listener_add (event_base< IdEnum > base, event_callback< std::type_identity_t< IdEnum > > callback)
 Registers a typed listener for any event from a base.
 
void listener_remove (listener_handle handle)
 Removes a listener by handle.
 
result< void > try_listener_remove (listener_handle handle)
 Removes a listener by handle.
 
template<typename IdEnum >
void post (event_base< IdEnum > base, IdEnum id, const void *data=nullptr, size_t size=0)
 Posts a typed event, waiting indefinitely.
 
template<typename IdEnum , typename Rep , typename Period >
void post (event_base< IdEnum > base, IdEnum id, const void *data, size_t size, const std::chrono::duration< Rep, Period > &timeout)
 Posts a typed event with a timeout.
 
template<typename IdEnum >
void post (event_type< IdEnum > event, const void *data=nullptr, size_t size=0)
 Posts a typed event via event_type, waiting indefinitely.
 
template<typename IdEnum , typename Rep , typename Period >
void post (event_type< IdEnum > event, const void *data, size_t size, const std::chrono::duration< Rep, Period > &timeout)
 Posts a typed event via event_type with a timeout.
 
template<typename IdEnum >
result< void > try_post (event_base< IdEnum > base, IdEnum id, const void *data=nullptr, size_t size=0)
 Posts a typed event, waiting indefinitely.
 
template<typename IdEnum , typename Rep , typename Period >
result< void > try_post (event_base< IdEnum > base, IdEnum id, const void *data, size_t size, const std::chrono::duration< Rep, Period > &timeout)
 Posts a typed event with a timeout.
 
template<typename IdEnum >
result< void > try_post (event_type< IdEnum > event, const void *data=nullptr, size_t size=0)
 Posts a typed event via event_type, waiting indefinitely.
 
template<typename IdEnum , typename Rep , typename Period >
result< void > try_post (event_type< IdEnum > event, const void *data, size_t size, const std::chrono::duration< Rep, Period > &timeout)
 Posts a typed event via event_type with a timeout.
 
esp_event_loop_handle_t idf_handle () const
 Returns the underlying ESP-IDF event loop handle.
 
virtual ~event_loop ()
 Virtual destructor.
 
 event_loop (const event_loop &)=delete
 
event_loopoperator= (const event_loop &)=delete
 
 event_loop (event_loop &&)=delete
 
event_loopoperator= (event_loop &&)=delete
 

Static Public Member Functions

static void create_system ()
 Creates the system (default) event loop.
 
static void destroy_system ()
 Destroys the system (default) event loop.
 
static result< void > try_create_system ()
 Creates the system (default) event loop.
 
static result< void > try_destroy_system ()
 Destroys the system (default) event loop.
 
static event_loopsystem ()
 Returns a reference to the system (default) event loop.
 
static std::unique_ptr< user_event_loopmake_user (size_t queue_size=32)
 Creates a user event loop without a dedicated task.
 
static std::unique_ptr< event_loopmake_user (task_config task, size_t queue_size=32)
 Creates a user event loop with a dedicated dispatch task.
 
static result< std::unique_ptr< user_event_loop > > try_make_user (size_t queue_size=32)
 Creates a user event loop without a dedicated task.
 
static result< std::unique_ptr< event_loop > > try_make_user (task_config task, size_t queue_size=32)
 Creates a user event loop with a dedicated dispatch task.
 

Protected Member Functions

 event_loop (esp_event_loop_handle_t handle)
 Constructs an event_loop with the given handle.
 

Detailed Description

Base class for event loops.

Provides listener registration and event posting operations.

void register_listeners(event_loop& loop) {
loop.listener_add(my_events, my_event::started,
[](auto, auto id, void*) { idfxx::log::info("event", "Event received"); });
}
// Use with user-created loop (with task)
auto loop = event_loop::make_user(32, {.name = "events"});
register_listeners(*loop);
// Use with system loop
register_listeners(event_loop::system());
Base class for event loops.
Definition event.hpp:211
static void create_system()
Creates the system (default) event loop.
Definition event.hpp:227
static event_loop & system()
Returns a reference to the system (default) event loop.
listener_handle listener_add(event_base< IdEnum > base, IdEnum id, event_callback< std::type_identity_t< IdEnum > > callback)
Registers a typed listener for a specific event.
Definition event.hpp:846
static std::unique_ptr< user_event_loop > make_user(size_t queue_size=32)
Creates a user event loop without a dedicated task.
Definition event.hpp:834
@ info
Informational messages about normal operation.

Definition at line 211 of file event.hpp.

Constructor & Destructor Documentation

◆ ~event_loop()

virtual idfxx::event_loop::~event_loop ( )
virtual

Virtual destructor.

Deletes the owned event loop handle if non-null.

◆ event_loop() [1/3]

idfxx::event_loop::event_loop ( const event_loop )
delete

◆ event_loop() [2/3]

idfxx::event_loop::event_loop ( event_loop &&  )
delete

◆ event_loop() [3/3]

idfxx::event_loop::event_loop ( esp_event_loop_handle_t  handle)
inlineexplicitprotected

Constructs an event_loop with the given handle.

Parameters
handleThe ESP-IDF event loop handle, or nullptr for the system loop.

Definition at line 621 of file event.hpp.

Member Function Documentation

◆ create_system()

static void idfxx::event_loop::create_system ( )
inlinestatic

Creates the system (default) event loop.

Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

Definition at line 227 of file event.hpp.

References try_create_system(), and idfxx::unwrap().

◆ destroy_system()

static void idfxx::event_loop::destroy_system ( )
inlinestatic

Destroys the system (default) event loop.

Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

Definition at line 235 of file event.hpp.

References try_destroy_system(), and idfxx::unwrap().

◆ idf_handle()

esp_event_loop_handle_t idfxx::event_loop::idf_handle ( ) const
inline

Returns the underlying ESP-IDF event loop handle.

Returns
The esp_event_loop_handle_t, or nullptr for the system loop.

Definition at line 604 of file event.hpp.

Referenced by idfxx::user_event_loop::try_run().

◆ operator=() [1/2]

event_loop & idfxx::event_loop::operator= ( const event_loop )
delete

◆ operator=() [2/2]

event_loop & idfxx::event_loop::operator= ( event_loop &&  )
delete

◆ post() [1/4]

template<typename IdEnum , typename Rep , typename Period >
void idfxx::event_loop::post ( event_base< IdEnum >  base,
IdEnum  id,
const void *  data,
size_t  size,
const std::chrono::duration< Rep, Period > &  timeout 
)
inline

Posts a typed event with a timeout.

Template Parameters
IdEnumThe event ID enum type.
RepThe representation type of the duration.
PeriodThe period type of the duration.
Parameters
baseThe event base.
idThe event ID.
dataEvent data.
sizeSize of the event data.
timeoutMaximum time to wait for space in the event queue.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure or timeout.

Definition at line 472 of file event.hpp.

References idfxx::timeout, try_post(), and idfxx::unwrap().

◆ post() [2/4]

template<typename IdEnum >
void idfxx::event_loop::post ( event_base< IdEnum >  base,
IdEnum  id,
const void *  data = nullptr,
size_t  size = 0 
)
inline

Posts a typed event, waiting indefinitely.

Template Parameters
IdEnumThe event ID enum type.
Parameters
baseThe event base.
idThe event ID.
dataOptional event data.
sizeSize of the event data.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

Definition at line 453 of file event.hpp.

References try_post(), and idfxx::unwrap().

◆ post() [3/4]

template<typename IdEnum , typename Rep , typename Period >
void idfxx::event_loop::post ( event_type< IdEnum >  event,
const void *  data,
size_t  size,
const std::chrono::duration< Rep, Period > &  timeout 
)
inline

Posts a typed event via event_type with a timeout.

Template Parameters
IdEnumThe event ID enum type.
RepThe representation type of the duration.
PeriodThe period type of the duration.
Parameters
eventThe event type (base + id).
dataEvent data.
sizeSize of the event data.
timeoutMaximum time to wait for space in the event queue.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure or timeout.

Definition at line 512 of file event.hpp.

References idfxx::timeout, try_post(), and idfxx::unwrap().

◆ post() [4/4]

template<typename IdEnum >
void idfxx::event_loop::post ( event_type< IdEnum >  event,
const void *  data = nullptr,
size_t  size = 0 
)
inline

Posts a typed event via event_type, waiting indefinitely.

Template Parameters
IdEnumThe event ID enum type.
Parameters
eventThe event type (base + id).
dataOptional event data.
sizeSize of the event data.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

Definition at line 493 of file event.hpp.

References try_post(), and idfxx::unwrap().

◆ system()

static event_loop & idfxx::event_loop::system ( )
static

Returns a reference to the system (default) event loop.

Returns
A reference to the event_loop representing the system event loop.
Note
The system loop must have been created via create_system() first.

◆ try_create_system()

static result< void > idfxx::event_loop::try_create_system ( )
static

Creates the system (default) event loop.

Returns
Success or an error if already created or on failure.

Referenced by create_system().

◆ try_destroy_system()

static result< void > idfxx::event_loop::try_destroy_system ( )
static

Destroys the system (default) event loop.

Returns
Success or an error.

Referenced by destroy_system().

◆ try_listener_remove()

result< void > idfxx::event_loop::try_listener_remove ( listener_handle  handle)

Removes a listener by handle.

Parameters
handleThe listener handle to remove.
Returns
Success or an error.

Referenced by listener_remove().

◆ try_make_user() [1/2]

static result< std::unique_ptr< user_event_loop > > idfxx::event_loop::try_make_user ( size_t  queue_size = 32)
static

Creates a user event loop without a dedicated task.

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

Parameters
queue_sizeMaximum number of events in the queue.
Returns
A user event loop with manual dispatch support, or an error.

Referenced by make_user(), and make_user().

◆ try_make_user() [2/2]

static result< std::unique_ptr< event_loop > > idfxx::event_loop::try_make_user ( task_config  task,
size_t  queue_size = 32 
)
static

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, or an error.

◆ try_post() [1/4]

template<typename IdEnum , typename Rep , typename Period >
result< void > idfxx::event_loop::try_post ( event_base< IdEnum >  base,
IdEnum  id,
const void *  data,
size_t  size,
const std::chrono::duration< Rep, Period > &  timeout 
)
inline

Posts a typed event with a timeout.

Template Parameters
IdEnumThe event ID enum type.
RepThe representation type of the duration.
PeriodThe period type of the duration.
Parameters
baseThe event base.
idThe event ID.
dataEvent data.
sizeSize of the event data.
timeoutMaximum time to wait for space in the event queue.
Returns
Success or an error (e.g., timeout).

Definition at line 550 of file event.hpp.

References idfxx::event_base< IdEnum >::idf_base(), idfxx::chrono::ticks(), idfxx::timeout, and idfxx::wrap().

◆ try_post() [2/4]

template<typename IdEnum >
result< void > idfxx::event_loop::try_post ( event_base< IdEnum >  base,
IdEnum  id,
const void *  data = nullptr,
size_t  size = 0 
)
inline

Posts a typed event, waiting indefinitely.

Template Parameters
IdEnumThe event ID enum type.
Parameters
baseThe event base.
idThe event ID.
dataOptional event data.
sizeSize of the event data.
Returns
Success or an error.

Definition at line 529 of file event.hpp.

References idfxx::event_base< IdEnum >::idf_base(), and idfxx::wrap().

Referenced by post(), post(), post(), post(), try_post(), and try_post().

◆ try_post() [3/4]

template<typename IdEnum , typename Rep , typename Period >
result< void > idfxx::event_loop::try_post ( event_type< IdEnum >  event,
const void *  data,
size_t  size,
const std::chrono::duration< Rep, Period > &  timeout 
)
inline

Posts a typed event via event_type with a timeout.

Template Parameters
IdEnumThe event ID enum type.
RepThe representation type of the duration.
PeriodThe period type of the duration.
Parameters
eventThe event type (base + id).
dataEvent data.
sizeSize of the event data.
timeoutMaximum time to wait for space in the event queue.
Returns
Success or an error (e.g., timeout).

Definition at line 591 of file event.hpp.

References idfxx::event_type< IdEnum >::base, idfxx::event_type< IdEnum >::id, idfxx::timeout, and try_post().

◆ try_post() [4/4]

template<typename IdEnum >
result< void > idfxx::event_loop::try_post ( event_type< IdEnum >  event,
const void *  data = nullptr,
size_t  size = 0 
)
inline

Posts a typed event via event_type, waiting indefinitely.

Template Parameters
IdEnumThe event ID enum type.
Parameters
eventThe event type (base + id).
dataOptional event data.
sizeSize of the event data.
Returns
Success or an error.

Definition at line 574 of file event.hpp.

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


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