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.

111 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. namespace juce
  18. {
  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. @tags{Events}
  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, after which a callback to the
  41. handleAsyncUpdate() method will be made by the message thread as
  42. soon as possible.
  43. If an update callback is already pending but hasn't happened yet, calling
  44. this method will have no effect.
  45. It's thread-safe to call this method from any thread, BUT beware of calling
  46. it from a real-time (e.g. audio) thread, because it involves posting a message
  47. to the system queue, which means it may block (and in general will do on
  48. most OSes).
  49. */
  50. void triggerAsyncUpdate();
  51. /** This will stop any pending updates from happening.
  52. If called after triggerAsyncUpdate() and before the handleAsyncUpdate()
  53. callback happens, this will cancel the handleAsyncUpdate() callback.
  54. Note that this method simply cancels the next callback - if a callback is already
  55. in progress on a different thread, this won't block until the callback finishes, so
  56. there's no guarantee that the callback isn't still running when the method returns.
  57. */
  58. void cancelPendingUpdate() noexcept;
  59. /** If an update has been triggered and is pending, this will invoke it
  60. synchronously.
  61. Use this as a kind of "flush" operation - if an update is pending, the
  62. handleAsyncUpdate() method will be called immediately; if no update is
  63. pending, then nothing will be done.
  64. Because this may invoke the callback, this method must only be called on
  65. the main event thread.
  66. */
  67. void handleUpdateNowIfNeeded();
  68. /** Returns true if there's an update callback in the pipeline. */
  69. bool isUpdatePending() const noexcept;
  70. //==============================================================================
  71. /** Called back to do whatever your class needs to do.
  72. This method is called by the message thread at the next convenient time
  73. after the triggerAsyncUpdate() method has been called.
  74. */
  75. virtual void handleAsyncUpdate() = 0;
  76. private:
  77. //==============================================================================
  78. class AsyncUpdaterMessage;
  79. friend class ReferenceCountedObjectPtr<AsyncUpdaterMessage>;
  80. ReferenceCountedObjectPtr<AsyncUpdaterMessage> activeMessage;
  81. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AsyncUpdater)
  82. };
  83. } // namespace juce