Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

TextMsgTrans.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_TextMsgTrans_h_
00003 #define INCLUDED_TextMsgTrans_h_
00004 
00005 #include "Behaviors/Transition.h"
00006 #include "Events/TextMsgEvent.h"
00007 #include "Events/EventRouter.h"
00008 #include "Shared/MarkScope.h"
00009 
00010 //! Fires when a matching string is received
00011 class TextMsgTrans : public Transition {
00012 
00013   // **************************** //
00014   // ******* CONSTRUCTORS ******* //
00015   // **************************** //
00016 public:
00017   //! default constructor, use type name as instance name
00018   TextMsgTrans(StateNode* destination, const std::string& message)
00019     : Transition(destination), msg(message), haveMsg(true), savedEvent()
00020   {}
00021 
00022   //! constructor with explicit instance name
00023   TextMsgTrans(const std::string& name, StateNode* destination, const std::string& message)
00024     : Transition(name,destination), msg(message), haveMsg(true), savedEvent()
00025   {}
00026   
00027   //! constructor with explicit instance name but no message; will match anything
00028   TextMsgTrans(const std::string& name, StateNode* destination)
00029     : Transition(name,destination), msg(), haveMsg(false), savedEvent()
00030   {}
00031   
00032   
00033   // **************************** //
00034   // ********* METHODS ********** //
00035   // **************************** //
00036 public:
00037   virtual void preStart() {
00038     Transition::preStart();
00039     erouter->addListener(this, EventBase::textmsgEGID );
00040   }
00041 
00042   virtual void doEvent() {
00043     if ( event->getGeneratorID() == EventBase::textmsgEGID ) {
00044 
00045       // we're looking to match a specific message
00046       if ( haveMsg ) {
00047         if ( const TextMsgEvent *txtev = dynamic_cast<const TextMsgEvent*>(event) ) {
00048     if ( txtev->getText() == msg )
00049       fire(*event);
00050     else
00051       return;
00052         }
00053       }
00054       // default match case (haveMsg is false): set timer so we
00055       // can unwind stack and then match this message if no
00056       // other transition has fired
00057       else {
00058         savedEvent = *event;
00059         erouter->addTimer(this, 1, 1, false);
00060       }
00061     }
00062 
00063     // timer has expired, so we'll match this message
00064     else if ( event->getGeneratorID() == EventBase::timerEGID )
00065       fire(savedEvent);
00066   }
00067 
00068   static std::string getClassDescription() { return "Fires when a matching string is received"; }
00069   virtual std::string getDescription() const { return getClassDescription(); }
00070 
00071 
00072   // **************************** //
00073   // ********* MEMBERS ********** //
00074   // **************************** //
00075 protected:
00076   std::string msg; //!< the trigger to match messages against
00077   bool haveMsg; //!< true if msg value was supplied in constructor
00078   TextMsgEvent savedEvent; //!< copy of triggering event, used for reporting default match
00079 
00080 
00081   // **************************** //
00082   // ********** OTHER *********** //
00083   // **************************** //
00084 private:
00085   // Providing declarations for these functions will avoid a compiler warning if
00086   // you have any class members which are pointers.  However, as it is, an error
00087   // will result if you inadvertantly cause a call to either (which is probably
00088   // a good thing, unless you really intended to copy/assign a behavior, in
00089   // which case simply provide implementations for the functions)
00090   TextMsgTrans(const TextMsgTrans&); //!< don't call (copy constructor)
00091   TextMsgTrans& operator=(const TextMsgTrans&); //!< don't call (assignment operator)
00092 };
00093 
00094 /*! @file
00095  * @brief Defines TextMsgTrans, which fires when a matching string is received
00096  * @author ejt (Creator)
00097  */
00098 
00099 #endif

Tekkotsu v5.1CVS
Generated Fri Mar 16 05:26:53 2012 by Doxygen 1.6.3