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 void StateNode::addTransition(Transition* trans) {
00019   transitions.push_back(trans);
00020   trans->AddReference();
00021   trans->addSource(this);
00022 }
00023 
00024 StateNode* StateNode::addNode(StateNode* node) {
00025   nodes.push_back(node);
00026   node->AddReference();
00027   if ( node->parent == NULL )
00028     node->parent = this;
00029   return node;
00030 }
00031 
00032 void StateNode::DoStart() {
00033   if ( parent == NULL && transitions.size() > 0 )
00034     serr->printf("Warning! StateNode '%s' has transitions but no parent; you probably forgot to call addNode().\n",getName().c_str());
00035   BehaviorBase::DoStart();
00036   if(!issetup) {
00037     setup();
00038     issetup=true;
00039   }
00040   for(std::vector<Transition*>::iterator it=transitions.begin(); it!=transitions.end(); it++)
00041     if ( !(*it)->isActive()  ) (*it)->DoStart();
00042   erouter->postEvent(EventBase::stateMachineEGID,reinterpret_cast<unsigned int>(this),EventBase::activateETID,0,getName(),1);
00043   if(parent!=NULL)
00044     parent->transitionTo(this);
00045 }
00046 
00047 void StateNode::DoStop() {
00048   for(std::vector<Transition*>::iterator it=transitions.begin(); it!=transitions.end(); it++) {
00049     if((*it)->isActive())
00050       (*it)->DoStop();
00051   }
00052   for(std::vector<StateNode*>::iterator it=nodes.begin(); it!=nodes.end(); it++)
00053     if((*it)->isActive())
00054       (*it)->DoStop();
00055   if(!retain) {
00056     teardown();
00057     issetup=false;
00058     for(std::vector<StateNode*>::iterator it=nodes.begin(); it!=nodes.end(); it++)
00059       (*it)->RemoveReference();
00060     nodes.clear();
00061   }
00062   erouter->postEvent(EventBase::stateMachineEGID,reinterpret_cast<unsigned int>(this),EventBase::deactivateETID,0,getName(),0);
00063   BehaviorBase::DoStop();
00064 }
00065 
00066 void StateNode::transitionTo(StateNode*) {
00067   // may want to throw a status event here
00068 }
00069 
00070 void StateNode::transitionFrom(StateNode*) {
00071   // may want to throw a status event here
00072 }
00073 
00074 /*! @file 
00075  * @brief Describes StateNode, which is both a state machine controller as well as a node within a state machine itself
00076  * @author ejt (Creator)
00077  *
00078  * $Author: ejt $
00079  * $Name: tekkotsu-2_2_1 $
00080  * $Revision: 1.19 $
00081  * $State: Exp $
00082  * $Date: 2004/11/11 01:45:31 $
00083  */
00084 

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