JUCE v8.0.9
JUCE API
 
Loading...
Searching...
No Matches
juce::ListenerList< ListenerClass, ArrayType > Class Template Reference

Holds a set of objects and can invoke a member function callback on each object in the set with a single call. More...

#include <juce_ListenerList.h>

Inheritance diagram for juce::ListenerList< ListenerClass, ArrayType >:
Collaboration diagram for juce::ListenerList< ListenerClass, ArrayType >:

Classes

struct  DummyBailOutChecker
 A dummy bail-out checker that always returns false. More...
 
struct  Iterator
 

Public Types

using ListenerType = ListenerClass
 
using ThisType = ListenerList< ListenerClass, ArrayType >
 

Public Member Functions

 ListenerList ()=default
 Creates an empty list.
 
 ~ListenerList ()
 Destructor.
 
void add (ListenerClass *listenerToAdd)
 Adds a listener to the list.
 
ErasedScopeGuard addScoped (ListenerClass &listenerToAdd)
 Adds a listener that will be automatically removed again when the Guard is destroyed.
 
template<typename Callback >
void call (Callback &&callback)
 Calls an invokable object for each listener in the list.
 
template<typename... MethodArgs, typename... Args>
void call (void(ListenerClass::*callbackFunction)(MethodArgs...), Args &&... args)
 Calls a specific listener method for each listener in the list.
 
template<typename Callback , typename BailOutCheckerType >
void callChecked (const BailOutCheckerType &bailOutChecker, Callback &&callback)
 Calls an invokable object for each listener in the list, additionally checking the bail-out checker before each call.
 
template<typename BailOutCheckerType , typename... MethodArgs, typename... Args>
void callChecked (const BailOutCheckerType &bailOutChecker, void(ListenerClass::*callbackFunction)(MethodArgs...), Args &&... args)
 Calls a specific listener method for each listener in the list, additionally checking the bail-out checker before each call.
 
template<typename Callback , typename BailOutCheckerType >
void callCheckedExcluding (ListenerClass *listenerToExclude, const BailOutCheckerType &bailOutChecker, Callback &&callback)
 Calls an invokable object for each listener in the list, except for the listener specified by listenerToExclude, additionally checking the bail-out checker before each call.
 
template<typename BailOutCheckerType , typename... MethodArgs, typename... Args>
void callCheckedExcluding (ListenerClass *listenerToExclude, const BailOutCheckerType &bailOutChecker, void(ListenerClass::*callbackFunction)(MethodArgs...), Args &&... args)
 Calls a specific listener method for each listener in the list, except for the listener specified by listenerToExclude, additionally checking the bail-out checker before each call.
 
template<typename Callback >
void callExcluding (ListenerClass *listenerToExclude, Callback &&callback)
 Calls an invokable object for each listener in the list, except for the listener specified by listenerToExclude.
 
template<typename... MethodArgs, typename... Args>
void callExcluding (ListenerClass *listenerToExclude, void(ListenerClass::*callbackFunction)(MethodArgs...), Args &&... args)
 Calls a specific listener method for each listener in the list, except for the listener specified by listenerToExclude.
 
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.
 
bool isEmpty () const noexcept
 Returns true if no listeners are registered, false otherwise.
 
void remove (ListenerClass *listenerToRemove)
 Removes a listener from the list.
 
int size () const noexcept
 Returns the number of registered listeners.
 

Private Types

using SafeIterators = std::vector< Iterator * >
 
using ScopedLockType = typename ArrayType::ScopedLockType
 
using SharedIterators = std::shared_ptr< SafeIterators >
 
using SharedListeners = std::shared_ptr< ArrayType >
 
enum class  State {
  uninitialised ,
  initialising ,
  initialised
}
 

Private Member Functions

bool initialised () const noexcept
 
void initialiseIfNeeded () noexcept
 

Private Attributes

SharedIterators iterators
 
SharedListeners listeners
 
std::atomic< Statestate { State::uninitialised }
 

Detailed Description

template<typename ListenerClass, typename ArrayType = Array<ListenerClass*>>
class juce::ListenerList< ListenerClass, ArrayType >

Holds a set of objects and can invoke a member function callback on each object in the set with a single call.

It is safe to add listeners, remove listeners, clear the listeners, and even delete the ListenerList itself during any listener callback. If you don't need these extra guarantees consider using a LightweightListenerList instead.

If a Listener is added during a callback, it is guaranteed not to be called in the same iteration.

If a Listener is removed during a callback, it is guaranteed not to be called if it hasn't already been called.

If the ListenerList is cleared or deleted during a callback, it is guaranteed that no more listeners will be called.

It is NOT safe to make concurrent calls to the listeners without a mutex. If you need this functionality, either use a LightweightListenerList or a ThreadSafeListenerList.

When calling listeners the iteration can be escaped early by using a "BailOutChecker". A BailOutChecker is a type that has a public member function with the following signature:

bool shouldBailOut() const

This function will be called before making a call to each listener. For an example see the DummyBailOutChecker.

See also
LightweightListenerList, ThreadSafeListenerList

@tags{Core}

Member Typedef Documentation

◆ ListenerType

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
using juce::ListenerList< ListenerClass, ArrayType >::ListenerType = ListenerClass

◆ SafeIterators

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
using juce::ListenerList< ListenerClass, ArrayType >::SafeIterators = std::vector<Iterator*>
private

◆ ScopedLockType

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
using juce::ListenerList< ListenerClass, ArrayType >::ScopedLockType = typename ArrayType::ScopedLockType
private

◆ SharedIterators

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
using juce::ListenerList< ListenerClass, ArrayType >::SharedIterators = std::shared_ptr<SafeIterators>
private

◆ SharedListeners

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
using juce::ListenerList< ListenerClass, ArrayType >::SharedListeners = std::shared_ptr<ArrayType>
private

◆ ThisType

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
using juce::ListenerList< ListenerClass, ArrayType >::ThisType = ListenerList<ListenerClass, ArrayType>

Member Enumeration Documentation

◆ State

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
enum class juce::ListenerList::State
strongprivate
Enumerator
uninitialised 
initialising 
initialised 

Constructor & Destructor Documentation

◆ ListenerList()

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
juce::ListenerList< ListenerClass, ArrayType >::ListenerList ( )
default

Creates an empty list.

◆ ~ListenerList()

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
juce::ListenerList< ListenerClass, ArrayType >::~ListenerList ( )
inline

Member Function Documentation

◆ add()

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
void juce::ListenerList< ListenerClass, ArrayType >::add ( ListenerClass *  listenerToAdd)
inline

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.

If a Listener is added during a callback, it is guaranteed not to be called in the same iteration.

See also
remove

References juce::ListenerList< ListenerClass, ArrayType >::initialiseIfNeeded(), jassertfalse, and juce::ListenerList< ListenerClass, ArrayType >::listeners.

Referenced by juce::OpenGLContext::NativeContext::addListener(), and juce::ListenerList< ListenerClass, ArrayType >::addScoped().

◆ addScoped()

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
ErasedScopeGuard juce::ListenerList< ListenerClass, ArrayType >::addScoped ( ListenerClass &  listenerToAdd)
inline

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 juce::ListenerList< ListenerClass, ArrayType >::add(), and juce::ListenerList< ListenerClass, ArrayType >::remove().

◆ call() [1/2]

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
template<typename Callback >
void juce::ListenerList< ListenerClass, ArrayType >::call ( Callback &&  callback)
inline

◆ call() [2/2]

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
template<typename... MethodArgs, typename... Args>
void juce::ListenerList< ListenerClass, ArrayType >::call ( void(ListenerClass::*)(MethodArgs...)  callbackFunction,
Args &&...  args 
)
inline

Calls a specific listener method for each listener in the list.

References juce::ListenerList< ListenerClass, ArrayType >::callCheckedExcluding().

◆ callChecked() [1/2]

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
template<typename Callback , typename BailOutCheckerType >
void juce::ListenerList< ListenerClass, ArrayType >::callChecked ( const BailOutCheckerType &  bailOutChecker,
Callback &&  callback 
)
inline

Calls an invokable object for each listener in the list, additionally checking the bail-out checker before each call.

See the class description for info about writing a bail-out checker.

References juce::ListenerList< ListenerClass, ArrayType >::callCheckedExcluding().

◆ callChecked() [2/2]

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
template<typename BailOutCheckerType , typename... MethodArgs, typename... Args>
void juce::ListenerList< ListenerClass, ArrayType >::callChecked ( const BailOutCheckerType &  bailOutChecker,
void(ListenerClass::*)(MethodArgs...)  callbackFunction,
Args &&...  args 
)
inline

Calls a specific listener method for each listener in the list, additionally checking the bail-out checker before each call.

See the class description for info about writing a bail-out checker.

References juce::ListenerList< ListenerClass, ArrayType >::callCheckedExcluding().

◆ callCheckedExcluding() [1/2]

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
template<typename Callback , typename BailOutCheckerType >
void juce::ListenerList< ListenerClass, ArrayType >::callCheckedExcluding ( ListenerClass *  listenerToExclude,
const BailOutCheckerType &  bailOutChecker,
Callback &&  callback 
)
inline

◆ callCheckedExcluding() [2/2]

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
template<typename BailOutCheckerType , typename... MethodArgs, typename... Args>
void juce::ListenerList< ListenerClass, ArrayType >::callCheckedExcluding ( ListenerClass *  listenerToExclude,
const BailOutCheckerType &  bailOutChecker,
void(ListenerClass::*)(MethodArgs...)  callbackFunction,
Args &&...  args 
)
inline

Calls a specific listener method for each listener in the list, except for the listener specified by listenerToExclude, additionally checking the bail-out checker before each call.

See the class description for info about writing a bail-out checker.

References juce::ListenerList< ListenerClass, ArrayType >::callCheckedExcluding().

◆ callExcluding() [1/2]

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
template<typename Callback >
void juce::ListenerList< ListenerClass, ArrayType >::callExcluding ( ListenerClass *  listenerToExclude,
Callback &&  callback 
)
inline

Calls an invokable object for each listener in the list, except for the listener specified by listenerToExclude.

References juce::ListenerList< ListenerClass, ArrayType >::callCheckedExcluding().

◆ callExcluding() [2/2]

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
template<typename... MethodArgs, typename... Args>
void juce::ListenerList< ListenerClass, ArrayType >::callExcluding ( ListenerClass *  listenerToExclude,
void(ListenerClass::*)(MethodArgs...)  callbackFunction,
Args &&...  args 
)
inline

Calls a specific listener method for each listener in the list, except for the listener specified by listenerToExclude.

References juce::ListenerList< ListenerClass, ArrayType >::callCheckedExcluding().

◆ clear()

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
void juce::ListenerList< ListenerClass, ArrayType >::clear ( )
inline

◆ contains()

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
bool juce::ListenerList< ListenerClass, ArrayType >::contains ( ListenerClass *  listener) const
inlinenoexcept

Returns true if the specified listener has been added to the list.

References juce::ListenerList< ListenerClass, ArrayType >::initialised(), and juce::ListenerList< ListenerClass, ArrayType >::listeners.

◆ getListeners()

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
const ArrayType & juce::ListenerList< ListenerClass, ArrayType >::getListeners ( ) const
inlinenoexcept

Returns the raw array of listeners.

Any attempt to mutate the array may result in undefined behaviour.

If the array uses a mutex/CriticalSection, reading from the array without first obtaining the lock may potentially result in undefined behaviour.

See also
add, remove, clear, contains

References juce::ListenerList< ListenerClass, ArrayType >::initialiseIfNeeded(), and juce::ListenerList< ListenerClass, ArrayType >::listeners.

◆ initialised()

◆ initialiseIfNeeded()

◆ isEmpty()

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
bool juce::ListenerList< ListenerClass, ArrayType >::isEmpty ( ) const
inlinenoexcept

Returns true if no listeners are registered, false otherwise.

References juce::ListenerList< ListenerClass, ArrayType >::initialised(), and juce::ListenerList< ListenerClass, ArrayType >::listeners.

◆ remove()

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
void juce::ListenerList< ListenerClass, ArrayType >::remove ( ListenerClass *  listenerToRemove)
inline

Removes a listener from the list.

If the listener wasn't in the list, this has no effect.

If a Listener is removed during a callback, it is guaranteed not to be called if it hasn't already been called.

References juce::end(), juce::ListenerList< ListenerClass, ArrayType >::initialised(), juce::ListenerList< ListenerClass, ArrayType >::iterators, jassert, and juce::ListenerList< ListenerClass, ArrayType >::listeners.

Referenced by juce::ListenerList< ListenerClass, ArrayType >::addScoped(), and juce::OpenGLContext::NativeContext::removeListener().

◆ size()

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
int juce::ListenerList< ListenerClass, ArrayType >::size ( ) const
inlinenoexcept

Member Data Documentation

◆ iterators

◆ listeners

◆ state

template<typename ListenerClass , typename ArrayType = Array<ListenerClass*>>
std::atomic<State> juce::ListenerList< ListenerClass, ArrayType >::state { State::uninitialised }
private

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