OpenShot Library | libopenshot-audio  0.1.9
juce_MultiTimer.h
1 
2 /** @weakgroup juce_events-timers
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  A type of timer class that can run multiple timers with different frequencies,
33  all of which share a single callback.
34 
35  This class is very similar to the Timer class, but allows you run multiple
36  separate timers, where each one has a unique ID number. The methods in this
37  class are exactly equivalent to those in Timer, but with the addition of
38  this ID number.
39 
40  To use it, you need to create a subclass of MultiTimer, implementing the
41  timerCallback() method. Then you can start timers with startTimer(), and
42  each time the callback is triggered, it passes in the ID of the timer that
43  caused it.
44 
45  @see Timer
46 
47  @tags{Events}
48 */
50 {
51 protected:
52  //==============================================================================
53  /** Creates a MultiTimer.
54 
55  When created, no timers are running, so use startTimer() to start things off.
56  */
57  MultiTimer() noexcept;
58 
59  /** Creates a copy of another timer.
60 
61  Note that this timer will not contain any running timers, even if the one you're
62  copying from was running.
63  */
64  MultiTimer (const MultiTimer&) noexcept;
65 
66 public:
67  //==============================================================================
68  /** Destructor. */
69  virtual ~MultiTimer();
70 
71  //==============================================================================
72  /** The user-defined callback routine that actually gets called by each of the
73  timers that are running.
74 
75  It's perfectly ok to call startTimer() or stopTimer() from within this
76  callback to change the subsequent intervals.
77  */
78  virtual void timerCallback (int timerID) = 0;
79 
80  //==============================================================================
81  /** Starts a timer and sets the length of interval required.
82 
83  If the timer is already started, this will reset it, so the
84  time between calling this method and the next timer callback
85  will not be less than the interval length passed in.
86 
87  @param timerID a unique Id number that identifies the timer to
88  start. This is the id that will be passed back
89  to the timerCallback() method when this timer is
90  triggered
91  @param intervalInMilliseconds the interval to use (any values less than 1 will be
92  rounded up to 1)
93  */
94  void startTimer (int timerID, int intervalInMilliseconds) noexcept;
95 
96  /** Stops a timer.
97 
98  If a timer has been started with the given ID number, it will be cancelled.
99  No more callbacks will be made for the specified timer after this method returns.
100 
101  If this is called from a different thread, any callbacks that may
102  be currently executing may be allowed to finish before the method
103  returns.
104  */
105  void stopTimer (int timerID) noexcept;
106 
107  //==============================================================================
108  /** Checks whether a timer has been started for a specified ID.
109  @returns true if a timer with the given ID is running.
110  */
111  bool isTimerRunning (int timerID) const noexcept;
112 
113  /** Returns the interval for a specified timer ID.
114  @returns the timer's interval in milliseconds if it's running, or 0 if no
115  timer was running for the ID number specified.
116  */
117  int getTimerInterval (int timerID) const noexcept;
118 
119 
120  //==============================================================================
121 private:
122  SpinLock timerListLock;
123  OwnedArray<Timer> timers;
124 
125  Timer* getCallback (int) const noexcept;
126  MultiTimer& operator= (const MultiTimer&);
127 };
128 
129 } // namespace juce
130 
131 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
A simple spin-lock class that can be used as a simple, low-overhead mutex for uncontended situations...
Definition: juce_SpinLock.h:45
A type of timer class that can run multiple timers with different frequencies, all of which share a s...
An array designed for holding objects.
Makes repeated callbacks to a virtual method at a specified time interval.
Definition: juce_Timer.h:55