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.

104 lines
2.9KB

  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. struct MultiTimerCallback : public Timer
  18. {
  19. MultiTimerCallback (const int tid, MultiTimer& mt) noexcept
  20. : owner (mt), timerID (tid)
  21. {
  22. }
  23. void timerCallback() override
  24. {
  25. owner.timerCallback (timerID);
  26. }
  27. MultiTimer& owner;
  28. const int timerID;
  29. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiTimerCallback)
  30. };
  31. //==============================================================================
  32. MultiTimer::MultiTimer() noexcept {}
  33. MultiTimer::MultiTimer (const MultiTimer&) noexcept {}
  34. MultiTimer::~MultiTimer()
  35. {
  36. const SpinLock::ScopedLockType sl (timerListLock);
  37. timers.clear();
  38. }
  39. //==============================================================================
  40. Timer* MultiTimer::getCallback (int timerID) const noexcept
  41. {
  42. for (int i = timers.size(); --i >= 0;)
  43. {
  44. MultiTimerCallback* const t = static_cast<MultiTimerCallback*> (timers.getUnchecked(i));
  45. if (t->timerID == timerID)
  46. return t;
  47. }
  48. return nullptr;
  49. }
  50. void MultiTimer::startTimer (const int timerID, const int intervalInMilliseconds) noexcept
  51. {
  52. const SpinLock::ScopedLockType sl (timerListLock);
  53. Timer* timer = getCallback (timerID);
  54. if (timer == nullptr)
  55. timers.add (timer = new MultiTimerCallback (timerID, *this));
  56. timer->startTimer (intervalInMilliseconds);
  57. }
  58. void MultiTimer::stopTimer (const int timerID) noexcept
  59. {
  60. const SpinLock::ScopedLockType sl (timerListLock);
  61. if (Timer* const t = getCallback (timerID))
  62. t->stopTimer();
  63. }
  64. bool MultiTimer::isTimerRunning (const int timerID) const noexcept
  65. {
  66. const SpinLock::ScopedLockType sl (timerListLock);
  67. if (Timer* const t = getCallback (timerID))
  68. return t->isTimerRunning();
  69. return false;
  70. }
  71. int MultiTimer::getTimerInterval (const int timerID) const noexcept
  72. {
  73. const SpinLock::ScopedLockType sl (timerListLock);
  74. if (Timer* const t = getCallback (timerID))
  75. return t->getTimerInterval();
  76. return 0;
  77. }