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 {
MultiTimerCallbackjuce::MultiTimerCallback28     MultiTimerCallback (const int tid, MultiTimer& mt) noexcept
29         : owner (mt), timerID (tid)
30     {
31     }
32 
timerCallbackjuce::MultiTimerCallback33     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 //==============================================================================
MultiTimer()45 MultiTimer::MultiTimer() noexcept {}
MultiTimer(const MultiTimer &)46 MultiTimer::MultiTimer (const MultiTimer&) noexcept {}
47 
~MultiTimer()48 MultiTimer::~MultiTimer()
49 {
50     const SpinLock::ScopedLockType sl (timerListLock);
51     timers.clear();
52 }
53 
54 //==============================================================================
getCallback(int timerID) const55 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 
startTimer(const int timerID,const int intervalInMilliseconds)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 
stopTimer(const int timerID)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 
isTimerRunning(const int timerID) const88 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 
getTimerInterval(const int timerID) const98 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
109