/* ============================================================================== This file is part of the JUCE library. Copyright (c) 2017 - ROLI Ltd. JUCE is an open source library subject to commercial or open-source licensing. The code included in this file is provided under the terms of the ISC license http://www.isc.org/downloads/software-support-policy/isc-license. Permission To use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted provided that the above copyright notice and this permission notice appear in all copies. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE DISCLAIMED. ============================================================================== */ namespace juce { class ActionBroadcaster::ActionMessage : public MessageManager::MessageBase { public: ActionMessage (const ActionBroadcaster* ab, const String& messageText, ActionListener* l) noexcept : broadcaster (const_cast (ab)), message (messageText), listener (l) {} void messageCallback() override { if (auto b = broadcaster.get()) if (b->actionListeners.contains (listener)) listener->actionListenerCallback (message); } private: WeakReference broadcaster; const String message; ActionListener* const listener; JUCE_DECLARE_NON_COPYABLE (ActionMessage) }; //============================================================================== ActionBroadcaster::ActionBroadcaster() { // are you trying to create this object before or after juce has been intialised?? JUCE_ASSERT_MESSAGE_MANAGER_EXISTS } ActionBroadcaster::~ActionBroadcaster() { // all event-based objects must be deleted BEFORE juce is shut down! JUCE_ASSERT_MESSAGE_MANAGER_EXISTS } void ActionBroadcaster::addActionListener (ActionListener* const listener) { const ScopedLock sl (actionListenerLock); if (listener != nullptr) actionListeners.add (listener); } void ActionBroadcaster::removeActionListener (ActionListener* const listener) { const ScopedLock sl (actionListenerLock); actionListeners.removeValue (listener); } void ActionBroadcaster::removeAllActionListeners() { const ScopedLock sl (actionListenerLock); actionListeners.clear(); } void ActionBroadcaster::sendActionMessage (const String& message) const { const ScopedLock sl (actionListenerLock); for (int i = actionListeners.size(); --i >= 0;) (new ActionMessage (this, message, actionListeners.getUnchecked(i)))->post(); } } // namespace juce