PTLib  Version 2.10.11
timer.h
Go to the documentation of this file.
1 /*
2  * timer.h
3  *
4  * Real time down counting time interval class.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  *
29  * $Revision: 26594 $
30  * $Author: rjongbloed $
31  * $Date: 2011-10-13 06:51:30 -0500 (Thu, 13 Oct 2011) $
32  */
33 
34 #ifndef PTLIB_TIMER_H
35 #define PTLIB_TIMER_H
36 
37 #ifdef P_USE_PRAGMA
38 #pragma interface
39 #endif
40 
41 class PThread;
42 
43 #include <ptlib/notifier.h>
44 
59 {
60  PCLASSINFO(PSimpleTimer, PTimeInterval);
61 
62  public:
69  long milliseconds = 0,
70  int seconds = 0,
71  int minutes = 0,
72  int hours = 0,
73  int days = 0
74  );
76  const PTimeInterval & time
77  );
79  const PSimpleTimer & timer
80  );
81 
89  DWORD milliseconds
90  );
92  const PTimeInterval & time
93  );
95  const PSimpleTimer & timer
96  );
98 
115  virtual void SetInterval(
116  PInt64 milliseconds = 0,
117  long seconds = 0,
118  long minutes = 0,
119  long hours = 0,
120  int days = 0
121  );
122 
125  void Stop();
126 
129  PTimeInterval GetElapsed() const;
130 
133  PTimeInterval GetRemaining() const;
134 
137  bool IsRunning() const;
138 
141  bool HasExpired() const;
142 
145  operator bool() const;
147 
148  protected:
150 };
151 
152 
181 class PTimer : public PTimeInterval
182 {
183  PCLASSINFO(PTimer, PTimeInterval);
184 
185  public:
186  typedef unsigned IDType;
187 
195  PTimer(
196  long milliseconds = 0,
197  int seconds = 0,
198  int minutes = 0,
199  int hours = 0,
200  int days = 0
201  );
202  PTimer(
203  const PTimeInterval & time
204  );
205  PTimer(
206  const PTimer & timer
207  );
208 
215  PTimer & operator=(
216  DWORD milliseconds
217  );
218  PTimer & operator=(
219  const PTimeInterval & time
220  );
221  PTimer & operator=(
222  const PTimer & timer
223  );
224 
228  virtual ~PTimer();
230 
244  virtual void SetInterval(
245  PInt64 milliseconds = 0,
246  long seconds = 0,
247  long minutes = 0,
248  long hours = 0,
249  int days = 0
250  );
251 
256  void RunContinuous(
257  const PTimeInterval & time // New time interval for timer.
258  );
259 
272  void Stop(
273  bool wait = true
274  );
275 
282  PBoolean IsRunning() const;
283 
288  void Pause();
289 
294  void Resume();
295 
301  PBoolean IsPaused() const;
302 
305  void Reset();
306 
309  const PTimeInterval & GetResetTime() const;
311 
326  virtual void OnTimeout();
327 
334  const PNotifier & GetNotifier() const;
335 
339  void SetNotifier(
340  const PNotifier & func // New notifier function for the timer.
341  );
343 
358  static PTimeInterval Tick();
359 
368  static unsigned Resolution();
370 
375  PInt64 GetMilliSeconds() const;
376 
379  PInt64 GetAbsoluteTime() const { return m_absoluteTime; }
381 
382  // Internal functions.
383  IDType GetTimerId() const { return m_timerId; }
384  PAtomicInteger::IntegerType GetNextSerialNumber() { return ++m_serialNumber; }
385 
386  private:
387  void Construct();
388 
389  /* Start or restart the timer from the <code>resetTime</code> variable.
390  This is an internal function.
391  */
392  void StartRunning(
393  PBoolean once // Flag for one shot or continuous.
394  );
395 
396  /* Process the timer decrementing it by the delta amount and calling the
397  <code>OnTimeout()</code> when zero. This is used internally by the
398  <code>PTimerList::Process()</code> function.
399  */
400  void Process(
401  PInt64 now // time consider as "now"
402  );
403 
404  // Member variables
405 
406  // Callback function for expired timers.
407  PNotifier m_callback;
408 
409  // The time to reset a timer to when RunContinuous() is called.
410  PTimeInterval m_resetTime;
411 
412  // Timer operates once then stops.
413  PBoolean m_oneshot;
414 
415  // Timer state.
416  enum { Stopped, Running, Paused } m_state;
417 
418  friend class PTimerList; // needed for Process
419  class PTimerList * m_timerList;
420 
421  IDType m_timerId;
422  PAtomicInteger m_serialNumber;
423  PInt64 m_absoluteTime;
424 
425 // Include platform dependent part of class
426 #ifdef _WIN32
427 #include "msos/ptlib/timer.h"
428 #else
429 #include "unix/ptlib/timer.h"
430 #endif
431 };
432 
433 #endif // PTLIB_TIMER_H
434 
435 
436 // End Of File ///////////////////////////////////////////////////////////////
PAtomicInteger::IntegerType GetNextSerialNumber()
Definition: timer.h:384
PTimer & operator=(DWORD milliseconds)
Restart the timer in one shot mode using the specified time value.
bool HasExpired() const
Indicate timer has expired.
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
Class specialisation for PNotifierTemplate
virtual void SetInterval(PInt64 milliseconds=0, long seconds=0, long minutes=0, long hours=0, int days=0)
Set the value of the time interval.
virtual ~PTimer()
Destroy the timer object, removing it from the applications timer list if it was running.
void Pause()
Pause a running timer.
virtual void OnTimeout()
This function is called on time out.
virtual void SetInterval(PInt64 milliseconds=0, long seconds=0, long minutes=0, long hours=0, int days=0)
Set the value of the time interval.
void Stop()
Stop the timer.
void Stop(bool wait=true)
Stop a running timer.
bool IsRunning() const
Indicate timer has not expired.
PBoolean IsRunning() const
Determine if the timer is currently running.
PTimeInterval m_startTick
Definition: timer.h:149
A class representing a system timer.
Definition: timer.h:181
BOOL PBoolean
Definition: object.h:102
PTimeInterval GetElapsed() const
Return the real time elapsed since instantiation.
Definition: pprocess.h:106
void RunContinuous(const PTimeInterval &time)
Start a timer in continous cycle mode.
This class implements an integer that can be atomically incremented and decremented in a thread-safe ...
Definition: critsec.h:171
PSimpleTimer & operator=(DWORD milliseconds)
Restart the timer using the specified time value.
PTimer(long milliseconds=0, int seconds=0, int minutes=0, int hours=0, int days=0)
Create a new timer object and start it in one shot mode for the specified amount of time...
static PTimeInterval Tick()
Get the number of milliseconds since some arbtrary point in time.
PBoolean IsPaused() const
Determine if the timer is currently paused.
static unsigned Resolution()
Get the smallest number of milliseconds that the timer can be set to.
IDType GetTimerId() const
Definition: timer.h:383
PInt64 GetAbsoluteTime() const
Return absolute time timer will expire.
Definition: timer.h:379
PTimeInterval GetRemaining() const
Return the real time remaining before expiry.
This class defines a thread of execution in the system.
Definition: thread.h:66
PInt64 GetMilliSeconds() const
Return number of milliseconds left in timer.
A class represeting a simple timer.
Definition: timer.h:58
PSimpleTimer(long milliseconds=0, int seconds=0, int minutes=0, int hours=0, int days=0)
Create a new timer object which will be expired the specified time interval after "now" in real time...
PAtomicBase::IntegerType IntegerType
Definition: critsec.h:174
const PNotifier & GetNotifier() const
Get the current call back function that is called whenever the timer expires.
void Resume()
Restart a paused timer continuing at the time it was paused.
void SetNotifier(const PNotifier &func)
Set the call back function that is called whenever the timer expires.
void Reset()
Restart a timer continuing from the time it was initially.
const PTimeInterval & GetResetTime() const
Get the time this timer was set to initially.
unsigned IDType
Definition: timer.h:186