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.

122 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. class MultiTimer::MultiTimerCallback : public Timer
  19. {
  20. public:
  21. MultiTimerCallback (const int timerId_, MultiTimer& owner_)
  22. : timerId (timerId_),
  23. owner (owner_)
  24. {
  25. }
  26. void timerCallback()
  27. {
  28. owner.timerCallback (timerId);
  29. }
  30. const int timerId;
  31. private:
  32. MultiTimer& owner;
  33. };
  34. //==============================================================================
  35. MultiTimer::MultiTimer() noexcept
  36. {
  37. }
  38. MultiTimer::MultiTimer (const MultiTimer&) noexcept
  39. {
  40. }
  41. MultiTimer::~MultiTimer()
  42. {
  43. const SpinLock::ScopedLockType sl (timerListLock);
  44. timers.clear();
  45. }
  46. //==============================================================================
  47. void MultiTimer::startTimer (const int timerId, const int intervalInMilliseconds) noexcept
  48. {
  49. const SpinLock::ScopedLockType sl (timerListLock);
  50. for (int i = timers.size(); --i >= 0;)
  51. {
  52. MultiTimerCallback* const t = timers.getUnchecked(i);
  53. if (t->timerId == timerId)
  54. {
  55. t->startTimer (intervalInMilliseconds);
  56. return;
  57. }
  58. }
  59. MultiTimerCallback* const newTimer = new MultiTimerCallback (timerId, *this);
  60. timers.add (newTimer);
  61. newTimer->startTimer (intervalInMilliseconds);
  62. }
  63. void MultiTimer::stopTimer (const int timerId) noexcept
  64. {
  65. const SpinLock::ScopedLockType sl (timerListLock);
  66. for (int i = timers.size(); --i >= 0;)
  67. {
  68. MultiTimerCallback* const t = timers.getUnchecked(i);
  69. if (t->timerId == timerId)
  70. t->stopTimer();
  71. }
  72. }
  73. bool MultiTimer::isTimerRunning (const int timerId) const noexcept
  74. {
  75. const SpinLock::ScopedLockType sl (timerListLock);
  76. for (int i = timers.size(); --i >= 0;)
  77. {
  78. const MultiTimerCallback* const t = timers.getUnchecked(i);
  79. if (t->timerId == timerId)
  80. return t->isTimerRunning();
  81. }
  82. return false;
  83. }
  84. int MultiTimer::getTimerInterval (const int timerId) const noexcept
  85. {
  86. const SpinLock::ScopedLockType sl (timerListLock);
  87. for (int i = timers.size(); --i >= 0;)
  88. {
  89. const MultiTimerCallback* const t = timers.getUnchecked(i);
  90. if (t->timerId == timerId)
  91. return t->getTimerInterval();
  92. }
  93. return 0;
  94. }