juce_MultiTimer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. A type of timer class that can run multiple timers with different frequencies,
  22. all of which share a single callback.
  23. This class is very similar to the Timer class, but allows you run multiple
  24. separate timers, where each one has a unique ID number. The methods in this
  25. class are exactly equivalent to those in Timer, but with the addition of
  26. this ID number.
  27. To use it, you need to create a subclass of MultiTimer, implementing the
  28. timerCallback() method. Then you can start timers with startTimer(), and
  29. each time the callback is triggered, it passes in the ID of the timer that
  30. caused it.
  31. @see Timer
  32. @tags{Events}
  33. */
  34. class JUCE_API MultiTimer
  35. {
  36. protected:
  37. //==============================================================================
  38. /** Creates a MultiTimer.
  39. When created, no timers are running, so use startTimer() to start things off.
  40. */
  41. MultiTimer() noexcept;
  42. /** Creates a copy of another timer.
  43. Note that this timer will not contain any running timers, even if the one you're
  44. copying from was running.
  45. */
  46. MultiTimer (const MultiTimer&) noexcept;
  47. public:
  48. //==============================================================================
  49. /** Destructor. */
  50. virtual ~MultiTimer();
  51. //==============================================================================
  52. /** The user-defined callback routine that actually gets called by each of the
  53. timers that are running.
  54. It's perfectly ok to call startTimer() or stopTimer() from within this
  55. callback to change the subsequent intervals.
  56. */
  57. virtual void timerCallback (int timerID) = 0;
  58. //==============================================================================
  59. /** Starts a timer and sets the length of interval required.
  60. If the timer is already started, this will reset it, so the
  61. time between calling this method and the next timer callback
  62. will not be less than the interval length passed in.
  63. @param timerID a unique Id number that identifies the timer to
  64. start. This is the id that will be passed back
  65. to the timerCallback() method when this timer is
  66. triggered
  67. @param intervalInMilliseconds the interval to use (any values less than 1 will be
  68. rounded up to 1)
  69. */
  70. void startTimer (int timerID, int intervalInMilliseconds) noexcept;
  71. /** Stops a timer.
  72. If a timer has been started with the given ID number, it will be cancelled.
  73. No more callbacks will be made for the specified timer after this method returns.
  74. If this is called from a different thread, any callbacks that may
  75. be currently executing may be allowed to finish before the method
  76. returns.
  77. */
  78. void stopTimer (int timerID) noexcept;
  79. //==============================================================================
  80. /** Checks whether a timer has been started for a specified ID.
  81. @returns true if a timer with the given ID is running.
  82. */
  83. bool isTimerRunning (int timerID) const noexcept;
  84. /** Returns the interval for a specified timer ID.
  85. @returns the timer's interval in milliseconds if it's running, or 0 if no
  86. timer was running for the ID number specified.
  87. */
  88. int getTimerInterval (int timerID) const noexcept;
  89. //==============================================================================
  90. private:
  91. SpinLock timerListLock;
  92. OwnedArray<Timer> timers;
  93. Timer* getCallback (int) const noexcept;
  94. MultiTimer& operator= (const MultiTimer&);
  95. };
  96. } // namespace juce