00001
00019 #ifndef Timer_h
00020 #define Timer_h
00021
00022 #include <coil/TimeValue.h>
00023 #include <coil/Listener.h>
00024 #include <coil/Mutex.h>
00025 #include <coil/Guard.h>
00026 #include <coil/Task.h>
00027 #include <vector>
00028
00029 typedef ListenerBase* ListenerId;
00030
00031 namespace coil
00032 {
00053 class Timer
00054 : public coil::Task
00055 {
00056 typedef coil::Mutex Mutex;
00057 typedef coil::Guard<Mutex> Guard;
00058 public:
00076 Timer(TimeValue& interval);
00077
00091 virtual ~Timer();
00092
00093
00094
00095
00119 virtual int open(void *args);
00120
00140 virtual int svc(void);
00141
00142
00143
00144
00158 void start();
00159
00173 void stop();
00174
00193 void invoke();
00194
00225 ListenerId registerListener(ListenerBase* listener, TimeValue tm);
00226
00254 template <class ListenerClass>
00255 ListenerId registerListenerObj(ListenerClass* obj,
00256 void (ListenerClass::*cbf)(),
00257 TimeValue tm)
00258 {
00259 return registerListener(new ListenerObject<ListenerClass>(obj, cbf), tm);
00260 }
00261
00286 ListenerId registerListenerFunc(void (*cbf)(), TimeValue tm)
00287 {
00288 return registerListener(new ListenerFunc(cbf), tm);
00289 }
00290
00314 bool unregisterListener(ListenerId id);
00315
00316 private:
00317 TimeValue m_interval;
00318
00319 Mutex m_runningMutex;
00320 bool m_running;
00321
00322 struct Task
00323 {
00324 Task(ListenerBase* l, TimeValue p)
00325 : listener(l), period(p), remains(p)
00326 {
00327 }
00328 ListenerBase* listener;
00329 TimeValue period;
00330 TimeValue remains;
00331 };
00332
00333 std::vector<Task> m_tasks;
00334 Mutex m_taskMutex;
00335 };
00336 };
00337 #endif // Timer_h
00338