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: 26594 $
00030  * $Author: rjongbloed $
00031  * $Date: 2011-10-13 06:51:30 -0500 (Thu, 13 Oct 2011) $
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     void Stop();
00126 
00129     PTimeInterval GetElapsed() const;
00130 
00133     PTimeInterval GetRemaining() const;
00134 
00137     bool IsRunning() const;
00138 
00141     bool HasExpired() const;
00142 
00145     operator bool() const;
00147 
00148   protected:
00149     PTimeInterval m_startTick;
00150 };
00151 
00152 
00181 class PTimer : public PTimeInterval
00182 {
00183   PCLASSINFO(PTimer, PTimeInterval);
00184 
00185   public:
00186     typedef unsigned IDType;
00187 
00195     PTimer(
00196       long milliseconds = 0,  
00197       int seconds = 0,        
00198       int minutes = 0,        
00199       int hours = 0,          
00200       int days = 0            
00201     );
00202     PTimer(
00203       const PTimeInterval & time    
00204     );
00205     PTimer(
00206       const PTimer & timer    
00207     );
00208 
00215     PTimer & operator=(
00216       DWORD milliseconds            
00217     );
00218     PTimer & operator=(
00219       const PTimeInterval & time    
00220     );
00221     PTimer & operator=(
00222       const PTimer & timer          
00223     );
00224 
00228     virtual ~PTimer();
00230 
00244     virtual void SetInterval(
00245       PInt64 milliseconds = 0,  
00246       long seconds = 0,         
00247       long minutes = 0,         
00248       long hours = 0,           
00249       int days = 0              
00250     );
00251 
00256     void RunContinuous(
00257       const PTimeInterval & time    // New time interval for timer.
00258     );
00259 
00272     void Stop(
00273       bool wait = true  
00274     );
00275 
00282     PBoolean IsRunning() const;
00283 
00288     void Pause();
00289 
00294     void Resume();
00295 
00301     PBoolean IsPaused() const;
00302 
00305     void Reset();
00306 
00309     const PTimeInterval & GetResetTime() const;
00311 
00326     virtual void OnTimeout();
00327 
00334     const PNotifier & GetNotifier() const;
00335 
00339     void SetNotifier(
00340       const PNotifier & func  // New notifier function for the timer.
00341     );
00343 
00358     static PTimeInterval Tick();
00359 
00368     static unsigned Resolution();
00370 
00375     PInt64 GetMilliSeconds() const;
00376 
00379     PInt64 GetAbsoluteTime() const { return m_absoluteTime; }
00381 
00382     // Internal functions.
00383     IDType GetTimerId() const { return m_timerId; }
00384     PAtomicInteger::IntegerType GetNextSerialNumber() { return ++m_serialNumber; }
00385 
00386   private:
00387     void Construct();
00388 
00389     /* Start or restart the timer from the <code>resetTime</code> variable.
00390        This is an internal function.
00391      */
00392     void StartRunning(
00393       PBoolean once   // Flag for one shot or continuous.
00394     );
00395 
00396     /* Process the timer decrementing it by the delta amount and calling the
00397        <code>OnTimeout()</code> when zero. This is used internally by the
00398        <code>PTimerList::Process()</code> function.
00399      */
00400     void Process(
00401       PInt64 now             // time consider as "now"
00402     );
00403 
00404     // Member variables
00405 
00406     // Callback function for expired timers.
00407     PNotifier m_callback;
00408 
00409     // The time to reset a timer to when RunContinuous() is called.
00410     PTimeInterval m_resetTime;
00411 
00412     // Timer operates once then stops.
00413     PBoolean m_oneshot;
00414 
00415     // Timer state.
00416     enum { Stopped, Running, Paused } m_state;
00417 
00418     friend class PTimerList;              // needed for Process
00419     class PTimerList * m_timerList;  
00420 
00421     IDType m_timerId;
00422     PAtomicInteger m_serialNumber;
00423     PInt64 m_absoluteTime;
00424 
00425 // Include platform dependent part of class
00426 #ifdef _WIN32
00427 #include "msos/ptlib/timer.h"
00428 #else
00429 #include "unix/ptlib/timer.h"
00430 #endif
00431 };
00432 
00433 #endif // PTLIB_TIMER_H
00434 
00435 
00436 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Feb 15 20:58:32 2013 for PTLib by  doxygen 1.4.7