Timer.h

00001 #ifndef TIMER_H
00002 #define TIMER_H 1
00003 
00004 #include <omnetpp.h>
00005 
00006 #include "TimerCore.h"
00007 
00008 /* Usage:
00009  * Your class needs to be a subclass of Timer (as well as BaseWhatever),
00010  * call Timer::init(self) before doing anything, and override handleTimer()
00011  * to do whatever things need doing when timers fire.
00012  *
00013  * The Timer module will also auto-cleanup all leftover timers at the end of 
00014  * simulation, but deleteTimer can be used to reclaim memory.
00015  *
00016  * Example (for a localisation module extending BaseLocalisation):
00017  *
00018  * class MyClass: public BaseLocalisation, public Timer
00019  * {
00020  *     public:
00021  *      //Module_Class_Members(MyClass, BaseLocalisation, 0)
00022  *      void initialize();
00023  *      void handleTimer(unsigned int index);
00024  * }
00025  *
00026  * void MyClass::initialize()
00027  * {
00028  *      Timer::init(self);
00029  *      setTimer(1.0);
00030  * }
00031  *
00032  * void MyClass::handleTimer(unsigned int index)
00033  * {
00034  *    // Do timer stuff, which will happen 
00035  *      // 1.0s after startup (see the setTimer above)
00036  * }
00037  */
00038 
00039 class Timer
00040 {
00041   friend class TimerCore;
00042   protected:
00043     TimerCore *ct;
00044     cModule *owner;
00045 
00046   public:
00047       Timer(){ct = NULL;owner=NULL;}
00048     virtual ~Timer(){delete ct;}
00049   
00054   virtual void init(cModule *parent);
00055 
00061   void setTimer(unsigned int index, simtime_t when){checkCT();ct->setTimer(index,when);}
00062 
00069   unsigned int setTimer(simtime_t when){checkCT();return ct->setTimer(when);}
00070 
00076   void cancelTimer(unsigned int index){checkCT();ct->cancelTimer(index);}
00077 
00082   float remainingTimer(unsigned int index) {checkCT();return ct->remainingTimer(index);}
00083 
00084   /* Timer "fire" handler routine. Needs to be overriden by classes that want to use Timer
00085    * @param index The timer number for the timer that has just completed
00086    */
00087   virtual void handleTimer(unsigned int index)=0;
00088 
00093   void setContextPointer(unsigned int index,void * data) {checkCT();ct->setContextPointer(index,data);}
00094 
00099   void * contextPointer(unsigned int index) {checkCT();return ct->contextPointer(index);}
00100 
00106   void setContextDestructor(unsigned int index, void (*func)(void * data)){checkCT();ct->setContextDestructor(index,func);}
00107 
00108   /* Mark the first @count pointer ids (from 0 to @count-1) as allocated, so they don't get
00109    * auto-allocated by setTimer
00110    * @param count Number of timers to allocate
00111    */
00112   void allocateTimers(unsigned int count) {checkCT();ct->allocateTimers(count);}
00113 
00114   /* Delete a timer. Useful for auto-allocated timers that you don't need any more to 
00115    * reduce memory usage. Does nothing if the timer doesn't exist, and works even if 
00116    * a timer is still running.
00117    *
00118    * This does not need to be called for every timer, as the Timer module will automatically cleanup
00119    * all of the remaining timers at the end of the simulation. It is a tool for reclaiming memory 
00120    * early for simulations that might need many timers, and would otherwise fill up all the free 
00121    * memory with old timers.
00122    * @param index Timer to wipe
00123    */
00124   void deleteTimer(unsigned int index) {checkCT();ct->deleteTimer(index);}
00125 
00126 private: 
00127   /* checkCT is an internal check function to test if init() has been called */
00128   void checkCT() 
00129   {
00130     if (ct == NULL)
00131       opp_error("init() must be called before using Timer functions");
00132   }
00133 
00134 };
00135 
00136 #endif