doxygen: add newline after \file
[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
21  * \ingroup GHOST
22  */
23
24
25 /**
26  * Copyright (C) 2001 NaN Technologies B.V.
27  */
28
29 #include "GHOST_TimerManager.h"
30
31 #include <algorithm>
32
33 #include "GHOST_TimerTask.h"
34
35
36 GHOST_TimerManager::GHOST_TimerManager()
37 {
38 }
39
40
41 GHOST_TimerManager::~GHOST_TimerManager()
42 {
43         disposeTimers();
44 }
45
46
47 GHOST_TUns32 GHOST_TimerManager::getNumTimers()
48 {
49         return (GHOST_TUns32)m_timers.size();
50 }
51
52
53 bool GHOST_TimerManager::getTimerFound(GHOST_TimerTask *timer)
54 {
55         TTimerVector::const_iterator iter = std::find(m_timers.begin(), m_timers.end(), timer);
56         return iter != m_timers.end();
57 }
58
59
60 GHOST_TSuccess GHOST_TimerManager::addTimer(GHOST_TimerTask *timer)
61 {
62         GHOST_TSuccess success;
63         if (!getTimerFound(timer)) {
64                 // Add the timer task
65                 m_timers.push_back(timer);
66                 success = GHOST_kSuccess;
67         }
68         else {
69                 success = GHOST_kFailure;
70         }
71         return success;
72 }
73
74
75 GHOST_TSuccess GHOST_TimerManager::removeTimer(GHOST_TimerTask *timer)
76 {
77         GHOST_TSuccess success;
78         TTimerVector::iterator iter = std::find(m_timers.begin(), m_timers.end(), timer);
79         if (iter != m_timers.end()) {
80                 // Remove the timer task
81                 m_timers.erase(iter);
82                 delete timer;
83                 success = GHOST_kSuccess;
84         }
85         else {
86                 success = GHOST_kFailure;
87         }
88         return success;
89 }
90
91 GHOST_TUns64 GHOST_TimerManager::nextFireTime()
92 {
93         GHOST_TUns64 smallest = GHOST_kFireTimeNever;
94         TTimerVector::iterator iter;
95
96         for (iter = m_timers.begin(); iter != m_timers.end(); ++iter) {
97                 GHOST_TUns64 next = (*iter)->getNext();
98
99                 if (next < smallest)
100                         smallest = next;
101         }
102
103         return smallest;
104 }
105
106 bool GHOST_TimerManager::fireTimers(GHOST_TUns64 time)
107 {
108         TTimerVector::iterator iter;
109         bool anyProcessed = false;
110
111         for (iter = m_timers.begin(); iter != m_timers.end(); ++iter) {
112                 if (fireTimer(time, *iter))
113                         anyProcessed = true;
114         }
115
116         return anyProcessed;
117 }
118
119
120 bool GHOST_TimerManager::fireTimer(GHOST_TUns64 time, GHOST_TimerTask *task)
121 {
122         GHOST_TUns64 next = task->getNext();
123
124         // Check if the timer should be fired
125         if (time > next) {
126                 // Fire the timer
127                 GHOST_TimerProcPtr timerProc = task->getTimerProc();
128                 GHOST_TUns64 start = task->getStart();
129                 timerProc(task, time - start);
130
131                 // Update the time at which we will fire it again
132                 GHOST_TUns64 interval = task->getInterval();
133                 GHOST_TUns64 numCalls = (next - start) / interval;
134                 numCalls++;
135                 next = start + numCalls * interval;
136                 task->setNext(next);
137
138                 return true;
139         }
140         else {
141                 return false;
142         }
143 }
144
145
146 void GHOST_TimerManager::disposeTimers()
147 {
148         while (m_timers.empty() == false) {
149                 delete m_timers[0];
150                 m_timers.erase(m_timers.begin());
151         }
152 }