Timer.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #ifndef Timer_h
00020 #define Timer_h
00021 
00022 #include <rtm/RTC.h>
00023 #include <rtm/TimeValue.h>
00024 #include <rtm/Listener.h>
00025 #include <ace/Synch.h>
00026 #include <ace/Task.h>
00027 #include <vector>
00028 
00029 typedef ListenerBase* ListenerId;
00030 
00031 namespace RTC
00032 {
00053   class Timer
00054     : public ACE_Task<ACE_MT_SYNCH>
00055   {
00056   public:
00074     Timer(TimeValue& interval);
00075     
00089     virtual ~Timer();
00090     
00091     //============================================================
00092     // ACE_Task 
00093     //============================================================
00117     virtual int open(void *args);
00118     
00138     virtual int svc(void);
00139     
00140     //============================================================
00141     // public functions
00142     //============================================================
00156     void start();
00157     
00171     void stop();
00172     
00191     void invoke();
00192     
00223     ListenerId registerListener(ListenerBase* listener, TimeValue tm);
00224     
00252     template <class ListenerClass>
00253     ListenerId registerListenerObj(ListenerClass* obj,
00254                                    void (ListenerClass::*cbf)(),
00255                                    TimeValue tm)
00256     {
00257       return registerListener(new ListenerObject<ListenerClass>(obj, cbf), tm);
00258     }
00259     
00284     ListenerId registerListenerFunc(void (*cbf)(), TimeValue tm)
00285     {
00286       return registerListener(new ListenerFunc(cbf), tm);
00287     }
00288     
00312     bool unregisterListener(ListenerId id);
00313     
00314   private:
00315     TimeValue m_interval;
00316     
00317     ACE_Thread_Mutex m_runningMutex;
00318     bool m_running;
00319     
00320     struct Task
00321     {
00322       Task(ListenerBase* l, TimeValue p)
00323         : listener(l), period(p), remains(p)
00324       {
00325       }
00326       ListenerBase* listener;
00327       TimeValue period;
00328       TimeValue remains;
00329     };
00330     
00331     std::vector<Task> m_tasks;
00332     ACE_Thread_Mutex  m_taskMutex;
00333   };
00334 };
00335 #endif // Timer_h
00336 

Generated on Wed May 21 05:17:34 2008 for OpenRTM by  doxygen 1.5.3