Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

CompletionTrans.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_CompletionTrans_h_
00003 #define INCLUDED_CompletionTrans_h_
00004 
00005 #include "Behaviors/StateNode.h"
00006 #include "Behaviors/Transition.h"
00007 #include "Events/EventRouter.h"
00008 
00009 //! causes a transition when at least @e n sources have signalled completion;  @e n = 0 means "all" (default)
00010 class CompletionTrans : public Transition {
00011 protected:
00012   int minsrcs; //!< the minimum number of sources which must signal completion before this transition will fire
00013   bool *completions;  //!< pointer to array for recording completion events for all sources
00014 
00015 public:
00016   //! constructor, pass @a destination and the minimum number of sources which must signal completion before this transition will fire
00017   CompletionTrans(StateNode* destination, int n=0) :
00018     Transition("CompletionTrans",destination), minsrcs(n), completions(NULL) {};
00019 
00020   //! constructor, pass @a name, @a destination and the minimum number of times the source must signal completion beyond the first (@a n)
00021   CompletionTrans(const std::string& name, StateNode* destination, int n=0) :
00022     Transition("CompletionTrans",name,destination), minsrcs(n), completions(NULL) {};
00023 
00024   //! starts listening
00025   virtual void DoStart() {
00026     Transition::DoStart();
00027     unsigned int const numsrcs = getSources().size();
00028     completions = new bool[numsrcs];
00029     for (unsigned int i = 0; i < numsrcs; i++) {
00030       completions[i] = false;
00031       erouter->addListener(this,
00032          EventBase::stateMachineEGID,
00033          (unsigned int)getSources()[i],
00034          EventBase::statusETID);
00035     };
00036   }
00037 
00038   //! stops listening
00039   virtual void DoStop() {
00040     erouter->removeListener(this);
00041     delete completions;
00042     completions = NULL;
00043     Transition::DoStop();
00044   }
00045 
00046   //! record completions, and fire the transition if all sources have completed
00047   virtual void processEvent(const EventBase &event) {
00048     int numcomplete = 0;
00049     for ( unsigned int i=0; i<getSources().size(); i++ ) {
00050       if ( event.getSourceID() == (unsigned int)getSources()[i] )
00051   completions[i] = true;
00052       if ( completions[i] ) ++numcomplete;
00053     };
00054     int const threshold = (minsrcs > 0 ? minsrcs : (int)getSources().size());
00055     if (numcomplete >= threshold) fire();
00056   }
00057 
00058 protected:
00059   //!constructor, this version is only need by subclasses so they can pass their type name
00060   CompletionTrans(const std::string& classname, const std::string& instancename, StateNode* destination, int n=0) :
00061     Transition(classname,instancename,destination), minsrcs(n), completions(NULL) {};
00062 
00063   //!@name Dummy functions to satisfy the compiler
00064   CompletionTrans(const CompletionTrans&);  //!< don't call this
00065   CompletionTrans& operator=(const CompletionTrans&);  //!< don't call this
00066   //@}
00067 
00068 };
00069 
00070 /*! @file
00071  * @brief Defines Completiontrans, which causes a transition if all sources have signalled completion
00072  * @author dst (Creator)
00073  *
00074  * $Author: ejt $
00075  * $Name: tekkotsu-2_4_1 $
00076  * $Revision: 1.9 $
00077  * $State: Exp $
00078  * $Date: 2004/12/23 01:47:07 $
00079  */
00080 
00081 #endif

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:46 2005 by Doxygen 1.4.4