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: 24758 $
00030  * $Author: rjongbloed $
00031  * $Date: 2010-09-28 04:05:11 -0500 (Tue, 28 Sep 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 
00058 class PSimpleTimer : public PTimeInterval
00059 {
00060   PCLASSINFO(PSimpleTimer, PTimeInterval);
00061 
00062   public:
00068     PSimpleTimer(
00069       long milliseconds = 0,  
00070       int seconds = 0,        
00071       int minutes = 0,        
00072       int hours = 0,          
00073       int days = 0            
00074     );
00075     PSimpleTimer(
00076       const PTimeInterval & time    
00077     );
00078     PSimpleTimer(
00079       const PSimpleTimer & timer    
00080     );
00081 
00088     PSimpleTimer & operator=(
00089       DWORD milliseconds            
00090     );
00091     PSimpleTimer & operator=(
00092       const PTimeInterval & time    
00093     );
00094     PSimpleTimer & operator=(
00095       const PSimpleTimer & timer          
00096     );
00098 
00115     virtual void SetInterval(
00116       PInt64 milliseconds = 0,  
00117       long seconds = 0,         
00118       long minutes = 0,         
00119       long hours = 0,           
00120       int days = 0              
00121     );
00122 
00125     PTimeInterval GetElapsed() const;
00126 
00129     PTimeInterval GetRemaining() const;
00130 
00133     bool IsRunning() const;
00134 
00137     bool HasExpired() const;
00138 
00141     operator bool() const;
00143 
00144   protected:
00145     PTimeInterval m_startTick;
00146 };
00147 
00148 
00177 class PTimer : public PTimeInterval
00178 {
00179   PCLASSINFO(PTimer, PTimeInterval);
00180 
00181   public:
00182     typedef unsigned IDType;
00183 
00191     PTimer(
00192       long milliseconds = 0,  
00193       int seconds = 0,        
00194       int minutes = 0,        
00195       int hours = 0,          
00196       int days = 0            
00197     );
00198     PTimer(
00199       const PTimeInterval & time    
00200     );
00201     PTimer(
00202       const PTimer & timer    
00203     );
00204 
00211     PTimer & operator=(
00212       DWORD milliseconds            
00213     );
00214     PTimer & operator=(
00215       const PTimeInterval & time    
00216     );
00217     PTimer & operator=(
00218       const PTimer & timer          
00219     );
00220 
00224     virtual ~PTimer();
00226 
00240     virtual void SetInterval(
00241       PInt64 milliseconds = 0,  
00242       long seconds = 0,         
00243       long minutes = 0,         
00244       long hours = 0,           
00245       int days = 0              
00246     );
00247 
00252     void RunContinuous(
00253       const PTimeInterval & time    // New time interval for timer.
00254     );
00255 
00268     void Stop(
00269       bool wait = true  
00270     );
00271 
00278     PBoolean IsRunning() const;
00279 
00284     void Pause();
00285 
00290     void Resume();
00291 
00297     PBoolean IsPaused() const;
00298 
00301     void Reset();
00302 
00305     const PTimeInterval & GetResetTime() const;
00307 
00322     virtual void OnTimeout();
00323 
00330     const PNotifier & GetNotifier() const;
00331 
00335     void SetNotifier(
00336       const PNotifier & func  // New notifier function for the timer.
00337     );
00339 
00354     static PTimeInterval Tick();
00355 
00364     static unsigned Resolution();
00366 
00371     PInt64 GetMilliSeconds() const;
00372 
00375     PInt64 GetAbsoluteTime() const { return m_absoluteTime; }
00377 
00378     // Internal functions.
00379     IDType GetTimerId() const { return m_timerId; }
00380     PAtomicInteger::IntegerType GetNextSerialNumber() { return ++m_serialNumber; }
00381 
00382   private:
00383     void Construct();
00384 
00385     /* Start or restart the timer from the <code>resetTime</code> variable.
00386        This is an internal function.
00387      */
00388     void StartRunning(
00389       PBoolean once   // Flag for one shot or continuous.
00390     );
00391 
00392     /* Process the timer decrementing it by the delta amount and calling the
00393        <code>OnTimeout()</code> when zero. This is used internally by the
00394        <code>PTimerList::Process()</code> function.
00395      */
00396     void Process(
00397       PInt64 now             // time consider as "now"
00398     );
00399 
00400     // Member variables
00401 
00402     // Callback function for expired timers.
00403     PNotifier m_callback;
00404 
00405     // The time to reset a timer to when RunContinuous() is called.
00406     PTimeInterval m_resetTime;
00407 
00408     // Timer operates once then stops.
00409     PBoolean m_oneshot;
00410 
00411     // Timer state.
00412     enum { Stopped, Running, Paused } m_state;
00413 
00414     friend class PTimerList;              // needed for Process
00415     class PTimerList * m_timerList;  
00416 
00417     IDType m_timerId;
00418     PAtomicInteger m_serialNumber;
00419     PInt64 m_absoluteTime;
00420 
00421 // Include platform dependent part of class
00422 #ifdef _WIN32
00423 #include "msos/ptlib/timer.h"
00424 #else
00425 #include "unix/ptlib/timer.h"
00426 #endif
00427 };
00428 
00429 #endif // PTLIB_TIMER_H
00430 
00431 
00432 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Oct 14 01:44:10 2011 for PTLib by  doxygen 1.4.7