OpenRTM  1.0.0
Timer.h
Go to the documentation of this file.
1 // -*- C++ -*-
19 #ifndef Timer_h
20 #define Timer_h
21 
22 #include <coil/TimeValue.h>
23 #include <coil/Listener.h>
24 #include <coil/Mutex.h>
25 #include <coil/Guard.h>
26 #include <coil/Task.h>
27 #include <vector>
28 
30 
31 namespace coil
32 {
53  class Timer
54  : public coil::Task
55  {
56  typedef coil::Mutex Mutex;
57  typedef coil::Guard<Mutex> Guard;
58  public:
76  Timer(TimeValue& interval);
77 
91  virtual ~Timer();
92 
93  //============================================================
94  // ACE_Task
95  //============================================================
119  virtual int open(void *args);
120 
140  virtual int svc(void);
141 
142  //============================================================
143  // public functions
144  //============================================================
158  void start();
159 
173  void stop();
174 
193  void invoke();
194 
226 
254  template <class ListenerClass>
255  ListenerId registerListenerObj(ListenerClass* obj,
256  void (ListenerClass::*cbf)(),
257  TimeValue tm)
258  {
259  return registerListener(new ListenerObject<ListenerClass>(obj, cbf), tm);
260  }
261 
287  {
288  return registerListener(new ListenerFunc(cbf), tm);
289  }
290 
315 
316  private:
317  TimeValue m_interval;
318 
319  Mutex m_runningMutex;
320  bool m_running;
321 
322  struct Task
323  {
325  : listener(l), period(p), remains(p)
326  {
327  }
328  ListenerBase* listener;
329  TimeValue period;
330  TimeValue remains;
331  };
332 
333  std::vector<Task> m_tasks;
334  Mutex m_taskMutex;
335  };
336 };
337 #endif // Timer_h
338