EduArt Sensor Ring Library 3.0.0
Loading...
Searching...
No Matches
eduart::subscription::Publisher< Args > Class Template Reference

Generic thread-safe publisher that manages callback subscriptions. More...

#include <Publisher.hpp>

Public Types

using Callback = std::function<void(Args...)>
 Callback type for subscribers.

Public Member Functions

Subscription subscribe (Callback callback)
 Register a callback.
void unsubscribe (SubscriberToken token)
 Remove a previously registered callback.
void publish (Args... args) const
 Invoke all registered callbacks with the given arguments.
std::vector< CallbackcopySubscribers () const
 Copy the current subscriber list under lock.

Detailed Description

template<typename... Args>
class eduart::subscription::Publisher< Args >

Generic thread-safe publisher that manages callback subscriptions.

Stores subscriber callbacks keyed by SubscriberToken. The publish() method copies the callback list under lock and invokes the copies outside the lock (copy-then-invoke), preventing deadlocks when a callback subscribes or unsubscribes.

Template Parameters
ArgsParameter types forwarded to subscriber callbacks.

Member Function Documentation

◆ copySubscribers()

template<typename... Args>
std::vector< Callback > eduart::subscription::Publisher< Args >::copySubscribers ( ) const
inline

Copy the current subscriber list under lock.

Useful when the caller needs custom exception handling or filtering around the invocation loop.

Returns
Vector of callback copies.

◆ publish()

template<typename... Args>
void eduart::subscription::Publisher< Args >::publish ( Args... args) const
inline

Invoke all registered callbacks with the given arguments.

Callbacks are copied under lock and invoked outside the lock. Exceptions thrown by callbacks are silently absorbed.

Parameters
[in]argsArguments forwarded to each callback.

◆ subscribe()

template<typename... Args>
Subscription eduart::subscription::Publisher< Args >::subscribe ( Callback callback)
inline

Register a callback.

Parameters
[in]callbackInvoked on each publish().
Returns
RAII Subscription that auto-cancels on destruction.

◆ unsubscribe()

template<typename... Args>
void eduart::subscription::Publisher< Args >::unsubscribe ( SubscriberToken token)
inline

Remove a previously registered callback.

Parameters
[in]tokenToken identifying the subscription to cancel.

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