libcommonc++  0.7
IntervalTimer.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_IntervalTimer_hxx
24 #define __ccxx_IntervalTimer_hxx
25 
26 #include <commonc++/Common.h++>
28 
29 #ifdef CCXX_OS_POSIX
30 #include <signal.h>
31 #include <time.h>
32 #endif
33 
34 namespace ccxx {
35 
47 {
48  public:
49 
51  virtual ~IntervalTimer();
52 
58  void start();
59 
61  void stop();
62 
64  inline uint_t getInitialDelay() const
65  { return(_initialDelay); }
66 
71  inline void setInitialDelay(uint_t initialDelay)
72  { _initialDelay = initialDelay; }
73 
75  inline uint_t getInterval() const
76  { return(_interval); }
77 
82  inline void setInterval(uint_t interval)
83  { _interval = interval; }
84 
86  inline bool isRunning() const
87  { return(_running); }
88 
89  protected:
90 
100  IntervalTimer(uint_t initialDelay, uint_t interval = 0);
101 
103  virtual void fired() = 0;
104 
105  private:
106 
107 #ifdef CCXX_OS_WINDOWS
108  static void CALLBACK _callback(LPVOID arg, DWORD timerLowValue,
109  DWORD timerHighValue);
110 #else
111  static void _callback(union sigval data);
112 #endif
113 
114  uint_t _initialDelay;
115  uint_t _interval;
116  bool _running;
117 
118 #if defined(CCXX_OS_WINDOWS)
119  HANDLE _timer;
120 #elif ! defined(CCXX_OS_MACOSX)
121  timer_t _timer;
122  struct sigevent _event;
123 #endif
124 };
125 
126 } // namespace ccxx
127 
128 #endif // __ccxx_IntervalTimer_hxx
bool isRunning() const
Determine if the timer is currently running.
Definition: IntervalTimer.h++:86
uint_t getInterval() const
Get the interval, in milliseconds.
Definition: IntervalTimer.h++:75
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
void setInterval(uint_t interval)
Set the interval, in milliseconds.
Definition: IntervalTimer.h++:82
uint_t getInitialDelay() const
Get the initial delay, in milliseconds.
Definition: IntervalTimer.h++:64
void setInitialDelay(uint_t initialDelay)
Set the initial delay, in milliseconds.
Definition: IntervalTimer.h++:71
#define COMMONCPP_API
Definition: Common.h++:126
A high-precision interval timer that does not require blocking or polling from a dedicated thread...
Definition: IntervalTimer.h++:46
Definition: AllocationMap.c++:25