Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

TimeOutTrans.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_TimeOutTrans_h_
00003 #define INCLUDED_TimeOutTrans_h_
00004 
00005 #include "Behaviors/Transition.h"
00006 #include "Events/EventRouter.h"
00007 #include "Shared/MarkScope.h"
00008 
00009 //! causes a transition after a specified amount of time has passed
00010 /*! If any event parameters are specified, this transition will listen
00011  *  for matching events, and if any are received, it will reset the
00012  *  timer */
00013 class TimeOutTrans : public Transition {
00014 public:
00015   //! constructor, specify delay in milliseconds
00016   TimeOutTrans(StateNode* destination, unsigned int delay)
00017     : Transition(destination), d(delay),
00018       eventargcount(0), egid(EventBase::unknownEGID), esid(0), etid(EventBase::statusETID) {}
00019 
00020   //! constructor, specify delay in milliseconds
00021   TimeOutTrans(std::string const &name,  StateNode* destination, unsigned int delay)
00022     : Transition(name,destination), d(delay),
00023       eventargcount(0), egid(EventBase::unknownEGID), esid(0), etid(EventBase::statusETID) {}
00024 
00025   //! constructor, specify delay in milliseconds, if any events matching given parameters are received, the timer will be reset
00026   TimeOutTrans(StateNode* destination, unsigned int delay, EventBase::EventGeneratorID_t gid)
00027     : Transition(destination), d(delay),
00028       eventargcount(1), egid(gid), esid(0), etid(EventBase::statusETID) {}
00029 
00030   //! constructor, specify delay in milliseconds, if any events matching given parameters are received, the timer will be reset
00031   TimeOutTrans(StateNode* destination, unsigned int delay, EventBase::EventGeneratorID_t gid, unsigned int sid)
00032     : Transition(destination), d(delay),
00033       eventargcount(2), egid(gid), esid(sid), etid(EventBase::statusETID) {}
00034 
00035   //! constructor, specify delay in milliseconds, if any events matching given parameters are received, the timer will be reset
00036   TimeOutTrans(StateNode* destination, unsigned int delay, EventBase::EventGeneratorID_t gid, unsigned int sid, EventBase::EventTypeID_t tid)
00037     : Transition(destination), d(delay),
00038       eventargcount(3), egid(gid), esid(sid), etid(tid) {}
00039 
00040   //!starts timer
00041   virtual void postStart() {
00042     Transition::postStart();
00043     switch (eventargcount) {
00044     case 1: erouter->addListener(this,egid); break;
00045     case 2: erouter->addListener(this,egid,esid); break;
00046     case 3: erouter->addListener(this,egid,esid,etid); break;
00047     };
00048     resetTimer(); 
00049   }
00050 
00051   //!resets timer
00052   void resetTimer() {
00053     // std::cout << "Reset @ " << get_time() << " stop @ " << get_time()+d << ' ' << this << std::endl;
00054     erouter->addTimer(this,0,d,false);
00055   }
00056 
00057   //!if we receive the timer event, fire()
00058   virtual void doEvent() {
00059     // std::cout << "Timeout @ " << get_time() << " from " << event.getName() << ' ' << this << std::endl;
00060     if(event->getGeneratorID()==EventBase::timerEGID)
00061       fire();
00062     else
00063       resetTimer();
00064   }
00065 
00066 protected:
00067   //!amount to delay (in milliseconds) before transition
00068   unsigned int d;
00069 
00070   //!level of specificity of events to listen for
00071   unsigned int eventargcount;
00072 
00073   EventBase::EventGeneratorID_t egid; //!< the event generator to listen for
00074   unsigned int esid; //!< the source to listen for
00075   EventBase::EventTypeID_t etid; //!< the type to listen for
00076 
00077 };
00078 
00079 /*! @file
00080  * @brief Defines TimeOutTrans, which causes a transition after a specified amount of time has passed
00081  * @author ejt (Creator)
00082  */
00083 
00084 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:52 2016 by Doxygen 1.6.3