Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

CompareTrans.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_CompareTrans_h_
00003 #define INCLUDED_CompareTrans_h_
00004 
00005 #include "Behaviors/Transition.h"
00006 #include "Events/EventRouter.h"
00007 #include "Shared/MarkScope.h"
00008 
00009 //! causes a transition if a value (through a pointer) goes above a given value
00010 /*! You will need to specify an event mask which will be listened for.  This event
00011  *  will then be listened for - each time it is received, CompareTrans will check
00012  *  the values for possible activation.
00013  *
00014  *  For example, if you want to transition when the IR sensor goes below, say 200,
00015  *  pass &state->sensors[IRDistOffset], CompareTrans::LT, 200, and
00016  *  EventBase(EventBase::sensorEGID,SensorSrcID::UpdatedSID,EventBase::statusETID)
00017  *  as the polling event.  Or a timer event to just check at a certain interval.
00018  *
00019  *  If you pass a class as the templated type, only requires that < operator is
00020  *  defined for comparing inequality, == for equality, and a copy constructor (CompareTrans
00021  *  holds a protected copy of the value)
00022  *  
00023  *  Passing NULL as the value to monitor will cause a transition on the first event received
00024  */
00025 template<class T>
00026 class CompareTrans : public Transition {
00027 public:
00028   //! use these values to sepecify what kind of comparison should be made to test for activation
00029   enum Test_t {
00030     LT, //!< less than
00031     GT, //!< greater than
00032     LTE, //!< less than or equal
00033     GTE, //!< greater than or equal
00034     EQ, //!< equal
00035     NE //!< not equal
00036   };
00037   
00038   //! constructor, only checks @a monitor when it is first activated (no polling)
00039   CompareTrans(StateNode* destination, const T* monitor, Test_t test, const T& value)
00040     : Transition(destination), mon(monitor), tst(test), val(value), isPolling(false), poller()
00041   { }
00042   
00043   //! constructor, only checks @a monitor when it is first activated (no polling)
00044   CompareTrans(const std::string& name, StateNode* destination, const T* monitor, Test_t test, const T& value)
00045     : Transition(name,destination), mon(monitor), tst(test), val(value), isPolling(false), poller()
00046   { }
00047   
00048   //! constructor, see CompareTrans class notes for information
00049   CompareTrans(StateNode* destination, const T* monitor, Test_t test, const T& value, const EventBase& poll)
00050     : Transition(destination), mon(monitor), tst(test), val(value), isPolling(true), poller(poll)
00051   { }
00052   
00053   //! constructor, see CompareTrans class notes for information
00054   CompareTrans(const std::string& name, StateNode* destination, const T* monitor, Test_t test, const T& value, const EventBase& poll)
00055     : Transition(name,destination), mon(monitor), tst(test), val(value), isPolling(true), poller(poll)
00056   { }
00057   
00058   //!starts listening
00059   virtual void postStart() {
00060     Transition::postStart();
00061     if(isPolling)
00062       erouter->addListener(this,poller);
00063     else
00064       doEvent();
00065   }
00066 
00067   //!don't care about the event, just a pulse to check the values
00068   virtual void doEvent() {
00069     switch(tst) {
00070     case LT:
00071       if(*mon<val) fire();
00072       break;
00073     case GT:
00074       if(val<*mon) fire();
00075       break;
00076     case LTE:
00077       if(!(val<*mon)) fire();
00078       break;
00079     case GTE:
00080       if(!(*mon<val)) fire();
00081       break;
00082     case EQ:
00083       if(*mon==val) fire();
00084       break;
00085     case NE:
00086       if(!(*mon==val)) fire();
00087       break;
00088     }
00089   }
00090 
00091 protected:
00092   const T* mon; //!< address of value to monitor
00093   Test_t tst; //!< test to make
00094   T val; //!< value to compare against
00095   bool isPolling; //!< set to true if #poller should be used (otherwise only checked on activation)
00096   EventBase poller; //!< event to listen to, when it comes, compare the values
00097 
00098 private:
00099   CompareTrans(const CompareTrans& node); //!< don't call this
00100   CompareTrans operator=(const CompareTrans& node); //!< don't call this
00101 };
00102 
00103 /*! @file
00104  * @brief Defines CompareTrans, which causes a transition if a value (through a pointer) goes above a given value
00105  * @author ejt (Creator)
00106  */
00107 
00108 #endif

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