RAVENNAKIT
Loading...
Searching...
No Matches
rav::SubscriberList< T, C > Class Template Reference

Super basic list of subscribers. More...

#include <subscriber_list.hpp>

Public Member Functions

 SubscriberList ()=default
 ~SubscriberList ()
 SubscriberList (const SubscriberList &)=delete
SubscriberList & operator= (const SubscriberList &)=delete
 SubscriberList (SubscriberList &&)=default
SubscriberList & operator= (SubscriberList &&)=default
auto begin () const
auto end () const
auto begin ()
auto end ()
bool add (T *subscriber, C context)
 Adds the given subscriber to the list.
bool add_or_update_context (T *subscriber, C context)
 Adds the given subscriber to the list.
bool update_context (T *subscriber, C context)
 Updates the context of the given subscriber.
std::optional< C > remove (const T *subscriber)
 Removes the given subscriber from the list.
void clear ()
 Clears the list.
void foreach (const std::function< void(T *, const C &)> &f, const T *excluding=nullptr)
 Calls given function for each subscriber.
void foreach (const std::function< void(T *)> &f, const T *excluding=nullptr)
 Calls given function for each subscriber.
size_t size () const
bool empty () const
bool contains (T *subscriber) const
 Checks if the list contains the given subscriber.

Detailed Description

template<class T, class C = void>
class rav::SubscriberList< T, C >

Super basic list of subscribers.

Can optionally store a context with each subscriber. This class is not thread save and make sure that the subscriber is not destroyed while it is in the list.

Template Parameters
TThe type of the subscriber.
CThe type of the context to store with each subscriber. If void, no context will be stored.

Definition at line 41 of file subscriber_list.hpp.

Constructor & Destructor Documentation

◆ SubscriberList() [1/3]

template<class T, class C = void>
rav::SubscriberList< T, C >::SubscriberList ( )
default

◆ ~SubscriberList()

template<class T, class C = void>
rav::SubscriberList< T, C >::~SubscriberList ( )
inline

Definition at line 45 of file subscriber_list.hpp.

◆ SubscriberList() [2/3]

template<class T, class C = void>
rav::SubscriberList< T, C >::SubscriberList ( const SubscriberList< T, C > & )
delete

◆ SubscriberList() [3/3]

template<class T, class C = void>
rav::SubscriberList< T, C >::SubscriberList ( SubscriberList< T, C > && )
default

Member Function Documentation

◆ add()

template<class T, class C = void>
bool rav::SubscriberList< T, C >::add ( T * subscriber,
C context )
inlinenodiscard

Adds the given subscriber to the list.

If subscriber is already in the list it will not be added and the context will not be updated. Use add_or_update to update the context.

Parameters
subscriberThe subscriber to add.
contextThe context to add.
Returns
true if the subscriber was added, or false if it was already in the list.

Definition at line 93 of file subscriber_list.hpp.

◆ add_or_update_context()

template<class T, class C = void>
bool rav::SubscriberList< T, C >::add_or_update_context ( T * subscriber,
C context )
inlinenodiscard

Adds the given subscriber to the list.

If subscriber is already in the list it will be updated with the new context.

Parameters
subscriberThe subscriber to add.
contextThe context to add.
Returns
true if the subscriber was added, or false if it was already in the list. In both cases the context will be updated.

Definition at line 109 of file subscriber_list.hpp.

◆ begin() [1/2]

template<class T, class C = void>
auto rav::SubscriberList< T, C >::begin ( )
inline
Returns
An iterator to the beginning of the list.

Definition at line 75 of file subscriber_list.hpp.

◆ begin() [2/2]

template<class T, class C = void>
auto rav::SubscriberList< T, C >::begin ( ) const
inline
Returns
An iterator to the beginning of the list.

Definition at line 61 of file subscriber_list.hpp.

◆ clear()

template<class T, class C = void>
void rav::SubscriberList< T, C >::clear ( )
inline

Clears the list.

Definition at line 156 of file subscriber_list.hpp.

◆ contains()

template<class T, class C = void>
bool rav::SubscriberList< T, C >::contains ( T * subscriber) const
inlinenodiscard

Checks if the list contains the given subscriber.

Parameters
subscriberThe subscriber to check.
Returns
true if the list contains the subscriber, or false if not.

Definition at line 205 of file subscriber_list.hpp.

◆ empty()

template<class T, class C = void>
bool rav::SubscriberList< T, C >::empty ( ) const
inlinenodiscard
Returns
true if there are no subscribers.

Definition at line 196 of file subscriber_list.hpp.

◆ end() [1/2]

template<class T, class C = void>
auto rav::SubscriberList< T, C >::end ( )
inline
Returns
An iterator to the end of the list.

Definition at line 82 of file subscriber_list.hpp.

◆ end() [2/2]

template<class T, class C = void>
auto rav::SubscriberList< T, C >::end ( ) const
inline
Returns
An iterator to the end of the list.

Definition at line 68 of file subscriber_list.hpp.

◆ foreach() [1/2]

template<class T, class C = void>
void rav::SubscriberList< T, C >::foreach ( const std::function< void(T *)> & f,
const T * excluding = nullptr )
inline

Calls given function for each subscriber.

Parameters
fThe function to call for each subscriber. Must be not null.
excludingThe subscriber to exclude from the call.

Definition at line 178 of file subscriber_list.hpp.

◆ foreach() [2/2]

template<class T, class C = void>
void rav::SubscriberList< T, C >::foreach ( const std::function< void(T *, const C &)> & f,
const T * excluding = nullptr )
inline

Calls given function for each subscriber.

Parameters
fThe function to call for each subscriber. Must be not null.
excludingThe subscriber to exclude from the call.

Definition at line 165 of file subscriber_list.hpp.

◆ operator=() [1/2]

template<class T, class C = void>
SubscriberList & rav::SubscriberList< T, C >::operator= ( const SubscriberList< T, C > & )
delete

◆ operator=() [2/2]

template<class T, class C = void>
SubscriberList & rav::SubscriberList< T, C >::operator= ( SubscriberList< T, C > && )
default

◆ remove()

template<class T, class C = void>
std::optional< C > rav::SubscriberList< T, C >::remove ( const T * subscriber)
inlinenodiscard

Removes the given subscriber from the list.

Parameters
subscriberThe subscriber to remove.
Returns
An option containing the context of the removed subscriber, or nullopt if the subscriber was not in the list.

Definition at line 142 of file subscriber_list.hpp.

◆ size()

template<class T, class C = void>
size_t rav::SubscriberList< T, C >::size ( ) const
inlinenodiscard
Returns
The number of subscribers.

Definition at line 189 of file subscriber_list.hpp.

◆ update_context()

template<class T, class C = void>
bool rav::SubscriberList< T, C >::update_context ( T * subscriber,
C context )
inlinenodiscard

Updates the context of the given subscriber.

Parameters
subscriberThe subscriber to update.
contextThe new context.
Returns
true if the subscriber was updated, or false if it was not in the list.

Definition at line 126 of file subscriber_list.hpp.


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