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.

juce_ActionBroadcaster.cpp 2.9KB

10 years ago
10 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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* ab,
  21. const String& messageText, ActionListener* l) noexcept
  22. : broadcaster (const_cast<ActionBroadcaster*> (ab)),
  23. message (messageText),
  24. listener (l)
  25. {}
  26. void messageCallback() override
  27. {
  28. if (const ActionBroadcaster* const b = broadcaster)
  29. if (b->actionListeners.contains (listener))
  30. listener->actionListenerCallback (message);
  31. }
  32. private:
  33. WeakReference<ActionBroadcaster> broadcaster;
  34. const String message;
  35. ActionListener* const listener;
  36. JUCE_DECLARE_NON_COPYABLE (ActionMessage)
  37. };
  38. //==============================================================================
  39. ActionBroadcaster::ActionBroadcaster()
  40. {
  41. // are you trying to create this object before or after juce has been intialised??
  42. jassert (MessageManager::getInstanceWithoutCreating() != nullptr);
  43. }
  44. ActionBroadcaster::~ActionBroadcaster()
  45. {
  46. // all event-based objects must be deleted BEFORE juce is shut down!
  47. jassert (MessageManager::getInstanceWithoutCreating() != nullptr);
  48. masterReference.clear();
  49. }
  50. void ActionBroadcaster::addActionListener (ActionListener* const listener)
  51. {
  52. const ScopedLock sl (actionListenerLock);
  53. if (listener != nullptr)
  54. actionListeners.add (listener);
  55. }
  56. void ActionBroadcaster::removeActionListener (ActionListener* const listener)
  57. {
  58. const ScopedLock sl (actionListenerLock);
  59. actionListeners.removeValue (listener);
  60. }
  61. void ActionBroadcaster::removeAllActionListeners()
  62. {
  63. const ScopedLock sl (actionListenerLock);
  64. actionListeners.clear();
  65. }
  66. void ActionBroadcaster::sendActionMessage (const String& message) const
  67. {
  68. const ScopedLock sl (actionListenerLock);
  69. for (int i = actionListeners.size(); --i >= 0;)
  70. (new ActionMessage (this, message, actionListeners.getUnchecked(i)))->post();
  71. }