Holds a set of objects and can invoke a member function callback on each object in the set with a single call. More...
Classes | |
struct | DummyBailOutChecker |
A dummy bail-out checker that always returns false. More... | |
struct | Iterator |
Iterates the listeners in a ListenerList. More... | |
Public Types | |
using | ThisType = ListenerList< ListenerClass, ArrayType > |
using | ListenerType = ListenerClass |
Public Member Functions | |
ListenerList ()=default | |
Creates an empty list. | |
~ListenerList () | |
Destructor. | |
void | add (ListenerClass *listenerToAdd) |
Adds a listener to the list. | |
void | remove (ListenerClass *listenerToRemove) |
Removes a listener from the list. | |
ErasedScopeGuard | addScoped (ListenerClass &listenerToAdd) |
Adds a listener that will be automatically removed again when the Guard is destroyed. | |
int | size () const noexcept |
Returns the number of registered listeners. | |
bool | isEmpty () const noexcept |
Returns true if no listeners are registered, false otherwise. | |
void | clear () |
Clears the list. | |
bool | contains (ListenerClass *listener) const noexcept |
Returns true if the specified listener has been added to the list. | |
const ArrayType & | getListeners () const noexcept |
Returns the raw array of listeners. | |
template<typename Callback > | |
void | call (Callback &&callback) |
Calls a member function on each listener in the list, with multiple parameters. | |
template<typename Callback > | |
void | callExcluding (ListenerClass *listenerToExclude, Callback &&callback) |
Calls a member function with 1 parameter, on all but the specified listener in the list. | |
template<typename Callback , typename BailOutCheckerType > | |
void | callChecked (const BailOutCheckerType &bailOutChecker, Callback &&callback) |
Calls a member function on each listener in the list, with 1 parameter and a bail-out-checker. | |
template<typename Callback , typename BailOutCheckerType > | |
void | callCheckedExcluding (ListenerClass *listenerToExclude, const BailOutCheckerType &bailOutChecker, Callback &&callback) |
Calls a member function, with 1 parameter, on all but the specified listener in the list with a bail-out-checker. | |
Holds a set of objects and can invoke a member function callback on each object in the set with a single call.
Use a ListenerList to manage a set of objects which need a callback, and you can invoke a member function by simply calling call() or callChecked().
E.g.
It is guaranteed that every Listener is called during an iteration if it's inside the ListenerList before the iteration starts and isn't removed until its end. This guarantee holds even if some Listeners are removed or new ones are added during the iteration.
Listeners added during an iteration are guaranteed to be not called in that iteration.
Sometimes, there's a chance that invoking one of the callbacks might result in the list itself being deleted while it's still iterating - to survive this situation, you can use callChecked() instead of call(), passing it a local object to act as a "BailOutChecker". The BailOutChecker must implement a method of the form "bool shouldBailOut()", and the list will check this after each callback to determine whether it should abort the operation. For an example of a bail-out checker, see the Component::BailOutChecker class, which can be used to check when a Component has been deleted. See also ListenerList::DummyBailOutChecker, which is a dummy checker that always returns false.
using ListenerList< ListenerClass, ArrayType >::ThisType = ListenerList<ListenerClass, ArrayType> |
using ListenerList< ListenerClass, ArrayType >::ListenerType = ListenerClass |
|
default |
Creates an empty list.
ListenerList< ListenerClass, ArrayType >::~ListenerList | ( | ) |
Destructor.
void ListenerList< ListenerClass, ArrayType >::add | ( | ListenerClass * | listenerToAdd | ) |
Adds a listener to the list.
A listener can only be added once, so if the listener is already in the list, this method has no effect.
References jassertfalse.
Referenced by ListenerList< ListenerClass, ArrayType >::addScoped().
void ListenerList< ListenerClass, ArrayType >::remove | ( | ListenerClass * | listenerToRemove | ) |
Removes a listener from the list.
If the listener wasn't in the list, this has no effect.
References jassert.
Referenced by ListenerList< ListenerClass, ArrayType >::addScoped().
ErasedScopeGuard ListenerList< ListenerClass, ArrayType >::addScoped | ( | ListenerClass & | listenerToAdd | ) |
Adds a listener that will be automatically removed again when the Guard is destroyed.
Be very careful to ensure that the ErasedScopeGuard is destroyed or released before the ListenerList is destroyed, otherwise the ErasedScopeGuard may attempt to dereference a dangling pointer when it is destroyed, which will result in a crash.
References ListenerList< ListenerClass, ArrayType >::add(), and ListenerList< ListenerClass, ArrayType >::remove().
|
noexcept |
Returns the number of registered listeners.
Referenced by ListenerList< ListenerClass, ArrayType >::Iterator::next().
|
noexcept |
Returns true if no listeners are registered, false otherwise.
void ListenerList< ListenerClass, ArrayType >::clear | ( | ) |
Clears the list.
|
noexcept |
Returns true if the specified listener has been added to the list.
|
noexcept |
Returns the raw array of listeners.
Referenced by ListenerList< ListenerClass, ArrayType >::Iterator::getListener().
void ListenerList< ListenerClass, ArrayType >::call | ( | Callback && | callback | ) |
Calls a member function on each listener in the list, with multiple parameters.
void ListenerList< ListenerClass, ArrayType >::callExcluding | ( | ListenerClass * | listenerToExclude, |
Callback && | callback | ||
) |
Calls a member function with 1 parameter, on all but the specified listener in the list.
This can be useful if the caller is also a listener and needs to exclude itself.
void ListenerList< ListenerClass, ArrayType >::callChecked | ( | const BailOutCheckerType & | bailOutChecker, |
Callback && | callback | ||
) |
Calls a member function on each listener in the list, with 1 parameter and a bail-out-checker.
See the class description for info about writing a bail-out checker.
void ListenerList< ListenerClass, ArrayType >::callCheckedExcluding | ( | ListenerClass * | listenerToExclude, |
const BailOutCheckerType & | bailOutChecker, | ||
Callback && | callback | ||
) |
Calls a member function, with 1 parameter, on all but the specified listener in the list with a bail-out-checker.
This can be useful if the caller is also a listener and needs to exclude itself. See the class description for info about writing a bail-out checker.