00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
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
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
00379 IDType GetTimerId() const { return m_timerId; }
00380 PAtomicInteger::IntegerType GetNextSerialNumber() { return ++m_serialNumber; }
00381
00382 private:
00383 void Construct();
00384
00385
00386
00387
00388 void StartRunning(
00389 PBoolean once
00390 );
00391
00392
00393
00394
00395
00396 void Process(
00397 PInt64 now
00398 );
00399
00400
00401
00402
00403 PNotifier m_callback;
00404
00405
00406 PTimeInterval m_resetTime;
00407
00408
00409 PBoolean m_oneshot;
00410
00411
00412 enum { Stopped, Running, Paused } m_state;
00413
00414 friend class PTimerList;
00415 class PTimerList * m_timerList;
00416
00417 IDType m_timerId;
00418 PAtomicInteger m_serialNumber;
00419 PInt64 m_absoluteTime;
00420
00421
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