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     switch ( event->getGeneratorID() ) {
00044 
00045     case EventBase::textmsgEGID:
00046       // we're looking to match a specific message
00047       if ( haveMsg ) {
00048         if ( const TextMsgEvent *txtev = dynamic_cast<const TextMsgEvent*>(event) ) {
00049           if ( txtev->getText() == msg )
00050             fire(*event);
00051           else
00052             return;
00053         }
00054       }
00055       // default match case (haveMsg is false): set timer so we
00056       // can unwind stack and then match this message if no
00057       // other transition has fired
00058       else {
00059         savedEvent = *event;
00060         erouter->addTimer(this, 9999, 1, false);
00061       }
00062       break;
00063 
00064     // timer has expired, so we'll match this message
00065     case EventBase::timerEGID:
00066       fire(savedEvent);
00067       break;
00068 
00069     default:
00070       std::cout << "TextMsgTrans received unexpected event type: " << event->getDescription() << std::endl;
00071     }
00072   }
00073 
00074   static std::string getClassDescription() { return "Fires when a matching string is received"; }
00075   virtual std::string getDescription() const { return getClassDescription(); }
00076 
00077 
00078   // **************************** //
00079   // ********* MEMBERS ********** //
00080   // **************************** //
00081 protected:
00082   std::string msg; //!< the trigger to match messages against
00083   bool haveMsg; //!< true if msg value was supplied in constructor
00084   TextMsgEvent savedEvent; //!< copy of triggering event, used for reporting default match
00085 
00086 
00087   // **************************** //
00088   // ********** OTHER *********** //
00089   // **************************** //
00090 private:
00091   // Providing declarations for these functions will avoid a compiler warning if
00092   // you have any class members which are pointers.  However, as it is, an error
00093   // will result if you inadvertantly cause a call to either (which is probably
00094   // a good thing, unless you really intended to copy/assign a behavior, in
00095   // which case simply provide implementations for the functions)
00096   TextMsgTrans(const TextMsgTrans&); //!< don't call (copy constructor)
00097   TextMsgTrans& operator=(const TextMsgTrans&); //!< don't call (assignment operator)
00098 };
00099 
00100 /*! @file
00101  * @brief Defines TextMsgTrans, which fires when a matching string is received
00102  * @author ejt (Creator)
00103  */
00104 
00105 #endif

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