Blender  V2.93
GHOST_TimerManager.cpp
Go to the documentation of this file.
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 
28 #include "GHOST_TimerManager.h"
29 
30 #include <algorithm>
31 
32 #include "GHOST_TimerTask.h"
33 
35 {
36 }
37 
39 {
40  disposeTimers();
41 }
42 
44 {
45  return (GHOST_TUns32)m_timers.size();
46 }
47 
49 {
50  TTimerVector::const_iterator iter = std::find(m_timers.begin(), m_timers.end(), timer);
51  return iter != m_timers.end();
52 }
53 
55 {
56  GHOST_TSuccess success;
57  if (!getTimerFound(timer)) {
58  // Add the timer task
59  m_timers.push_back(timer);
60  success = GHOST_kSuccess;
61  }
62  else {
63  success = GHOST_kFailure;
64  }
65  return success;
66 }
67 
69 {
70  GHOST_TSuccess success;
71  TTimerVector::iterator iter = std::find(m_timers.begin(), m_timers.end(), timer);
72  if (iter != m_timers.end()) {
73  // Remove the timer task
74  m_timers.erase(iter);
75  delete timer;
76  success = GHOST_kSuccess;
77  }
78  else {
79  success = GHOST_kFailure;
80  }
81  return success;
82 }
83 
85 {
87  TTimerVector::iterator iter;
88 
89  for (iter = m_timers.begin(); iter != m_timers.end(); ++iter) {
90  GHOST_TUns64 next = (*iter)->getNext();
91 
92  if (next < smallest)
93  smallest = next;
94  }
95 
96  return smallest;
97 }
98 
100 {
101  TTimerVector::iterator iter;
102  bool anyProcessed = false;
103 
104  for (iter = m_timers.begin(); iter != m_timers.end(); ++iter) {
105  if (fireTimer(time, *iter))
106  anyProcessed = true;
107  }
108 
109  return anyProcessed;
110 }
111 
113 {
114  GHOST_TUns64 next = task->getNext();
115 
116  // Check if the timer should be fired
117  if (time > next) {
118  // Fire the timer
119  GHOST_TimerProcPtr timerProc = task->getTimerProc();
120  GHOST_TUns64 start = task->getStart();
121  timerProc(task, time - start);
122 
123  // Update the time at which we will fire it again
124  GHOST_TUns64 interval = task->getInterval();
125  GHOST_TUns64 numCalls = (next - start) / interval;
126  numCalls++;
127  next = start + numCalls * interval;
128  task->setNext(next);
129 
130  return true;
131  }
132  else {
133  return false;
134  }
135 }
136 
138 {
139  while (m_timers.empty() == false) {
140  delete m_timers[0];
141  m_timers.erase(m_timers.begin());
142  }
143 }
unsigned int GHOST_TUns32
Definition: GHOST_Types.h:64
unsigned long long GHOST_TUns64
Definition: GHOST_Types.h:86
void(* GHOST_TimerProcPtr)(struct GHOST_TimerTaskHandle__ *task, GHOST_TUns64 time)
Definition: GHOST_Types.h:608
GHOST_TSuccess
Definition: GHOST_Types.h:91
@ GHOST_kFailure
Definition: GHOST_Types.h:91
@ GHOST_kSuccess
Definition: GHOST_Types.h:91
@ GHOST_kFireTimeNever
Definition: GHOST_Types.h:131
GHOST_TUns32 getNumTimers()
GHOST_TUns64 nextFireTime()
GHOST_TSuccess removeTimer(GHOST_TimerTask *timer)
bool fireTimer(GHOST_TUns64 time, GHOST_TimerTask *task)
bool fireTimers(GHOST_TUns64 time)
bool getTimerFound(GHOST_TimerTask *timer)
GHOST_TSuccess addTimer(GHOST_TimerTask *timer)
double time
static ulong * next
struct blender::compositor::@172::@174 task