GNode library (C++)
Loading...
Searching...
No Matches
gnode::Event< Args > Class Template Reference

Thread-safe publisher-subscriber event stream. More...

#include <event.hpp>

Classes

struct  Listener
 

Public Types

using callback_t = std::function< void(Args...)>
 
using listener_id_t = std::size_t
 

Public Member Functions

 Event ()=default
 
 Event (const Event &)=delete
 
Event & operator= (const Event &)=delete
 
 Event (Event &&) noexcept=default
 
Event & operator= (Event &&) noexcept=default
 
EventConnection subscribe (callback_t callback)
 Subscribe to the event.
 
void notify (Args... args)
 Notify all subscribers with the given arguments.
 

Private Member Functions

void unsubscribe (listener_id_t id)
 

Private Attributes

std::vector< Listener > listeners_
 
listener_id_t next_id_ = 0
 
std::unique_ptr< std::recursive_mutex > mutex_
 
std::shared_ptr< void > alive_token_ = std::make_shared<char>()
 

Detailed Description

template<typename... Args>
class gnode::Event< Args >

Thread-safe publisher-subscriber event stream.

Allows multiple callbacks to subscribe and receive notifications of new values/signals. It can be notified from another thread than the one that subscribes: once a connection is disconnected, its callback is not called anymore (the disconnection waits for a notification in progress).

Template Parameters
ArgsArgument types passed to subscribers when notified.

Member Typedef Documentation

◆ callback_t

template<typename... Args>
using gnode::Event< Args >::callback_t = std::function<void(Args...)>

◆ listener_id_t

template<typename... Args>
using gnode::Event< Args >::listener_id_t = std::size_t

Constructor & Destructor Documentation

◆ Event() [1/3]

template<typename... Args>
gnode::Event< Args >::Event ( )
default

◆ Event() [2/3]

template<typename... Args>
gnode::Event< Args >::Event ( const Event< Args > &  )
delete

◆ Event() [3/3]

template<typename... Args>
gnode::Event< Args >::Event ( Event< Args > &&  )
defaultnoexcept

Member Function Documentation

◆ notify()

template<typename... Args>
void gnode::Event< Args >::notify ( Args...  args)
inline

Notify all subscribers with the given arguments.

Copies the listener list internally to allow callbacks to safely unsubscribe themselves during execution.

◆ operator=() [1/2]

template<typename... Args>
Event & gnode::Event< Args >::operator= ( const Event< Args > &  )
delete

◆ operator=() [2/2]

template<typename... Args>
Event & gnode::Event< Args >::operator= ( Event< Args > &&  )
defaultnoexcept

◆ subscribe()

template<typename... Args>
EventConnection gnode::Event< Args >::subscribe ( callback_t  callback)
inline

Subscribe to the event.

Parameters
callbackFunction to be called when notify() is called.
Returns
EventConnection Managing the subscription lifetime.

◆ unsubscribe()

template<typename... Args>
void gnode::Event< Args >::unsubscribe ( listener_id_t  id)
inlineprivate

Member Data Documentation

◆ alive_token_

template<typename... Args>
std::shared_ptr<void> gnode::Event< Args >::alive_token_ = std::make_shared<char>()
private

◆ listeners_

template<typename... Args>
std::vector<Listener> gnode::Event< Args >::listeners_
private

◆ mutex_

template<typename... Args>
std::unique_ptr<std::recursive_mutex> gnode::Event< Args >::mutex_
private
Initial value:
=
std::make_unique<std::recursive_mutex>()

◆ next_id_

template<typename... Args>
listener_id_t gnode::Event< Args >::next_id_ = 0
private

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