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