Homepage Demos Overview Downloads Tutorials 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 
00008 //! causes a transition if a value (through a pointer) goes above a given value
00009 /*! You will need to specify an event mask which will be listened for.  This event
00010  *  will then be listened for - each time it is received, CompareTrans will check
00011  *  the values for possible activation.
00012  *
00013  *  For example, if you want to transition when the IR sensor goes below, say 200,
00014  *  pass &state->sensors[IRDistOffset], CompareTrans::LT, 200, and
00015  *  EventBase(EventBase::sensorEGID,SensorSourceID::UpdatedSID,EventBase::statusETID)
00016  *  as the polling event.  Or a timer event to just check at a certain interval.
00017  *
00018  *  If you pass a class as the templated type, only requires that < operator is
00019  *  defined for comparing inequality, == for equality, and a copy constructor (CompareTrans
00020  *  holds a protected copy of the value)
00021  *  
00022  *  Passing NULL as the value to monitor will cause a transition on the first event received
00023  */
00024 template<class T>
00025 class CompareTrans : public Transition {
00026 public:
00027   //! use these values to sepecify what kind of comparison should be made to test for activation
00028   enum Test_t {
00029     LT, //!< less than
00030     GT, //!< greater than
00031     LTE, //!< less than or equal
00032     GTE, //!< greater than or equal
00033     EQ, //!< equal
00034     NE //!< not equal
00035   };
00036   
00037   //! constructor, see CompareTrans class notes for information
00038   CompareTrans(StateNode* destination, const T* monitor, Test_t test, const T& value, const EventBase& poll)
00039     : Transition(destination), mon(monitor), tst(test), val(value), poller(poll)
00040   { }
00041   
00042   //!starts listening
00043   virtual void DoStart() { Transition::DoStart(); erouter->addListener(this,poller); }
00044 
00045   //!stops listening
00046   virtual void DoStop() { erouter->forgetListener(this); Transition::DoStop(); }
00047 
00048   //!don't care about the event, just a pulse to check the values
00049   virtual void processEvent(const EventBase&) {
00050     switch(tst) {
00051     case LT:
00052       if(*mon<val) activate();
00053       break;
00054     case GT:
00055       if(val<*mon) activate();
00056       break;
00057     case LTE:
00058       if(!(val<*mon)) activate();
00059       break;
00060     case GTE:
00061       if(!(*mon<val)) activate();
00062       break;
00063     case EQ:
00064       if(*mon==val) activate();
00065       break;
00066     case NE:
00067       if(!(*mon==val)) activate();
00068       break;
00069     }
00070   }
00071 
00072   virtual std::string getName() const { return "CompareTrans"; }
00073 
00074 protected:
00075   const T* mon; //!< address of value to monitor
00076   Test_t tst; //!< test to make
00077   T val; //!< value to compare against
00078   EventBase poller; //!< event to listen to, when it comes, compare the values
00079 
00080 private:
00081   CompareTrans(const CompareTrans& node); //!< don't call this
00082   CompareTrans operator=(const CompareTrans& node); //!< don't call this
00083 };
00084 
00085 /*! @file
00086  * @brief Defines CompareTrans, which causes a transition if a value (through a pointer) goes above a given value
00087  * @author ejt (Creator)
00088  *
00089  * $Author: ejt $
00090  * $Name: tekkotsu-2_0 $
00091  * $Revision: 1.5 $
00092  * $State: Exp $
00093  * $Date: 2003/11/11 00:08:18 $
00094  */
00095 
00096 #endif

Tekkotsu v2.0
Generated Wed Jan 21 03:20:27 2004 by Doxygen 1.3.4