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("CompareTrans",destination), mon(monitor), tst(test), val(value), poller(poll)
00040   { }
00041   
00042   //! constructor, see CompareTrans class notes for information
00043   CompareTrans(const std::string& name, StateNode* destination, const T* monitor, Test_t test, const T& value, const EventBase& poll)
00044     : Transition("CompareTrans",name,destination), mon(monitor), tst(test), val(value), poller(poll)
00045   { }
00046   
00047   //!starts listening
00048   virtual void DoStart() { Transition::DoStart(); erouter->addListener(this,poller); }
00049 
00050   //!stops listening
00051   virtual void DoStop() { erouter->removeListener(this); Transition::DoStop(); }
00052 
00053   //!don't care about the event, just a pulse to check the values
00054   virtual void processEvent(const EventBase&) {
00055     switch(tst) {
00056     case LT:
00057       if(*mon<val) fire();
00058       break;
00059     case GT:
00060       if(val<*mon) fire();
00061       break;
00062     case LTE:
00063       if(!(val<*mon)) fire();
00064       break;
00065     case GTE:
00066       if(!(*mon<val)) fire();
00067       break;
00068     case EQ:
00069       if(*mon==val) fire();
00070       break;
00071     case NE:
00072       if(!(*mon==val)) fire();
00073       break;
00074     }
00075   }
00076 
00077 protected:
00078   //! constructor, see CompareTrans class notes for information (this version is only need by subclasses so they can pass their type name)
00079   CompareTrans(const std::string& classname, const std::string& instancename, StateNode* destination, const T* monitor, Test_t test, const T& value, const EventBase& poll)
00080     : Transition(classname,instancename,destination), mon(monitor), tst(test), val(value), poller(poll)
00081   { }
00082 
00083   const T* mon; //!< address of value to monitor
00084   Test_t tst; //!< test to make
00085   T val; //!< value to compare against
00086   EventBase poller; //!< event to listen to, when it comes, compare the values
00087 
00088 private:
00089   CompareTrans(const CompareTrans& node); //!< don't call this
00090   CompareTrans operator=(const CompareTrans& node); //!< don't call this
00091 };
00092 
00093 /*! @file
00094  * @brief Defines CompareTrans, which causes a transition if a value (through a pointer) goes above a given value
00095  * @author ejt (Creator)
00096  *
00097  * $Author: ejt $
00098  * $Name: tekkotsu-2_2_1 $
00099  * $Revision: 1.9 $
00100  * $State: Exp $
00101  * $Date: 2004/11/15 22:46:19 $
00102  */
00103 
00104 #endif

Tekkotsu v2.2.1
Generated Tue Nov 23 16:36:37 2004 by Doxygen 1.3.9.1