Cleanup: remove redundant doxygen \file argument
[blender.git] / intern / ghost / intern / GHOST_TimerManager.cpp
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup GHOST
21  */
22
23
24 /**
25  * Copyright (C) 2001 NaN Technologies B.V.
26  */
27
28 #include "GHOST_TimerManager.h"
29
30 #include <algorithm>
31
32 #include "GHOST_TimerTask.h"
33
34
35 GHOST_TimerManager::GHOST_TimerManager()
36 {
37 }
38
39
40 GHOST_TimerManager::~GHOST_TimerManager()
41 {
42         disposeTimers();
43 }
44
45
46 GHOST_TUns32 GHOST_TimerManager::getNumTimers()
47 {
48         return (GHOST_TUns32)m_timers.size();
49 }
50
51
52 bool GHOST_TimerManager::getTimerFound(GHOST_TimerTask *timer)
53 {
54         TTimerVector::const_iterator iter = std::find(m_timers.begin(), m_timers.end(), timer);
55         return iter != m_timers.end();
56 }
57
58
59 GHOST_TSuccess GHOST_TimerManager::addTimer(GHOST_TimerTask *timer)
60 {
61         GHOST_TSuccess success;
62         if (!getTimerFound(timer)) {
63                 // Add the timer task
64                 m_timers.push_back(timer);
65                 success = GHOST_kSuccess;
66         }
67         else {
68                 success = GHOST_kFailure;
69         }
70         return success;
71 }
72
73
74 GHOST_TSuccess GHOST_TimerManager::removeTimer(GHOST_TimerTask *timer)
75 {
76         GHOST_TSuccess success;
77         TTimerVector::iterator iter = std::find(m_timers.begin(), m_timers.end(), timer);
78         if (iter != m_timers.end()) {
79                 // Remove the timer task
80                 m_timers.erase(iter);
81                 delete timer;
82                 success = GHOST_kSuccess;
83         }
84         else {
85                 success = GHOST_kFailure;
86         }
87         return success;
88 }
89
90 GHOST_TUns64 GHOST_TimerManager::nextFireTime()
91 {
92         GHOST_TUns64 smallest = GHOST_kFireTimeNever;
93         TTimerVector::iterator iter;
94
95         for (iter = m_timers.begin(); iter != m_timers.end(); ++iter) {
96                 GHOST_TUns64 next = (*iter)->getNext();
97
98                 if (next < smallest)
99                         smallest = next;
100         }
101
102         return smallest;
103 }
104
105 bool GHOST_TimerManager::fireTimers(GHOST_TUns64 time)
106 {
107         TTimerVector::iterator iter;
108         bool anyProcessed = false;
109
110         for (iter = m_timers.begin(); iter != m_timers.end(); ++iter) {
111                 if (fireTimer(time, *iter))
112                         anyProcessed = true;
113         }
114
115         return anyProcessed;
116 }
117
118
119 bool GHOST_TimerManager::fireTimer(GHOST_TUns64 time, GHOST_TimerTask *task)
120 {
121         GHOST_TUns64 next = task->getNext();
122
123         // Check if the timer should be fired
124         if (time > next) {
125                 // Fire the timer
126                 GHOST_TimerProcPtr timerProc = task->getTimerProc();
127                 GHOST_TUns64 start = task->getStart();
128                 timerProc(task, time - start);
129
130                 // Update the time at which we will fire it again
131                 GHOST_TUns64 interval = task->getInterval();
132                 GHOST_TUns64 numCalls = (next - start) / interval;
133                 numCalls++;
134                 next = start + numCalls * interval;
135                 task->setNext(next);
136
137                 return true;
138         }
139         else {
140                 return false;
141         }
142 }
143
144
145 void GHOST_TimerManager::disposeTimers()
146 {
147         while (m_timers.empty() == false) {
148                 delete m_timers[0];
149                 m_timers.erase(m_timers.begin());
150         }
151 }