66 template <
class ListenerClass,
67 class ArrayType = Array<ListenerClass*>>
84 void add (ListenerClass* listenerToAdd)
86 if (listenerToAdd !=
nullptr)
87 listeners.addIfNotAlreadyThere (listenerToAdd);
95 void remove (ListenerClass* listenerToRemove)
97 jassert (listenerToRemove !=
nullptr);
98 listeners.removeFirstMatchingValue (listenerToRemove);
102 int size() const noexcept {
return listeners.size(); }
105 bool isEmpty() const noexcept {
return listeners.isEmpty(); }
111 bool contains (ListenerClass* listener)
const noexcept {
return listeners.contains (listener); }
118 template <
typename Callback>
119 void call (Callback&& callback)
121 typename ArrayType::ScopedLockType lock (listeners.getLock());
123 for (Iterator<DummyBailOutChecker, ThisType> iter (*
this); iter.next();)
124 callback (*iter.getListener());
130 template <
typename Callback>
133 typename ArrayType::ScopedLockType lock (listeners.getLock());
135 for (Iterator<DummyBailOutChecker, ThisType> iter (*
this); iter.next();)
137 auto* l = iter.getListener();
139 if (l != listenerToExclude)
147 template <
typename Callback,
typename BailOutCheckerType>
148 void callChecked (
const BailOutCheckerType& bailOutChecker, Callback&& callback)
150 typename ArrayType::ScopedLockType lock (listeners.getLock());
152 for (Iterator<BailOutCheckerType, ThisType> iter (*
this); iter.next (bailOutChecker);)
153 callback (*iter.getListener());
160 template <
typename Callback,
typename BailOutCheckerType>
162 const BailOutCheckerType& bailOutChecker,
165 typename ArrayType::ScopedLockType lock (listeners.getLock());
167 for (Iterator<BailOutCheckerType, ThisType> iter (*
this); iter.next (bailOutChecker);)
169 auto* l = iter.getListener();
171 if (l != listenerToExclude)
182 bool shouldBailOut()
const noexcept {
return false; }
186 using ListenerType = ListenerClass;
190 template <
class BailOutCheckerType,
class ListType>
193 Iterator (
const ListType& listToIterate) noexcept
194 : list (listToIterate), index (listToIterate.size())
205 auto listSize = list.size();
207 if (--index < listSize)
210 index = listSize - 1;
214 bool next (
const BailOutCheckerType& bailOutChecker) noexcept
216 return (! bailOutChecker.shouldBailOut()) && next();
219 typename ListType::ListenerType* getListener()
const noexcept
221 return list.getListeners().getUnchecked (index);
226 const ListType& list;
229 JUCE_DECLARE_NON_COPYABLE (
Iterator)
236 void call (
void (ListenerClass::*callbackFunction) ())
238 call ([=] (ListenerClass& l) { (l.*callbackFunction)(); });
241 void callExcluding (ListenerClass* listenerToExclude,
void (ListenerClass::*callbackFunction) ())
243 callExcluding (listenerToExclude, [=] (ListenerClass& l) { (l.*callbackFunction)(); });
246 template <
class BailOutCheckerType>
247 void callChecked (
const BailOutCheckerType& bailOutChecker,
void (ListenerClass::*callbackFunction) ())
249 callChecked (bailOutChecker, [=] (ListenerClass& l) { (l.*callbackFunction)(); });
252 template <
class BailOutCheckerType>
254 const BailOutCheckerType& bailOutChecker,
255 void (ListenerClass::*callbackFunction) ())
257 callCheckedExcluding (listenerToExclude, bailOutChecker, [=] (ListenerClass& l) { (l.*callbackFunction)(); });
260 template <
typename... MethodArgs,
typename... Args>
261 void call (
void (ListenerClass::*callbackFunction) (MethodArgs...), Args&&... args)
263 typename ArrayType::ScopedLockType lock (listeners.getLock());
266 (iter.getListener()->*callbackFunction) (
static_cast<typename TypeHelpers::ParameterType<Args>::type
> (args)...);
269 template <
typename... MethodArgs,
typename... Args>
271 void (ListenerClass::*callbackFunction) (MethodArgs...),
274 typename ArrayType::ScopedLockType lock (listeners.getLock());
277 if (iter.getListener() != listenerToExclude)
278 (iter.getListener()->*callbackFunction) (
static_cast<typename TypeHelpers::ParameterType<Args>::type
> (args)...);
281 template <
typename BailOutCheckerType,
typename... MethodArgs,
typename... Args>
282 void callChecked (
const BailOutCheckerType& bailOutChecker,
283 void (ListenerClass::*callbackFunction) (MethodArgs...),
286 typename ArrayType::ScopedLockType lock (listeners.getLock());
289 (iter.getListener()->*callbackFunction) (
static_cast<typename TypeHelpers::ParameterType<Args>::type
> (args)...);
292 template <
typename BailOutCheckerType,
typename... MethodArgs,
typename... Args>
294 const BailOutCheckerType& bailOutChecker,
295 void (ListenerClass::*callbackFunction) (MethodArgs...),
298 typename ArrayType::ScopedLockType lock (listeners.getLock());
301 if (iter.getListener() != listenerToExclude)
302 (iter.getListener()->*callbackFunction) (
static_cast<typename TypeHelpers::ParameterType<Args>::type
> (args)...);
void callCheckedExcluding(ListenerClass *listenerToExclude, const BailOutCheckerType &bailOutChecker, Callback &&callback)
void callExcluding(ListenerClass *listenerToExclude, Callback &&callback)
bool isEmpty() const noexcept
bool contains(ListenerClass *listener) const noexcept
const ArrayType & getListeners() const noexcept
int size() const noexcept
void callChecked(const BailOutCheckerType &bailOutChecker, Callback &&callback)
void call(Callback &&callback)
void add(ListenerClass *listenerToAdd)