Homepage Demos Overview Downloads Tutorials Reference
Credits
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Search

StateNode.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_StateNode_h_
00003 #define INCLUDED_StateNode_h_
00004 
00005 #include "Transition.h"
00006 #include "Behaviors/BehaviorBase.h"
00007 #include <vector>
00008 #include <string>
00009 
00010 //! Recursive data structure - both a state machine controller as well as a node within a state machine itself
00011 /*! Override setup() to setup your own Transition and StateNode network.*/
00012 class StateNode  : public BehaviorBase {
00013 public:
00014   //!constructor
00015   StateNode() : BehaviorBase(), parent(NULL), transitions(), issetup(false), retain(true), nodes(), name("?") {}
00016 
00017   //!constructor, pass a name to use, calls setName(n) 
00018   StateNode(const char* n, StateNode* p=NULL) : BehaviorBase(), parent(p), transitions(), issetup(false), retain(true), nodes(), name(NULL) {
00019     setName(n);
00020   }
00021 
00022   //!destructor, frees memory used by its outgoing transitions (be careful of incoming ones - they're still around!), and calls RemoveReference() on subnodes
00023   virtual ~StateNode();
00024 
00025   //!Adds the specified StateTransition to the transition table
00026   virtual void addTransition(Transition* trans);
00027 
00028   //!Returns the std::vector of transitions so you can modify them yourself if need be
00029   std::vector<Transition*>& getTransitions() { return transitions; }
00030 
00031   //!Adds a StateNode to #nodes so it can be automatically deleted later, returns what it's passed (for convenience), calls AddReference() on @a node
00032   virtual StateNode* addNode(StateNode* node) { nodes.push_back(node); node->AddReference(); return node; }
00033 
00034   //!Returns the std::vector of nodes so you can modify them yourself if need be
00035   std::vector<StateNode*>& getNodes() { return nodes; }
00036 
00037   //!Sets the retain flag - if not retained, will RemoveReference() subnodes upon DoStop() and recreate them on DoStart (by calling setup()) - may be too expensive to be worth saving memory...
00038   void setRetain(bool f) { retain=f; }
00039 
00040   //!Transitions should call this when you are entering the state, so it can enable its transitions
00041   void DoStart();
00042 
00043   //!This is called by DoStart() when you should setup the network
00044   virtual void setup() {issetup=true;}
00045 
00046   //!Transitions should call this when you are leaving the state, so it can disable its transitions
00047   void DoStop();
00048   
00049   //!set name to @a n.  Makes a copy of @a n, so you can throw it away later.
00050   void setName(const std::string& n);
00051 
00052   //!returns name of StateNode
00053   virtual std::string getName() const { return name; }
00054 
00055   //!returns name again (you should override this for top-level nodes to give users better descriptions)
00056   virtual std::string getDescription() const { return name; }
00057 
00058   //!Doesn't do anything, supplied here so you don't have to (since events will probably be going to Transition's, not here)
00059   virtual void processEvent(const EventBase&) {}
00060 
00061   //!called by a subnode when it is being DoStart()'ed
00062   void transitionTo(StateNode* n);
00063 
00064   //!called by a subnode when it is being DoStop()'ed
00065   void transitionFrom(StateNode* n);
00066 
00067 protected:
00068   //Node Stuff:
00069   //! pointer to the machine that contains this node
00070   StateNode* parent;
00071   //! a vector of outgoing transitions
00072   std::vector<Transition*> transitions;
00073   
00074   //Machine Stuff:
00075   //! this is set to true if the network has been setup but not destroyed (i.e. don't need to call setupSubNodes again)
00076   bool issetup;
00077   //! this is set to true if the network should be retained between activations.  Otherwise it's deleted upon DoStop(). (or at least RemoveReference() is called on subnodes)
00078   bool retain;
00079   //! vector of StateNodes, just so they can be deleted again on DoStop() (unless retained) or ~StateNode()
00080   std::vector<StateNode*> nodes;
00081 
00082   //Behavior Stuff:
00083   //! holds the name of the Node/Machine
00084   std::string name;
00085 
00086 private:
00087   StateNode(const StateNode& node); //!< don't call this
00088   StateNode operator=(const StateNode& node); //!< don't call this
00089 };
00090 
00091 /*! @file
00092  * @brief Describes StateNode, which is both a state machine controller as well as a node within a state machine itself
00093  * @author ejt (Creator)
00094  *
00095  * $Author: ejt $
00096  * $Name: tekkotsu-1_4_1 $
00097  * $Revision: 1.2 $
00098  * $State: Exp $
00099  * $Date: 2003/06/05 17:03:09 $
00100  */
00101 
00102 #endif

Tekkotsu v1.4
Generated Sat Jul 19 00:06:31 2003 by Doxygen 1.3.2