Homepage Demos Overview Downloads Tutorials Reference
Credits

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("?") { /*std::cout << "!" << std::endl;*/ }
00016 
00017   //!constructor, pass a name to use, calls setName(n) 
00018   StateNode(const std::string& n, StateNode* p=NULL) : BehaviorBase(), parent(p), transitions(), issetup(false), retain(true), nodes(), name("") {
00019     setName(n);
00020   }
00021 
00022   //!destructor, removes references to 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 dereferenced 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   virtual void DoStart();
00042 
00043   //!This is called by DoStart() when you should setup the network of subnodes
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   virtual void DoStop();
00048   
00049   //!This is called by DoStop() when you should destruct subnodes
00050   virtual void teardown() { issetup=false; /*std::cout << "Teardown!!!!!!!!" << std::endl;*/ }
00051 
00052   //!set name to @a n.  Makes a copy of @a n, so you can throw it away later.
00053   void setName(const std::string& n);
00054 
00055   //!returns name of StateNode
00056   virtual std::string getName() const { return name; }
00057 
00058   //!returns name again (you should override this for top-level nodes to give users better descriptions)
00059   virtual std::string getDescription() const { return name; }
00060 
00061 protected:
00062   //!called by a subnode when it is being DoStart()'ed
00063   virtual void transitionTo(StateNode* n);
00064 
00065   //!called by a subnode when it is being DoStop()'ed
00066   virtual void transitionFrom(StateNode* n);
00067 
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 dereferenced upon DoStop(). (or at least RemoveReference() is called on subnodes)
00078   bool retain;
00079   //! vector of StateNodes, just so they can be dereferenced 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-2_1 $
00097  * $Revision: 1.9 $
00098  * $State: Exp $
00099  * $Date: 2003/10/30 23:23:15 $
00100  */
00101 
00102 #endif

Tekkotsu v2.1
Generated Tue Mar 16 23:19:15 2004 by Doxygen 1.3.5