timer.h

Go to the documentation of this file.
00001 /*
00002  * timer.h
00003  *
00004  * Real time down counting time interval class.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 24177 $
00030  * $Author: rjongbloed $
00031  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
00032  */
00033 
00034 #ifndef PTLIB_TIMER_H
00035 #define PTLIB_TIMER_H
00036 
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040 
00041 class PThread;
00042 
00043 #include <ptlib/notifier.h>
00044 
00073 class PTimer : public PTimeInterval
00074 {
00075   PCLASSINFO(PTimer, PTimeInterval);
00076 
00077   public:
00078     typedef unsigned IDType;
00079 
00087     PTimer(
00088       long milliseconds = 0,  
00089       int seconds = 0,        
00090       int minutes = 0,        
00091       int hours = 0,          
00092       int days = 0            
00093     );
00094     PTimer(
00095       const PTimeInterval & time    
00096     );
00097     PTimer(
00098       const PTimer & timer    
00099     );
00100 
00107     PTimer & operator=(
00108       DWORD milliseconds            
00109     );
00110     PTimer & operator=(
00111       const PTimeInterval & time    
00112     );
00113     PTimer & operator=(
00114       const PTimer & timer          
00115     );
00116 
00120     virtual ~PTimer();
00122 
00136     virtual void SetInterval(
00137       PInt64 milliseconds = 0,  
00138       long seconds = 0,         
00139       long minutes = 0,         
00140       long hours = 0,           
00141       int days = 0              
00142     );
00143 
00148     void RunContinuous(
00149       const PTimeInterval & time    // New time interval for timer.
00150     );
00151 
00164     void Stop(
00165       bool wait = true  
00166     );
00167 
00174     PBoolean IsRunning() const;
00175 
00180     void Pause();
00181 
00186     void Resume();
00187 
00193     PBoolean IsPaused() const;
00194 
00197     void Reset();
00198 
00201     const PTimeInterval & GetResetTime() const;
00203 
00218     virtual void OnTimeout();
00219 
00226     const PNotifier & GetNotifier() const;
00227 
00231     void SetNotifier(
00232       const PNotifier & func  // New notifier function for the timer.
00233     );
00235 
00250     static PTimeInterval Tick();
00251 
00260     static unsigned Resolution();
00262 
00267     PInt64 GetMilliSeconds() const;
00268 
00271     PInt64 GetAbsoluteTime() const { return m_absoluteTime; }
00273 
00274     // Internal functions.
00275     IDType GetTimerId() const { return m_timerId; }
00276     PAtomicInteger::IntegerType GetNextSerialNumber() { return ++m_serialNumber; }
00277 
00278   private:
00279     void Construct();
00280 
00281     /* Start or restart the timer from the <code>resetTime</code> variable.
00282        This is an internal function.
00283      */
00284     void StartRunning(
00285       PBoolean once   // Flag for one shot or continuous.
00286     );
00287 
00288     /* Process the timer decrementing it by the delta amount and calling the
00289        <code>OnTimeout()</code> when zero. This is used internally by the
00290        <code>PTimerList::Process()</code> function.
00291      */
00292     void Process(
00293       PInt64 now             // time consider as "now"
00294     );
00295 
00296     // Member variables
00297 
00298     // Callback function for expired timers.
00299     PNotifier m_callback;
00300 
00301     // The time to reset a timer to when RunContinuous() is called.
00302     PTimeInterval m_resetTime;
00303 
00304     // Timer operates once then stops.
00305     PBoolean m_oneshot;
00306 
00307     // Timer state.
00308     enum { Stopped, Running, Paused } m_state;
00309 
00310     friend class PTimerList;              // needed for Process
00311     class PTimerList * m_timerList;  
00312 
00313     IDType m_timerId;
00314     PAtomicInteger m_serialNumber;
00315     PInt64 m_absoluteTime;
00316 
00317 // Include platform dependent part of class
00318 #ifdef _WIN32
00319 #include "msos/ptlib/timer.h"
00320 #else
00321 #include "unix/ptlib/timer.h"
00322 #endif
00323 };
00324 
00325 #endif // PTLIB_TIMER_H
00326 
00327 
00328 // End Of File ///////////////////////////////////////////////////////////////

Generated on Thu May 27 01:49:40 2010 for PTLib by  doxygen 1.4.7