juce_HighResolutionTimer.h 4.2 KB

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