Medical Imaging Interaction Toolkit  2024.12.99-0da743f6
Medical Imaging Interaction Toolkit
berry::ListenerList< T, C > Class Template Reference

#include <berryListenerList.h>

Public Member Functions

void Add (const T &listener)
 
QList< T > GetListeners () const
 
bool IsEmpty () const
 
void Remove (const T &listener)
 
int Size () const
 
void Clear ()
 

Detailed Description

template<class T, typename C = ListenerListEquals>
class berry::ListenerList< T, C >

This class is a thread safe list that is designed for storing lists of listeners. The implementation is optimized for minimal memory footprint, frequent reads and infrequent writes. Modification of the list is synchronized and relatively expensive, while accessing the listeners is very fast. Readers are given access to the underlying array data structure for reading, with the trust that they will not modify the underlying array.

A listener list handles the same listener being added multiple times, and tolerates removal of listeners that are the same as other listeners in the list. For this purpose, listeners can be compared with each other using a custom comparison functor.

Use the GetListeners method when notifying listeners. The recommended code sequence for notifying all registered listeners of say, FooListener::EventHappened(), is:

QList<FooListener*> listeners = myListenerList.GetListeners();
for (int i = 0; i &lt; listeners.size(); ++i)
{
listeners[i]->EventHappened(event);
}

Definition at line 59 of file berryListenerList.h.

Member Function Documentation

◆ Add()

template<class T , typename C = ListenerListEquals>
void berry::ListenerList< T, C >::Add ( const T &  listener)
inline

Adds a listener to this list. This method has no effect if the same listener is already registered.

Parameters
listenerthe non-null listener to add

Definition at line 70 of file berryListenerList.h.

◆ Clear()

template<class T , typename C = ListenerListEquals>
void berry::ListenerList< T, C >::Clear ( )
inline

Removes all listeners from this list.

Definition at line 155 of file berryListenerList.h.

◆ GetListeners()

template<class T , typename C = ListenerListEquals>
QList<T> berry::ListenerList< T, C >::GetListeners ( ) const
inline

Returns an array containing all the registered listeners. The resulting array is unaffected by subsequent adds or removes. If there are no listeners registered, the result is an empty array. Use this method when notifying listeners, so that any modifications to the listener list during the notification will have no effect on the notification itself.

Warning
Callers of this method must not modify the returned array.
Returns
the list of registered listeners

Definition at line 100 of file berryListenerList.h.

◆ IsEmpty()

template<class T , typename C = ListenerListEquals>
bool berry::ListenerList< T, C >::IsEmpty ( ) const
inline

Returns whether this listener list is empty.

Returns
true if there are no registered listeners, and false otherwise

Definition at line 112 of file berryListenerList.h.

◆ Remove()

template<class T , typename C = ListenerListEquals>
void berry::ListenerList< T, C >::Remove ( const T &  listener)
inline

Removes a listener from this list. Has no effect if the same listener was not already registered.

Parameters
listenerthe listener to remove

Definition at line 124 of file berryListenerList.h.

◆ Size()

template<class T , typename C = ListenerListEquals>
int berry::ListenerList< T, C >::Size ( ) const
inline

Returns the number of registered listeners.

Returns
the number of registered listeners

Definition at line 146 of file berryListenerList.h.


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