OpenShot Library | OpenShotAudio  0.2.1
juce_MultiTimer.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 struct MultiTimerCallback : public Timer
27 {
28  MultiTimerCallback (const int tid, MultiTimer& mt) noexcept
29  : owner (mt), timerID (tid)
30  {
31  }
32 
33  void timerCallback() override
34  {
35  owner.timerCallback (timerID);
36  }
37 
38  MultiTimer& owner;
39  const int timerID;
40 
41  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiTimerCallback)
42 };
43 
44 //==============================================================================
46 MultiTimer::MultiTimer (const MultiTimer&) noexcept {}
47 
49 {
50  const SpinLock::ScopedLockType sl (timerListLock);
51  timers.clear();
52 }
53 
54 //==============================================================================
55 Timer* MultiTimer::getCallback (int timerID) const noexcept
56 {
57  for (int i = timers.size(); --i >= 0;)
58  {
59  MultiTimerCallback* const t = static_cast<MultiTimerCallback*> (timers.getUnchecked(i));
60 
61  if (t->timerID == timerID)
62  return t;
63  }
64 
65  return nullptr;
66 }
67 
68 void MultiTimer::startTimer (const int timerID, const int intervalInMilliseconds) noexcept
69 {
70  const SpinLock::ScopedLockType sl (timerListLock);
71 
72  Timer* timer = getCallback (timerID);
73 
74  if (timer == nullptr)
75  timers.add (timer = new MultiTimerCallback (timerID, *this));
76 
77  timer->startTimer (intervalInMilliseconds);
78 }
79 
80 void MultiTimer::stopTimer (const int timerID) noexcept
81 {
82  const SpinLock::ScopedLockType sl (timerListLock);
83 
84  if (Timer* const t = getCallback (timerID))
85  t->stopTimer();
86 }
87 
88 bool MultiTimer::isTimerRunning (const int timerID) const noexcept
89 {
90  const SpinLock::ScopedLockType sl (timerListLock);
91 
92  if (Timer* const t = getCallback (timerID))
93  return t->isTimerRunning();
94 
95  return false;
96 }
97 
98 int MultiTimer::getTimerInterval (const int timerID) const noexcept
99 {
100  const SpinLock::ScopedLockType sl (timerListLock);
101 
102  if (Timer* const t = getCallback (timerID))
103  return t->getTimerInterval();
104 
105  return 0;
106 }
107 
108 } // namespace juce
void startTimer(int intervalInMilliseconds) noexcept
Starts the timer and sets the length of interval required.
Definition: juce_Timer.cpp:323
MultiTimer() noexcept
Creates a MultiTimer.
virtual ~MultiTimer()
Destructor.
void stopTimer(int timerID) noexcept
Stops a timer.
A type of timer class that can run multiple timers with different frequencies, all of which share a s...
int getTimerInterval(int timerID) const noexcept
Returns the interval for a specified timer ID.
virtual void timerCallback(int timerID)=0
The user-defined callback routine that actually gets called by each of the timers that are running...
bool isTimerRunning(int timerID) const noexcept
Checks whether a timer has been started for a specified ID.
void startTimer(int timerID, int intervalInMilliseconds) noexcept
Starts a timer and sets the length of interval required.
Automatically locks and unlocks a mutex object.
Makes repeated callbacks to a virtual method at a specified time interval.
Definition: juce_Timer.h:55
void timerCallback() override
The user-defined callback routine that actually gets called periodically.