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:
- See also
- SubsctiptionManager
-
LateReturn