CARMA C++
AbsTimer.h
Go to the documentation of this file.
1 #ifndef ABSTIMER_H
2 #define ABSTIMER_H
3 
11 // C++ includes
12 
13 #include <list>
14 #include <algorithm> // Needed for find_if()
15 #include <functional> // Needed for predicates
16 
17 // C includes
18 
19 #include <unistd.h>
20 #include <signal.h>
21 #include <time.h>
22 
23 #define DEFAULT_CLOCK CLOCK_REALTIME
24 
25 namespace sza {
26  namespace util {
27 
28 #if !HAVE_RT
29 
30  // If we don't have realtime libs, this declaration will prevent
31  // the compiler from complaining about it.
32 
33  #define timer_t unsigned int
34 
35  #define SIGRTMIN 31
36 
37  enum {
38  CLOCK_REALTIME
39  };
40 
41 #endif
42 
52  class AbsTimer {
53 
54  public:
55 
59  class TimerId {
60  public:
61 
65  timer_t* timer_;
66 
70  int sigNo_;
71 
75  TimerId(timer_t* timer, int sigNo) : timer_(timer), sigNo_(sigNo) {}
76  };
77 
82  class TimerId_eq : public std::unary_function<TimerId, bool> {
83  timer_t* timer_;
84  public:
85  explicit TimerId_eq(timer_t* timer) : timer_(timer) {}
86  bool operator() (const TimerId& t) const {return t.timer_ == timer_;}
87  };
88 
93  class TimerSig_eq : public std::unary_function<TimerId, bool> {
94  int sigNo_;
95  public:
96  explicit TimerSig_eq(int sigNo) : sigNo_(sigNo) {}
97  bool operator() (const TimerId& t) const {return t.sigNo_ == sigNo_;}
98  };
99 
104  static std::list<TimerId> timerList_;
105 
111  AbsTimer(int signo, void (*handler)(int));
112 
116  AbsTimer(int signo);
117 
121  ~AbsTimer();
122 
128  unsigned long getResolution();
129 
135  void setInitialDelay(unsigned long sec, unsigned long nsec);
136 
140  void setIntervalDelay(unsigned long sec, unsigned long nsec);
141 
146  void reArm();
147 
151  void start();
152 
158  void stop();
159 
165  bool isRunning();
166 
171  void setIntegral(bool integral);
172 
176  void checkTimer();
177 
178  private:
179 
184  void privateConstructor(int signo, void (*handler)(int));
185 
189  bool isRunning_;
190 
194  bool integral_;
195 
199  bool periodic_;
200 
204  int sigNo_;
205 
209  timer_t timer_;
210 
215  struct sigevent evp_;
216 
220  unsigned long initSec_;
224  unsigned long initNanoSec_;
225 
229  unsigned long intervalSec_;
230 
234  unsigned long intervalNanoSec_;
235 
241  void addTimer(timer_t* timer, int signo);
242 
247  void remTimer(timer_t* timer);
248 
252  bool timerAlreadyExists(int sigNo);
253 
259  void setFutureTime(unsigned long initSec,
260  unsigned long initNanoSec,
261  unsigned long intervalSec,
262  unsigned long intervalNanoSec);
263 
264  }; // End class AbsTimer
265 
266  }; // End namespace util
267 }; // End namespace sza
268 
269 #endif // ABSTIMER_H
A class for storing information about a known Timer.
Definition: AbsTimer.h:59
int sigNo_
Signal associated with this timer.
Definition: AbsTimer.h:70
~AbsTimer()
AbsTimer destructor.
void start()
Start the Timer.
void stop()
Stop the Timer.
unsigned int * timer_
Pointer to the timer managed by this object.
Definition: AbsTimer.h:65
void setIntegral(bool integral)
Set whether this clock should run on integral second boundaries relative to the current time...
static std::list< TimerId > timerList_
A static list of timers.
Definition: AbsTimer.h:104
A predicate for testing if a timer&#39;s signal matches a requested signal.
Definition: AbsTimer.h:93
TimerId(unsigned int *timer, int sigNo)
Constructor for TimerId.
Definition: AbsTimer.h:75
A predicate for testing if a timer&#39;s id matches a requested id.
Definition: AbsTimer.h:82
void checkTimer()
A debugging function, to print out the signals of all know timers.
void reArm()
Re-arm a periodic timer which we want to fire relative to absolute second boundaries.
Class used to set up repetitive or one-shot timers on integral boundries relative to the system clock...
Definition: AbsTimer.h:52
AbsTimer(int signo, void(*handler)(int))
AbsTimer constructor.
void setIntervalDelay(unsigned long sec, unsigned long nsec)
Set interval delay for timer.
unsigned long getResolution()
Returns the resolution of the clock in nsec.
void setInitialDelay(unsigned long sec, unsigned long nsec)
Set initial delay when timer is to start from the moment its start() method is called.
bool isRunning()
Query if this timer is running.