iceoryx_posh 2.0.5
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
iox::popo::ListenerImpl< Capacity > Class Template Reference

The Listener is a class which reacts to registered events by executing a corresponding callback concurrently. This is achieved via an encapsulated thread inside this class. More...

#include <iceoryx_posh/popo/listener.hpp>

Public Member Functions

 ListenerImpl (const ListenerImpl &)=delete
 
 ListenerImpl (ListenerImpl &&)=delete
 
ListenerImploperator= (const ListenerImpl &)=delete
 
ListenerImploperator= (ListenerImpl &&)=delete
 
template<typename T , typename EventType , typename ContextDataType , typename = std::enable_if_t<std::is_enum<EventType>::value>>
cxx::expected< ListenerError > attachEvent (T &eventOrigin, const EventType eventType, const NotificationCallback< T, ContextDataType > &eventCallback) noexcept
 Attaches an event. Hereby the event is defined as a class T, the eventOrigin, an enum which further defines the event inside the class and the corresponding callback which will be called when the event occurs.
 
template<typename T , typename ContextDataType >
cxx::expected< ListenerError > attachEvent (T &eventOrigin, const NotificationCallback< T, ContextDataType > &eventCallback) noexcept
 Attaches an event. Hereby the event is defined as a class T, the eventOrigin and the corresponding callback which will be called when the event occurs.
 
template<typename T , typename EventType , typename = std::enable_if_t<std::is_enum<EventType>::value>>
void detachEvent (T &eventOrigin, const EventType eventType) noexcept
 Detaches an event. Hereby, the event is defined as a class T, the eventOrigin and the eventType with further specifies the event inside of eventOrigin.
 
template<typename T >
void detachEvent (T &eventOrigin) noexcept
 Detaches an event. Hereby, the event is defined as a class T, the eventOrigin.
 
uint64_t size () const noexcept
 Returns the size of the Listener.
 

Static Public Member Functions

static constexpr uint64_t capacity () noexcept
 Returns the capacity of the Listener.
 

Protected Member Functions

 ListenerImpl (ConditionVariableData &conditionVariableData) noexcept
 

Detailed Description

template<uint64_t Capacity>
class iox::popo::ListenerImpl< Capacity >

The Listener is a class which reacts to registered events by executing a corresponding callback concurrently. This is achieved via an encapsulated thread inside this class.

Note
The Listener is threadsafe and can be used without any restrictions concurrently.
Attention
Calling detachEvent for the same event from multiple threads is supported but can cause a race condition if you attach the same event again concurrently from another thread. Example:
  1. One calls detachEvent [1] from thread A, B and C
  2. thread B wins and detaches event [1]
  3. A new thread D spawns and would like to attach event [1] again while thread A and C are still waiting to detach [1].
  4. Thread A wins but cannot detach event [1] since it is not attached.
  5. Thread D wins and attaches event [1].
  6. Finally thread C can continue and detaches event [1] again.

If thread D is executed last then the event is attached. So depending on the operating system defined execution order the event is either attached or detached.

Best practice: Detach a specific event only from one specific thread and not from multiple contexts.

Member Function Documentation

◆ attachEvent() [1/2]

template<uint64_t Capacity>
template<typename T , typename EventType , typename ContextDataType , typename = std::enable_if_t<std::is_enum<EventType>::value>>
cxx::expected< ListenerError > iox::popo::ListenerImpl< Capacity >::attachEvent ( T eventOrigin,
const EventType  eventType,
const NotificationCallback< T, ContextDataType > &  eventCallback 
)
noexcept

Attaches an event. Hereby the event is defined as a class T, the eventOrigin, an enum which further defines the event inside the class and the corresponding callback which will be called when the event occurs.

Note
This method can be called from any thread concurrently without any restrictions! Furthermore, attachEvent does not take ownership of callback in the underlying eventCallback or the optional contextData. The user has to ensure that both will live as long as the event is attached.
Template Parameters

in] T type of the class which will signal the event

Parameters
[in]eventOriginthe object which will signal the event (the origin)
[in]eventTypeenum required to specify the type of event inside of eventOrigin
[in]eventCallbackcallback which will be executed concurrently when the event occurs. has to be created with iox::popo::createNotificationCallback
Returns
If an error occurs the enum packed inside an expected which describes the error.

◆ attachEvent() [2/2]

template<uint64_t Capacity>
cxx::expected< ListenerError > iox::popo::ListenerImpl< Capacity >::attachEvent ( T eventOrigin,
const NotificationCallback< T, ContextDataType > &  eventCallback 
)
noexcept

Attaches an event. Hereby the event is defined as a class T, the eventOrigin and the corresponding callback which will be called when the event occurs.

Note
This method can be called from any thread concurrently without any restrictions! Furthermore, attachEvent does not take ownership of callback in the underlying eventCallback or the optional contextData. The user has to ensure that both will live as long as the event is attached.
Template Parameters

in] T type of the class which will signal the event

Parameters
[in]eventOriginthe object which will signal the event (the origin)
[in]eventCallbackcallback which will be executed concurrently when the event occurs. Has to be created with iox::popo::createNotificationCallback
Returns
If an error occurs the enum packed inside an expected which describes the error.

◆ capacity()

template<uint64_t Capacity>
static constexpr uint64_t iox::popo::ListenerImpl< Capacity >::capacity ( )
staticconstexprnoexcept

Returns the capacity of the Listener.

Returns
capacity of the Listener

◆ detachEvent() [1/2]

template<uint64_t Capacity>
template<typename T >
void iox::popo::ListenerImpl< Capacity >::detachEvent ( T eventOrigin)
noexcept

Detaches an event. Hereby, the event is defined as a class T, the eventOrigin.

Note
This method can be called from any thread concurrently without any restrictions!
Template Parameters

in] T type of the class which will signal the event

◆ detachEvent() [2/2]

template<uint64_t Capacity>
template<typename T , typename EventType , typename = std::enable_if_t<std::is_enum<EventType>::value>>
void iox::popo::ListenerImpl< Capacity >::detachEvent ( T eventOrigin,
const EventType  eventType 
)
noexcept

Detaches an event. Hereby, the event is defined as a class T, the eventOrigin and the eventType with further specifies the event inside of eventOrigin.

Note
This method can be called from any thread concurrently without any restrictions!
Template Parameters

in] T type of the class which will signal the event

Parameters
[in]eventOriginthe object which will signal the event (the origin)
[in]eventTypeenum required to specify the type of event inside of eventOrigin

◆ size()

template<uint64_t Capacity>
uint64_t iox::popo::ListenerImpl< Capacity >::size ( ) const
noexcept

Returns the size of the Listener.

Returns
size of the Listener

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