Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class ActionBroadcaster::ActionMessage : public MessageManager::MessageBase
  18. {
  19. public:
  20. ActionMessage (const ActionBroadcaster* const broadcaster_,
  21. const String& messageText,
  22. ActionListener* const listener_) noexcept
  23. : broadcaster (const_cast <ActionBroadcaster*> (broadcaster_)),
  24. message (messageText),
  25. listener (listener_)
  26. {}
  27. void messageCallback() override
  28. {
  29. if (const ActionBroadcaster* const b = broadcaster)
  30. if (b->actionListeners.contains (listener))
  31. listener->actionListenerCallback (message);
  32. }
  33. private:
  34. WeakReference<ActionBroadcaster> broadcaster;
  35. const String message;
  36. ActionListener* const listener;
  37. JUCE_DECLARE_NON_COPYABLE (ActionMessage)
  38. };
  39. //==============================================================================
  40. ActionBroadcaster::ActionBroadcaster()
  41. {
  42. // are you trying to create this object before or after juce has been intialised??
  43. jassert (MessageManager::getInstanceWithoutCreating() != nullptr);
  44. }
  45. ActionBroadcaster::~ActionBroadcaster()
  46. {
  47. // all event-based objects must be deleted BEFORE juce is shut down!
  48. jassert (MessageManager::getInstanceWithoutCreating() != nullptr);
  49. masterReference.clear();
  50. }
  51. void ActionBroadcaster::addActionListener (ActionListener* const listener)
  52. {
  53. const ScopedLock sl (actionListenerLock);
  54. if (listener != nullptr)
  55. actionListeners.add (listener);
  56. }
  57. void ActionBroadcaster::removeActionListener (ActionListener* const listener)
  58. {
  59. const ScopedLock sl (actionListenerLock);
  60. actionListeners.removeValue (listener);
  61. }
  62. void ActionBroadcaster::removeAllActionListeners()
  63. {
  64. const ScopedLock sl (actionListenerLock);
  65. actionListeners.clear();
  66. }
  67. void ActionBroadcaster::sendActionMessage (const String& message) const
  68. {
  69. const ScopedLock sl (actionListenerLock);
  70. for (int i = actionListeners.size(); --i >= 0;)
  71. (new ActionMessage (this, message, actionListeners.getUnchecked(i)))->post();
  72. }