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  * $Log: timer.h,v $
00030  * Revision 1.29  2005/11/25 03:43:47  csoutheren
00031  * Fixed function argument comments to be compatible with Doxygen
00032  *
00033  * Revision 1.28  2005/06/02 19:25:18  dsandras
00034  * Applied patch from Miguel Rodríguez Pérez <miguelrp  @  gmail.com> (migras) to fix compilation with gcc 4.0.1.
00035  *
00036  * Revision 1.27  2003/09/17 09:01:00  csoutheren
00037  * Moved PSmartPointer and PNotifier into seperate files
00038  * Added detection for system regex libraries on all platforms
00039  *
00040  * Revision 1.26  2003/09/17 05:41:59  csoutheren
00041  * Removed recursive includes
00042  *
00043  * Revision 1.25  2003/09/17 01:18:02  csoutheren
00044  * Removed recursive include file system and removed all references
00045  * to deprecated coooperative threading support
00046  *
00047  * Revision 1.24  2002/09/16 01:08:59  robertj
00048  * Added #define so can select if #pragma interface/implementation is used on
00049  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00050  *
00051  * Revision 1.23  2002/05/28 13:05:26  robertj
00052  * Fixed PTimer::SetInterval so it restarts timer as per operator=()
00053  *
00054  * Revision 1.22  2002/04/09 00:09:10  robertj
00055  * Improved documentation on PTimer usage.
00056  *
00057  * Revision 1.21  2001/11/14 06:06:26  robertj
00058  * Added functions on PTimer to get reset value and restart timer to it.
00059  *
00060  * Revision 1.20  2001/05/22 12:49:32  robertj
00061  * Did some seriously wierd rewrite of platform headers to eliminate the
00062  *   stupid GNU compiler warning about braces not matching.
00063  *
00064  * Revision 1.19  2000/08/30 03:16:59  robertj
00065  * Improved multithreaded reliability of the timers under stress.
00066  *
00067  * Revision 1.18  2000/01/06 14:09:42  robertj
00068  * Fixed problems with starting up timers,losing up to 10 seconds
00069  *
00070  * Revision 1.17  1999/03/09 02:59:51  robertj
00071  * Changed comments to doc++ compatible documentation.
00072  *
00073  * Revision 1.16  1999/02/16 08:11:17  robertj
00074  * MSVC 6.0 compatibility changes.
00075  *
00076  * Revision 1.15  1998/09/23 06:21:45  robertj
00077  * Added open source copyright license.
00078  *
00079  * Revision 1.14  1996/12/21 07:57:22  robertj
00080  * Fixed possible deadlock in timers.
00081  *
00082  * Revision 1.13  1996/05/18 09:18:37  robertj
00083  * Added mutex to timer list.
00084  *
00085  * Revision 1.12  1995/06/17 11:13:36  robertj
00086  * Documentation update.
00087  *
00088  * Revision 1.11  1995/04/02 09:27:34  robertj
00089  * Added "balloon" help.
00090  *
00091  * Revision 1.10  1995/03/14 12:42:51  robertj
00092  * Updated documentation to use HTML codes.
00093  *
00094  * Revision 1.9  1995/01/18  09:01:06  robertj
00095  * Added notifiers to timers.
00096  * Documentation.
00097  *
00098  * Revision 1.8  1994/08/23  11:32:52  robertj
00099  * Oops
00100  *
00101  * Revision 1.7  1994/08/22  00:46:48  robertj
00102  * Added pragma fro GNU C++ compiler.
00103  *
00104  * Revision 1.6  1994/07/02  03:03:49  robertj
00105  * Redesign of timers.
00106  *
00107  * Revision 1.5  1994/06/25  11:55:15  robertj
00108  * Unix version synchronisation.
00109  *
00110  * Revision 1.4  1994/03/07  07:38:19  robertj
00111  * Major enhancementsacross the board.
00112  *
00113  * Revision 1.3  1994/01/03  04:42:23  robertj
00114  * Mass changes to common container classes and interactors etc etc etc.
00115  *
00116  * Revision 1.2  1993/08/31  03:38:02  robertj
00117  * Added missing virtual on destructor.
00118  *
00119  * Revision 1.1  1993/08/27  18:17:47  robertj
00120  * Initial revision
00121  *
00122  */
00123 
00124 #ifndef _PTIMER
00125 #define _PTIMER
00126 
00127 #ifdef P_USE_PRAGMA
00128 #pragma interface
00129 #endif
00130 
00131 class PThread;
00132 
00133 #include <ptlib/notifier.h>
00134 
00163 class PTimer : public PTimeInterval
00164 {
00165   PCLASSINFO(PTimer, PTimeInterval);
00166 
00167   public:
00175     PTimer(
00176       long milliseconds = 0,  
00177       int seconds = 0,        
00178       int minutes = 0,        
00179       int hours = 0,          
00180       int days = 0            
00181     );
00182     PTimer(
00183       const PTimeInterval & time    
00184     );
00185 
00192     PTimer & operator=(
00193       DWORD milliseconds            
00194     );
00195     PTimer & operator=(
00196       const PTimeInterval & time    
00197     );
00198 
00202     virtual ~PTimer();
00204 
00218     virtual void SetInterval(
00219       PInt64 milliseconds = 0,  
00220       long seconds = 0,         
00221       long minutes = 0,         
00222       long hours = 0,           
00223       int days = 0              
00224     );
00225 
00230     void RunContinuous(
00231       const PTimeInterval & time    // New time interval for timer.
00232     );
00233 
00238     void Stop();
00239 
00246     BOOL IsRunning() const;
00247 
00252     void Pause();
00253 
00258     void Resume();
00259 
00265     BOOL IsPaused() const;
00266 
00269     void Reset();
00270 
00273     const PTimeInterval & GetResetTime() const;
00275 
00290     virtual void OnTimeout();
00291 
00298     const PNotifier & GetNotifier() const;
00299 
00303     void SetNotifier(
00304       const PNotifier & func  // New notifier function for the timer.
00305     );
00307 
00322     static PTimeInterval Tick();
00323 
00332     static unsigned Resolution();
00334 
00335   private:
00336     void Construct();
00337 
00338     /* Start or restart the timer from the #resetTime# variable.
00339        This is an internal function.
00340      */
00341     void StartRunning(
00342       BOOL once   // Flag for one shot or continuous.
00343     );
00344 
00345     /* Process the timer decrementing it by the delta amount and calling the
00346        #OnTimeout()# when zero. This is used internally by the
00347        #PTimerList::Process()# function.
00348      */
00349     void Process(
00350       const PTimeInterval & delta,    // Time interval since last call.
00351       PTimeInterval & minTimeLeft     // Minimum time left till next timeout.
00352     );
00353 
00354   // Member variables
00355     PNotifier callback;
00356     // Callback function for expired timers.
00357 
00358     PTimeInterval resetTime;
00359     // The time to reset a timer to when RunContinuous() is called.
00360 
00361     BOOL oneshot;
00362     // Timer operates once then stops.
00363 
00364     enum { Stopped, Starting, Running, Paused } state;
00365     // Timer state.
00366 
00367 
00368   friend class PTimerList;
00369     class PTimerList * timerList;
00370 
00371 
00372 // Include platform dependent part of class
00373 #ifdef _WIN32
00374 #include "msos/ptlib/timer.h"
00375 #else
00376 #include "unix/ptlib/timer.h"
00377 #endif
00378 };
00379 
00380 #endif
00381 
00382 
00383 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Mar 7 06:25:03 2008 for PTLib by  doxygen 1.5.1