Stormancer::Event< TParams > Class Template Reference

Represents an event to which multiple handlers can subscribe. More...

#include <Event.h>

Public Types

using TEvent = Event< TParams... >
 
using TFunction = std::function< void(TParams...)>
 
using TContainer = std::list< TFunction >
 
using TIterator = typename TContainer::iterator
 
using Subscription = std::shared_ptr< Subscription_impl >
 
using TMutex = std::recursive_mutex
 

Public Member Functions

 Event (const Event &)=delete
 There is most likely no reason to copy Events. Getters should return an Event&. This constructor is deleted to help detect misuses of Event. More...
 
TEventoperator() (TParams... data)
 
bool hasSubscribers () const
 Returns true if any subscription is alive.
 
STORM_NODISCARD Subscription subscribe (TFunction callback)
 

Detailed Description

template<typename... TParams>
class Stormancer::Event< TParams >

Represents an event to which multiple handlers can subscribe.

When subscribing to events with the subsribe() function, a subscription object is returned. The lifetime of the subscription is directly tied to the lifetime of this object. When all copies of the object are destroyed, the provided callback gets unsubscribed from the event automatically. Events can carry any number of parameters. Callables passed to subscribe() must take the exact same parameters. To create an event with no parameters, use Event<>.

Constructor & Destructor Documentation

◆ Event()

template<typename... TParams>
Stormancer::Event< TParams >::Event ( const Event< TParams > &  )
delete

There is most likely no reason to copy Events. Getters should return an Event&. This constructor is deleted to help detect misuses of Event.

Member Function Documentation

◆ operator()()

template<typename... TParams>
TEvent& Stormancer::Event< TParams >::operator() ( TParams...  data)
inline

Trigger the Event and call the subscribed callbacks with the given parameters.

Parameters
dataParameters to give to the subscribed callbacks.
Returns
The Event.

◆ subscribe()

template<typename... TParams>
STORM_NODISCARD Subscription Stormancer::Event< TParams >::subscribe ( TFunction  callback)
inline

Calls the callback when the Event is triggered and returns a Subscription that will unsubscribe from the Event when it is deleted.

Warning
Don't forget to keep the Subscription returned for keeping registered to the Event.
Parameters
callbackCallback that will be called when the Event is triggered.
Returns
A Subscription that will unsubscribe from the Event when it is deleted.

The documentation for this class was generated from the following file:
  • stormancer/stormancer-sources/include/public/stormancer/Event.h