libAlgAudio  v1.99-440-g08538e5-dirty
The development library for AlgAudio framework.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AlgAudio::Signal< Types > Class Template Reference

#include <Signal.hpp>

Inheritance diagram for AlgAudio::Signal< Types >:
AlgAudio::SignalBase

Public Member Functions

unsigned int Count ()
 
bool Happen (Types...t)
 
Signaloperator= (const Signal &other)=delete
 
Signaloperator= (Signal &&other)=delete
 
 Signal ()
 
 Signal (const Signal &other)=delete
 
 Signal (Signal &&other)=delete
 
Subscription Subscribe (std::function< void(Types...)> f) __attribute__((warn_unused_result))
 
template<class C >
Subscription Subscribe (C *class_ptr, void(C::*member_ptr)(Types...)) __attribute__((warn_unused_result))
 
void SubscribeForever (std::function< void(Types...)> f)
 
template<class C >
void SubscribeForever (C *class_ptr, void(C::*member_ptr)(Types...))
 
void SubscribeOnce (std::function< void(Types...)> f)
 
template<class C >
void SubscribeOnce (C *class_ptr, void(C::*member_ptr)(Types...))
 
 ~Signal ()
 

Friends

class Subscription
 

Additional Inherited Members

- Protected Member Functions inherited from AlgAudio::SignalBase
SignalBaseoperator= (const SignalBase &)=delete
 
 SignalBase ()
 
 SignalBase (const SignalBase &)=delete
 
void SubscriptionAddressChanged (Subscription *old, Subscription *n)
 
virtual ~SignalBase ()
 
- Protected Attributes inherited from AlgAudio::SignalBase
std::list< Subscription * > subscriptions
 
- Static Protected Attributes inherited from AlgAudio::SignalBase
static int subscription_id_counter
 

Detailed Description

template<typename... Types>
class AlgAudio::Signal< Types >

A Signal represents an event happening from the application's point of view. Clicking a button may be represented as a Signal. Getting reply from SCLang may be represented as a Signal, etc. Signals are usually publicly exposed to other classes. They can subscribe to a Signal; a request to subscribe essentially means asking "please call this function when this event happens". This way code can be easily bound to be executed on user click. The class managing the signal decides when the event happens, and triggers all subscriber functions by calling .Happen(). A Signal can carry additional data, e.g. "toggle" signal of a checkbox will also carry one boolean, the toggle state. That argument should be passed when .Happen()ing the signal, and all subscribers will receive it.

There are 3 modes of subscription:

  • SubscribeOnce() - The function will be called the first time the signal happens after it was subscribed, it will be automatically unsubscribed afterwards.
  • SubscribeForever() - The function will be called always from now on, there is no way to unsibscribe. This is useful if you wish to react on a signal repetivelly, but don't want to care about releasing subscription.
  • Subscribe() - The standard way of subscribing. It returns a Subscription instance. The time for which the function stays subscribed is no longer then the time of life of that Subscription object. This is useful if your calee function refers to an instance of your class and makes no sense once the instance is destroyed - simply store the Subscription as a field of your class, and it will be unsubcsribed when your class is destroyed.

You can also unsubscribe from a signal by calling .Release() on a Subscription. This resets the Subscription to the default, empty state. A Subscription is not copiable and not copy-constructible, but it is movable.

If you subscribed to a signal using SubscribeForever or SubscribeOnce, and the signal gets destroyed, nothing wrong should happen, your calee function will simply never get executed anymore. However, if a signal you subscribed to using Subscribe is destroyed, then the calee function will never get called AND the Subscription stays in invalid state - but that shouldn't matter, as it's always safe to release a Subscription.

If your class has a lot of Subscription-type members which are used solely to automatically unregister subscriptions when the class is destructed, you may wish to inherit from SubsctiptionManager. This will inherit subscriptions field, where new subscriptions can be added with a += operator:

subscriptions += some_signal.Subscribe([](){ ... });
See also
SubsctiptionManager
LateReturn

Constructor & Destructor Documentation

template<typename... Types>
AlgAudio::Signal< Types >::Signal ( )
inline
template<typename... Types>
AlgAudio::Signal< Types >::~Signal ( )
inline
template<typename... Types>
AlgAudio::Signal< Types >::Signal ( const Signal< Types > &  other)
delete
template<typename... Types>
AlgAudio::Signal< Types >::Signal ( Signal< Types > &&  other)
delete

Member Function Documentation

template<typename... Types>
unsigned int AlgAudio::Signal< Types >::Count ( )
inline

Returns the number of subscribers currently subscribed to this signal.

template<typename... Types>
bool AlgAudio::Signal< Types >::Happen ( Types...  t)
inline

Triggers this event. The signal owner should call this method to trigger all subscribers. The arguments to this function will be passed to all subscribers.

Returns
True if anyone was called.
template<typename... Types>
Signal& AlgAudio::Signal< Types >::operator= ( const Signal< Types > &  other)
delete
template<typename... Types>
Signal& AlgAudio::Signal< Types >::operator= ( Signal< Types > &&  other)
delete
template<typename... Types>
Subscription AlgAudio::Signal< Types >::Subscribe ( std::function< void(Types...)>  f)

Creates a standards, releasable subscription for this Signal.

Parameters
fThe function to be called when this event happens.
template<typename... Types>
template<class C >
Subscription AlgAudio::Signal< Types >::Subscribe ( C *  class_ptr,
void(C::*)(Types...)  member_ptr 
)
template<typename... Types>
void AlgAudio::Signal< Types >::SubscribeForever ( std::function< void(Types...)>  f)
inline

Subscribes a function to this Signal forever.

Parameters
fThe function to be called when this event happens.
template<typename... Types>
template<class C >
void AlgAudio::Signal< Types >::SubscribeForever ( C *  class_ptr,
void(C::*)(Types...)  member_ptr 
)
inline
template<typename... Types>
void AlgAudio::Signal< Types >::SubscribeOnce ( std::function< void(Types...)>  f)
inline

Subscribes a function to this Signal once. The sucscription will be releases after the function is called for the first time.

Parameters
fThe function to be called when this event happens.
template<typename... Types>
template<class C >
void AlgAudio::Signal< Types >::SubscribeOnce ( C *  class_ptr,
void(C::*)(Types...)  member_ptr 
)
inline

Friends And Related Function Documentation

template<typename... Types>
friend class Subscription
friend

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