QTimer.hh

Go to the documentation of this file.
00001 
00002 #ifndef _Q_TIMER_HH_
00003 #define _Q_TIMER_HH_
00004 
00005 #include <sys/types.h>
00006 #include <event.h>
00007 #include <iostream>
00008 
00009 #include "QTimerDispatcher.hh"
00010 
00016 template <class T>
00017 class QTimer
00018 {
00019 public:
00025    typedef void (T::*PtrToMethod)(void);
00026 
00028    QTimer();
00029 
00034    QTimer(unsigned long timeoutMs);
00035    
00037    virtual ~QTimer();
00038 
00040    void Start();
00041    
00043    void Stop();
00044 
00049    void SetTimeout(unsigned long timeoutMs);
00050 
00055    void SetSingleShot(bool singleShot) { fSingleShot = singleShot; }
00056    
00068    void Connect(PtrToMethod mPtr,T* objPtr);
00069 
00070 private:
00072    struct timeval fTimeout;
00073    
00075    struct event fEvent;
00076       
00078    PtrToMethod fMethPtr;
00079    
00081    T* fClassPtr;
00082 
00083    bool fSingleShot;
00084 
00090    bool fEventIsSet;
00091 
00092    void Callback();
00093    static void EventEntryPoint(int fd, short event, void * arg);
00094 };
00095 
00096 //#endif
00097 
00098 
00099 //_____________________________________________________________________________
00100 template <class T>
00101 QTimer<T>::QTimer()
00102 {
00103    //
00104    // SDD FIXME
00105    // ho messo questa chiamata solo perche' ora e' QTD a fare event_init, 
00106    // quindi devo chiamarlo prima di fare qualsiasi cosa con gli eventi.
00107    //
00108    QTimerDispatcher::GetInstance();
00109    fSingleShot = false;
00110    fEventIsSet = false;
00111    SetTimeout(0);
00112 }
00113 
00114 
00115 //_____________________________________________________________________________
00116 template <class T>
00117 QTimer<T>::QTimer(unsigned long timeoutMs)
00118 {
00119    //
00120    // SDD FIXME
00121    // ho messo questa chiamata solo perche' ora e' QTD a fare event_init, 
00122    // quindi devo chiamarlo prima di fare qualsiasi cosa con gli eventi.
00123    //
00124    QTimerDispatcher::GetInstance();
00125    fSingleShot = false;
00126    fEventIsSet = false;
00127    SetTimeout(timeoutMs);
00128 }
00129 
00130 
00131 //_____________________________________________________________________________
00132 template <class T>
00133 QTimer<T>::~QTimer()
00134 {
00135    Stop();
00136 }
00137 
00138 
00139 //_____________________________________________________________________________
00140 template <class T>
00141 void QTimer<T>::Start()
00142 {
00143    evtimer_set(&fEvent,QTimer::EventEntryPoint,this);
00144    evtimer_add(&fEvent,&fTimeout);
00145    fEventIsSet = true;
00146    QTimerDispatcher::GetInstance().AddTimer();
00147 }
00148 
00149 
00150 //_____________________________________________________________________________
00151 template <class T>
00152 void QTimer<T>::Stop()
00153 {
00154    if(fEventIsSet)
00155       evtimer_del(&fEvent);
00156 }
00157 
00158 
00159 //_____________________________________________________________________________
00160 template <class T>
00161 void QTimer<T>::SetTimeout(unsigned long timeoutMs)
00162 {
00163    fTimeout.tv_sec = timeoutMs / 1000;
00164    fTimeout.tv_usec = (timeoutMs % 1000) * 1000;
00165 }
00166 
00167 
00168 //_____________________________________________________________________________
00169 template <class T>
00170 void QTimer<T>::EventEntryPoint(int fd, short event, void * ptr)
00171 {
00172    QTimer* timer = (QTimer*)ptr;
00173    timer->Callback();
00174 }
00175 
00176 
00177 //_____________________________________________________________________________
00178 template <class T>
00179 void QTimer<T>::Callback()
00180 {
00181    try
00182    {
00183       (fClassPtr->*fMethPtr)();
00184       if(!fSingleShot) evtimer_add(&fEvent,&fTimeout);
00185    }
00186    catch(QError& err)
00187    {
00188       QTimerDispatcher::GetInstance().SetError(err);
00189    }
00190 }
00191 
00192 
00193 //_____________________________________________________________________________
00194 template <class T>
00195 void QTimer<T>::Connect(PtrToMethod mPtr,T* objPtr)
00196 {
00197    fMethPtr = mPtr;
00198    fClassPtr = objPtr;
00199 }
00200 
00201 #endif

Generated on Tue Nov 16 10:49:55 2010 for CUORE Software by  doxygen 1.5.6