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.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_HIGHRESOLUTIONTIMER_H_INCLUDED
  24. #define JUCE_HIGHRESOLUTIONTIMER_H_INCLUDED
  25. /**
  26. A high-resolution periodic timer.
  27. This provides accurately-timed regular callbacks. Unlike the normal Timer
  28. class, this one uses a dedicated thread, not the message thread, so is
  29. far more stable and precise.
  30. You should only use this class in situations where you really need accuracy,
  31. because unlike the normal Timer class, which is very lightweight and cheap
  32. to start/stop, the HighResolutionTimer will use far more resources, and
  33. starting/stopping it may involve launching and killing threads.
  34. @see Timer
  35. */
  36. class JUCE_API HighResolutionTimer
  37. {
  38. protected:
  39. /** Creates a HighResolutionTimer.
  40. When created, the timer is stopped, so use startTimer() to get it going.
  41. */
  42. HighResolutionTimer();
  43. public:
  44. /** Destructor. */
  45. virtual ~HighResolutionTimer();
  46. //==============================================================================
  47. /** The user-defined callback routine that actually gets called periodically.
  48. This will be called on a dedicated timer thread, so make sure your
  49. implementation is thread-safe!
  50. It's perfectly ok to call startTimer() or stopTimer() from within this
  51. callback to change the subsequent intervals.
  52. */
  53. virtual void hiResTimerCallback() = 0;
  54. //==============================================================================
  55. /** Starts the timer and sets the length of interval required.
  56. If the timer is already started, this will reset its counter, so the
  57. time between calling this method and the next timer callback will not be
  58. less than the interval length passed in.
  59. @param intervalInMilliseconds the interval to use (any values less than 1 will be
  60. rounded up to 1)
  61. */
  62. void startTimer (int intervalInMilliseconds);
  63. /** Stops the timer.
  64. This method may block while it waits for pending callbacks to complete. Once it
  65. returns, no more callbacks will be made. If it is called from the timer's own thread,
  66. it will cancel the timer after the current callback returns.
  67. */
  68. void stopTimer();
  69. /** Checks if the timer has been started.
  70. @returns true if the timer is running.
  71. */
  72. bool isTimerRunning() const noexcept;
  73. /** Returns the timer's interval.
  74. @returns the timer's interval in milliseconds if it's running, or 0 if it's not.
  75. */
  76. int getTimerInterval() const noexcept;
  77. private:
  78. struct Pimpl;
  79. friend struct Pimpl;
  80. friend struct ContainerDeletePolicy<Pimpl>;
  81. ScopedPointer<Pimpl> pimpl;
  82. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HighResolutionTimer)
  83. };
  84. #endif // JUCE_HIGHRESOLUTIONTIMER_H_INCLUDED