70 template <
class ListenerClass,
71 class ArrayType = Array<ListenerClass*>>
88 void add (ListenerClass* listenerToAdd)
90 if (listenerToAdd !=
nullptr)
91 listeners.addIfNotAlreadyThere (listenerToAdd);
99 void remove (ListenerClass* listenerToRemove)
101 jassert (listenerToRemove !=
nullptr);
102 listeners.removeFirstMatchingValue (listenerToRemove);
106 int size() const noexcept {
return listeners.size(); }
109 bool isEmpty() const noexcept {
return listeners.isEmpty(); }
115 bool contains (ListenerClass* listener)
const noexcept {
return listeners.contains (listener); }
122 template <
typename Callback>
123 void call (Callback&& callback)
125 typename ArrayType::ScopedLockType lock (listeners.getLock());
127 for (Iterator<DummyBailOutChecker, ThisType> iter (*
this); iter.next();)
128 callback (*iter.getListener());
134 template <
typename Callback>
137 typename ArrayType::ScopedLockType lock (listeners.getLock());
139 for (Iterator<DummyBailOutChecker, ThisType> iter (*
this); iter.next();)
141 auto* l = iter.getListener();
143 if (l != listenerToExclude)
151 template <
typename Callback,
typename BailOutCheckerType>
152 void callChecked (
const BailOutCheckerType& bailOutChecker, Callback&& callback)
154 typename ArrayType::ScopedLockType lock (listeners.getLock());
156 for (Iterator<BailOutCheckerType, ThisType> iter (*
this); iter.next (bailOutChecker);)
157 callback (*iter.getListener());
164 template <
typename Callback,
typename BailOutCheckerType>
166 const BailOutCheckerType& bailOutChecker,
169 typename ArrayType::ScopedLockType lock (listeners.getLock());
171 for (Iterator<BailOutCheckerType, ThisType> iter (*
this); iter.next (bailOutChecker);)
173 auto* l = iter.getListener();
175 if (l != listenerToExclude)
186 bool shouldBailOut()
const noexcept {
return false; }
190 using ListenerType = ListenerClass;
194 template <
class BailOutCheckerType,
class ListType>
197 Iterator (
const ListType& listToIterate) noexcept
198 : list (listToIterate), index (listToIterate.size())
209 auto listSize = list.size();
211 if (--index < listSize)
214 index = listSize - 1;
218 bool next (
const BailOutCheckerType& bailOutChecker) noexcept
220 return (! bailOutChecker.shouldBailOut()) && next();
223 typename ListType::ListenerType* getListener()
const noexcept
225 return list.getListeners().getUnchecked (index);
230 const ListType& list;
233 JUCE_DECLARE_NON_COPYABLE (
Iterator)
240 void call (
void (ListenerClass::*callbackFunction) ())
242 call ([=] (ListenerClass& l) { (l.*callbackFunction)(); });
245 void callExcluding (ListenerClass* listenerToExclude,
void (ListenerClass::*callbackFunction) ())
247 callExcluding (listenerToExclude, [=] (ListenerClass& l) { (l.*callbackFunction)(); });
250 template <
class BailOutCheckerType>
251 void callChecked (
const BailOutCheckerType& bailOutChecker,
void (ListenerClass::*callbackFunction) ())
253 callChecked (bailOutChecker, [=] (ListenerClass& l) { (l.*callbackFunction)(); });
256 template <
class BailOutCheckerType>
258 const BailOutCheckerType& bailOutChecker,
259 void (ListenerClass::*callbackFunction) ())
261 callCheckedExcluding (listenerToExclude, bailOutChecker, [=] (ListenerClass& l) { (l.*callbackFunction)(); });
264 template <
typename... MethodArgs,
typename... Args>
265 void call (
void (ListenerClass::*callbackFunction) (MethodArgs...), Args&&... args)
267 typename ArrayType::ScopedLockType lock (listeners.getLock());
269 for (Iterator<DummyBailOutChecker, ThisType> iter (*
this); iter.next();)
270 (iter.getListener()->*callbackFunction) (
static_cast<typename TypeHelpers::ParameterType<Args>::type
> (args)...);
273 template <
typename... MethodArgs,
typename... Args>
275 void (ListenerClass::*callbackFunction) (MethodArgs...),
278 typename ArrayType::ScopedLockType lock (listeners.getLock());
280 for (Iterator<DummyBailOutChecker, ThisType> iter (*
this); iter.next();)
281 if (iter.getListener() != listenerToExclude)
282 (iter.getListener()->*callbackFunction) (
static_cast<typename TypeHelpers::ParameterType<Args>::type
> (args)...);
285 template <
typename BailOutCheckerType,
typename... MethodArgs,
typename... Args>
286 void callChecked (
const BailOutCheckerType& bailOutChecker,
287 void (ListenerClass::*callbackFunction) (MethodArgs...),
290 typename ArrayType::ScopedLockType lock (listeners.getLock());
292 for (Iterator<BailOutCheckerType, ThisType> iter (*
this); iter.next (bailOutChecker);)
293 (iter.getListener()->*callbackFunction) (
static_cast<typename TypeHelpers::ParameterType<Args>::type
> (args)...);
296 template <
typename BailOutCheckerType,
typename... MethodArgs,
typename... Args>
298 const BailOutCheckerType& bailOutChecker,
299 void (ListenerClass::*callbackFunction) (MethodArgs...),
302 typename ArrayType::ScopedLockType lock (listeners.getLock());
304 for (Iterator<BailOutCheckerType, ThisType> iter (*
this); iter.next (bailOutChecker);)
305 if (iter.getListener() != listenerToExclude)
306 (iter.getListener()->*callbackFunction) (
static_cast<typename TypeHelpers::ParameterType<Args>::type
> (args)...);