PTLib  Version 2.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 ///////////////////////////////////////////////////////////////