Homepage Demos Overview Downloads Tutorials 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;
00013   bool *completions;  //!< pointer to array for recording completion events for all sources
00014 
00015 public:
00016   CompletionTrans(StateNode* destination, int n=0) :
00017     Transition("CompletionTrans",destination), minsrcs(n), completions(NULL) {};
00018 
00019   CompletionTrans(const std::string& name, StateNode* destination, int n=0) :
00020     Transition("CompletionTrans",name,destination), minsrcs(n), completions(NULL) {};
00021 
00022   //! starts listening
00023   virtual void DoStart() {
00024     Transition::DoStart();
00025     unsigned int const numsrcs = getSources().size();
00026     completions = new bool[numsrcs];
00027     for (unsigned int i = 0; i < numsrcs; i++) {
00028       completions[i] = false;
00029       erouter->addListener(this,
00030          EventBase::stateMachineEGID,
00031          (unsigned int)getSources()[i],
00032          EventBase::statusETID);
00033     };
00034   }
00035 
00036   //! stops listening
00037   virtual void DoStop() {
00038     erouter->removeListener(this);
00039     delete completions;
00040     completions = NULL;
00041     Transition::DoStop();
00042   }
00043 
00044   //! record completions, and fire the transition if all sources have completed
00045   virtual void processEvent(const EventBase &event) {
00046     int numcomplete = 0;
00047     for ( unsigned int i=0; i<getSources().size(); i++ ) {
00048       if ( event.getSourceID() == (unsigned int)getSources()[i] )
00049   completions[i] = true;
00050       if ( completions[i] ) ++numcomplete;
00051     };
00052     int const threshold = (minsrcs > 0 ? minsrcs : (int)getSources().size());
00053     if (numcomplete >= threshold) fire();
00054   }
00055 
00056 protected:
00057   //!constructor, this version is only need by subclasses so they can pass their type name
00058   CompletionTrans(const std::string& classname, const std::string& instancename, StateNode* destination, int n=0) :
00059     Transition(classname,instancename,destination), minsrcs(n), completions(NULL) {};
00060 
00061   //!@name Dummy functions to satisfy the compiler
00062   CompletionTrans(const CompletionTrans&);  //!< don't call this
00063   CompletionTrans& operator=(const CompletionTrans&);  //!< don't call this
00064   //@}
00065 
00066 };
00067 
00068 /*! @file
00069  * @brief Defines Completiontrans, which causes a transition if all sources have signalled completion
00070  * @author dst (Creator)
00071  *
00072  * $Author: ejt $
00073  * $Name: tekkotsu-2_2_1 $
00074  * $Revision: 1.8 $
00075  * $State: Exp $
00076  * $Date: 2004/11/15 22:46:19 $
00077  */
00078 
00079 #endif

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