Loading...
Searching...
No Matches
juce_ActionBroadcaster.cpp
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
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 final : public MessageManager::MessageBase
{
public:
ActionMessage (const ActionBroadcaster* ab,
const String& messageText, ActionListener* l) noexcept
: broadcaster (const_cast<ActionBroadcaster*> (ab)),
message (messageText),
listener (l)
{}
void messageCallback() override
{
if (auto b = broadcaster.get())
if (b->actionListeners.contains (listener))
listener->actionListenerCallback (message);
}
private:
WeakReference<ActionBroadcaster> 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 initialised??
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
juce::ActionBroadcaster::removeActionListener
void removeActionListener(ActionListener *listener)
Definition juce_ActionBroadcaster.cpp:72
juce::ActionBroadcaster::ActionBroadcaster
ActionBroadcaster()
Definition juce_ActionBroadcaster.cpp:52
juce::ActionBroadcaster::addActionListener
void addActionListener(ActionListener *listener)
Definition juce_ActionBroadcaster.cpp:64
juce::ActionBroadcaster::~ActionBroadcaster
virtual ~ActionBroadcaster()
Definition juce_ActionBroadcaster.cpp:58
juce::ActionBroadcaster::removeAllActionListeners
void removeAllActionListeners()
Definition juce_ActionBroadcaster.cpp:78
juce::ActionBroadcaster::sendActionMessage
void sendActionMessage(const String &message) const
Definition juce_ActionBroadcaster.cpp:84
juce::ActionListener
Definition juce_ActionListener.h:35
juce::MessageManager::MessageBase
Definition juce_MessageManager.h:182
juce::String
Definition juce_String.h:53