Blender  V2.59
GHOST_TimerManager.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: GHOST_TimerManager.cpp 35152 2011-02-25 11:28:33Z jesterking $
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): none yet.
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00042 #include "GHOST_TimerManager.h"
00043 
00044 #include <algorithm>
00045 
00046 #include "GHOST_TimerTask.h"
00047 
00048 
00049 GHOST_TimerManager::GHOST_TimerManager()
00050 {
00051 }
00052 
00053 
00054 GHOST_TimerManager::~GHOST_TimerManager()
00055 {
00056         disposeTimers();
00057 }
00058 
00059 
00060 GHOST_TUns32 GHOST_TimerManager::getNumTimers()
00061 {
00062         return (GHOST_TUns32)m_timers.size();
00063 }
00064 
00065 
00066 bool GHOST_TimerManager::getTimerFound(GHOST_TimerTask* timer)
00067 {
00068         TTimerVector::const_iterator iter = std::find(m_timers.begin(), m_timers.end(), timer);
00069         return iter != m_timers.end();
00070 }
00071 
00072 
00073 GHOST_TSuccess GHOST_TimerManager::addTimer(GHOST_TimerTask* timer)
00074 {
00075         GHOST_TSuccess success;
00076         if (!getTimerFound(timer)) {
00077                 // Add the timer task
00078                 m_timers.push_back(timer);
00079                 success = GHOST_kSuccess;
00080         }
00081         else {
00082                 success = GHOST_kFailure;
00083         }
00084         return success;
00085 }
00086 
00087 
00088 GHOST_TSuccess GHOST_TimerManager::removeTimer(GHOST_TimerTask* timer)
00089 {
00090         GHOST_TSuccess success;
00091         TTimerVector::iterator iter = std::find(m_timers.begin(), m_timers.end(), timer);
00092         if (iter != m_timers.end()) {
00093                 // Remove the timer task
00094                 m_timers.erase(iter);
00095                 delete timer;
00096                 timer = 0;
00097                 success = GHOST_kSuccess;
00098         }
00099         else {
00100                 success = GHOST_kFailure;
00101         }
00102         return success;
00103 }
00104 
00105 GHOST_TUns64 GHOST_TimerManager::nextFireTime()
00106 {
00107         GHOST_TUns64 smallest = GHOST_kFireTimeNever;
00108         TTimerVector::iterator iter;
00109         
00110         for (iter = m_timers.begin(); iter != m_timers.end(); iter++) {
00111                 GHOST_TUns64 next = (*iter)->getNext();
00112                 
00113                 if (next<smallest)
00114                         smallest = next;
00115         }
00116         
00117         return smallest;
00118 }
00119 
00120 bool GHOST_TimerManager::fireTimers(GHOST_TUns64 time)
00121 {
00122         TTimerVector::iterator iter;
00123         bool anyProcessed = false;
00124 
00125         for (iter = m_timers.begin(); iter != m_timers.end(); iter++) {
00126                 if (fireTimer(time, *iter))
00127                         anyProcessed = true;
00128         }
00129 
00130         return anyProcessed;
00131 }
00132 
00133 
00134 bool GHOST_TimerManager::fireTimer(GHOST_TUns64 time, GHOST_TimerTask* task)
00135 {
00136         GHOST_TUns64 next = task->getNext();
00137 
00138         // Check if the timer should be fired
00139         if (time > next) {
00140                 // Fire the timer
00141                 GHOST_TimerProcPtr timerProc = task->getTimerProc();
00142                 GHOST_TUns64 start = task->getStart();
00143                 timerProc(task, time - start);
00144 
00145                 // Update the time at which we will fire it again
00146                 GHOST_TUns64 interval = task->getInterval();
00147                 GHOST_TUns64 numCalls = (next - start) / interval;
00148                 numCalls++;
00149                 next = start + numCalls * interval;
00150                 task->setNext(next);
00151 
00152                 return true;
00153         } else {
00154                 return false;
00155         }
00156 }
00157 
00158 
00159 void GHOST_TimerManager::disposeTimers()
00160 {
00161         while (m_timers.size() > 0) {
00162                 delete m_timers[0];
00163                 m_timers.erase(m_timers.begin());
00164         }
00165 }