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.

104 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. Has a callback method that is triggered asynchronously.
  21. This object allows an asynchronous callback function to be triggered, for
  22. tasks such as coalescing multiple updates into a single callback later on.
  23. Basically, one or more calls to the triggerAsyncUpdate() will result in the
  24. message thread calling handleAsyncUpdate() as soon as it can.
  25. */
  26. class JUCE_API AsyncUpdater
  27. {
  28. public:
  29. //==============================================================================
  30. /** Creates an AsyncUpdater object. */
  31. AsyncUpdater();
  32. /** Destructor.
  33. If there are any pending callbacks when the object is deleted, these are lost.
  34. */
  35. virtual ~AsyncUpdater();
  36. //==============================================================================
  37. /** Causes the callback to be triggered at a later time.
  38. This method returns immediately, having made sure that a callback
  39. to the handleAsyncUpdate() method will occur as soon as possible.
  40. If an update callback is already pending but hasn't happened yet, calls
  41. to this method will be ignored.
  42. It's thread-safe to call this method from any number of threads without
  43. needing to worry about locking.
  44. */
  45. void triggerAsyncUpdate();
  46. /** This will stop any pending updates from happening.
  47. If called after triggerAsyncUpdate() and before the handleAsyncUpdate()
  48. callback happens, this will cancel the handleAsyncUpdate() callback.
  49. Note that this method simply cancels the next callback - if a callback is already
  50. in progress on a different thread, this won't block until the callback finishes, so
  51. there's no guarantee that the callback isn't still running when the method returns.
  52. */
  53. void cancelPendingUpdate() noexcept;
  54. /** If an update has been triggered and is pending, this will invoke it
  55. synchronously.
  56. Use this as a kind of "flush" operation - if an update is pending, the
  57. handleAsyncUpdate() method will be called immediately; if no update is
  58. pending, then nothing will be done.
  59. Because this may invoke the callback, this method must only be called on
  60. the main event thread.
  61. */
  62. void handleUpdateNowIfNeeded();
  63. /** Returns true if there's an update callback in the pipeline. */
  64. bool isUpdatePending() const noexcept;
  65. //==============================================================================
  66. /** Called back to do whatever your class needs to do.
  67. This method is called by the message thread at the next convenient time
  68. after the triggerAsyncUpdate() method has been called.
  69. */
  70. virtual void handleAsyncUpdate() = 0;
  71. private:
  72. //==============================================================================
  73. class AsyncUpdaterMessage;
  74. friend class ReferenceCountedObjectPtr<AsyncUpdaterMessage>;
  75. ReferenceCountedObjectPtr<AsyncUpdaterMessage> activeMessage;
  76. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AsyncUpdater)
  77. };