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