Homepage Demos Overview Downloads Tutorials Reference
Credits

StateNode.cc

Go to the documentation of this file.
00001 #include "StateNode.h"
00002 #include "Events/EventRouter.h"
00003 #include "Wireless/Wireless.h"
00004 
00005 /*! @deprecated, behavior constructors should take a name argument (which by default should be the name of the type of the class) */
00006 StateNode::StateNode() : BehaviorBase("StateNode"), parent(NULL), transitions(), issetup(false), retain(true), nodes() {}
00007 
00008 StateNode::~StateNode() {
00009   ASSERT(!isActive(), "Destructing while active?")
00010   for(std::vector<Transition*>::iterator it=transitions.begin(); it!=transitions.end(); it++)
00011     (*it)->RemoveReference();
00012   if(issetup) {
00013     for(std::vector<StateNode*>::iterator it=nodes.begin(); it!=nodes.end(); it++)
00014       (*it)->RemoveReference();
00015   }
00016 }
00017 
00018 Transition* StateNode::addTransition(Transition* trans) {
00019   transitions.push_back(trans);
00020   trans->AddReference();
00021   trans->addSource(this);
00022   return trans;
00023 }
00024 
00025 StateNode* StateNode::addNode(StateNode* node) {
00026   nodes.push_back(node);
00027   node->AddReference();
00028   if ( node->parent == NULL )
00029     node->parent = this;
00030   return node;
00031 }
00032 
00033 void StateNode::DoStart() {
00034   if ( parent == NULL && transitions.size() > 0 )
00035     serr->printf("Warning! StateNode '%s' has transitions but no parent; you probably forgot to call addNode().\n",getName().c_str());
00036   BehaviorBase::DoStart();
00037   if(!issetup) {
00038     setup();
00039     issetup=true;
00040   }
00041   for(std::vector<Transition*>::iterator it=transitions.begin(); it!=transitions.end(); it++)
00042     if ( !(*it)->isActive()  ) (*it)->DoStart();
00043   erouter->postEvent(EventBase::stateMachineEGID,reinterpret_cast<unsigned int>(this),EventBase::activateETID,0,getName(),1);
00044   if(parent!=NULL)
00045     parent->transitionTo(this);
00046 }
00047 
00048 void StateNode::DoStop() {
00049   for(std::vector<Transition*>::iterator it=transitions.begin(); it!=transitions.end(); it++) {
00050     if((*it)->isActive())
00051       (*it)->DoStop();
00052   }
00053   for(std::vector<StateNode*>::iterator it=nodes.begin(); it!=nodes.end(); it++)
00054     if((*it)->isActive())
00055       (*it)->DoStop();
00056   if(!retain) {
00057     teardown();
00058     issetup=false;
00059     for(std::vector<StateNode*>::iterator it=nodes.begin(); it!=nodes.end(); it++)
00060       (*it)->RemoveReference();
00061     nodes.clear();
00062   }
00063   erouter->postEvent(EventBase::stateMachineEGID,reinterpret_cast<unsigned int>(this),EventBase::deactivateETID,0,getName(),0);
00064   BehaviorBase::DoStop();
00065 }
00066 
00067 void StateNode::transitionTo(StateNode*) {
00068   // may want to throw a status event here
00069 }
00070 
00071 void StateNode::transitionFrom(StateNode*) {
00072   // may want to throw a status event here
00073 }
00074 
00075 /*! @file 
00076  * @brief Describes StateNode, which is both a state machine controller as well as a node within a state machine itself
00077  * @author ejt (Creator)
00078  *
00079  * $Author: ejt $
00080  * $Name: tekkotsu-2_2_2 $
00081  * $Revision: 1.20 $
00082  * $State: Exp $
00083  * $Date: 2004/12/17 05:50:56 $
00084  */
00085 

Tekkotsu v2.2.2
Generated Tue Jan 4 15:43:15 2005 by Doxygen 1.4.0