The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

112 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_ASYNCUPDATER_JUCEHEADER__
  19. #define __JUCE_ASYNCUPDATER_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. Has a callback method that is triggered asynchronously.
  23. This object allows an asynchronous callback function to be triggered, for
  24. tasks such as coalescing multiple updates into a single callback later on.
  25. Basically, one or more calls to the triggerAsyncUpdate() will result in the
  26. message thread calling handleAsyncUpdate() as soon as it can.
  27. */
  28. class JUCE_API AsyncUpdater
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates an AsyncUpdater object. */
  33. AsyncUpdater();
  34. /** Destructor.
  35. If there are any pending callbacks when the object is deleted, these are lost.
  36. */
  37. virtual ~AsyncUpdater();
  38. //==============================================================================
  39. /** Causes the callback to be triggered at a later time.
  40. This method returns immediately, having made sure that a callback
  41. to the handleAsyncUpdate() method will occur as soon as possible.
  42. If an update callback is already pending but hasn't happened yet, calls
  43. to this method will be ignored.
  44. It's thread-safe to call this method from any number of threads without
  45. needing to worry about locking.
  46. */
  47. void triggerAsyncUpdate();
  48. /** This will stop any pending updates from happening.
  49. If called after triggerAsyncUpdate() and before the handleAsyncUpdate()
  50. callback happens, this will cancel the handleAsyncUpdate() callback.
  51. Note that this method simply cancels the next callback - if a callback is already
  52. in progress on a different thread, this won't block until it finishes, so there's
  53. no guarantee that the callback isn't still running when you return from
  54. */
  55. void cancelPendingUpdate() noexcept;
  56. /** If an update has been triggered and is pending, this will invoke it
  57. synchronously.
  58. Use this as a kind of "flush" operation - if an update is pending, the
  59. handleAsyncUpdate() method will be called immediately; if no update is
  60. pending, then nothing will be done.
  61. Because this may invoke the callback, this method must only be called on
  62. the main event thread.
  63. */
  64. void handleUpdateNowIfNeeded();
  65. /** Returns true if there's an update callback in the pipeline. */
  66. bool isUpdatePending() const noexcept;
  67. //==============================================================================
  68. /** Called back to do whatever your class needs to do.
  69. This method is called by the message thread at the next convenient time
  70. after the triggerAsyncUpdate() method has been called.
  71. */
  72. virtual void handleAsyncUpdate() = 0;
  73. private:
  74. //==============================================================================
  75. class AsyncUpdaterMessage;
  76. friend class ReferenceCountedObjectPtr<AsyncUpdaterMessage>;
  77. ReferenceCountedObjectPtr<AsyncUpdaterMessage> message;
  78. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AsyncUpdater);
  79. };
  80. #endif // __JUCE_ASYNCUPDATER_JUCEHEADER__