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_AsyncUpdater.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef JUCE_ASYNCUPDATER_H_INCLUDED
  18. #define JUCE_ASYNCUPDATER_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Has a callback method that is triggered asynchronously.
  22. This object allows an asynchronous callback function to be triggered, for
  23. tasks such as coalescing multiple updates into a single callback later on.
  24. Basically, one or more calls to the triggerAsyncUpdate() will result in the
  25. message thread calling handleAsyncUpdate() as soon as it can.
  26. */
  27. class JUCE_API AsyncUpdater
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates an AsyncUpdater object. */
  32. AsyncUpdater();
  33. /** Destructor.
  34. If there are any pending callbacks when the object is deleted, these are lost.
  35. */
  36. virtual ~AsyncUpdater();
  37. //==============================================================================
  38. /** Causes the callback to be triggered at a later time.
  39. This method returns immediately, having made sure that a callback
  40. to the handleAsyncUpdate() method will occur as soon as possible.
  41. If an update callback is already pending but hasn't happened yet, calls
  42. to this method will be ignored.
  43. It's thread-safe to call this method from any number of threads without
  44. needing to worry about locking.
  45. */
  46. void triggerAsyncUpdate();
  47. /** This will stop any pending updates from happening.
  48. If called after triggerAsyncUpdate() and before the handleAsyncUpdate()
  49. callback happens, this will cancel the handleAsyncUpdate() callback.
  50. Note that this method simply cancels the next callback - if a callback is already
  51. in progress on a different thread, this won't block until it finishes, so there's
  52. no guarantee that the callback isn't still running when you return from
  53. */
  54. void cancelPendingUpdate() noexcept;
  55. /** If an update has been triggered and is pending, this will invoke it
  56. synchronously.
  57. Use this as a kind of "flush" operation - if an update is pending, the
  58. handleAsyncUpdate() method will be called immediately; if no update is
  59. pending, then nothing will be done.
  60. Because this may invoke the callback, this method must only be called on
  61. the main event thread.
  62. */
  63. void handleUpdateNowIfNeeded();
  64. /** Returns true if there's an update callback in the pipeline. */
  65. bool isUpdatePending() const noexcept;
  66. //==============================================================================
  67. /** Called back to do whatever your class needs to do.
  68. This method is called by the message thread at the next convenient time
  69. after the triggerAsyncUpdate() method has been called.
  70. */
  71. virtual void handleAsyncUpdate() = 0;
  72. private:
  73. //==============================================================================
  74. class AsyncUpdaterMessage;
  75. friend class ReferenceCountedObjectPtr<AsyncUpdaterMessage>;
  76. ReferenceCountedObjectPtr<AsyncUpdaterMessage> activeMessage;
  77. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AsyncUpdater)
  78. };
  79. #endif // JUCE_ASYNCUPDATER_H_INCLUDED