Homepage
Demos
Overview
Downloads
Dev. Resources
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  *  There are two StateNode templates in <a href="http://cvs.tekkotsu.org/cgi-bin/viewcvs.cgi/Tekkotsu/project/templates/"><i>project</i><tt>/templates/</tt></a>: <a href="http://cvs.tekkotsu.org/cgi-bin/viewcvs.cgi/Tekkotsu/project/templates/statenode.h?rev=HEAD&content-type=text/vnd.viewcvs-markup">statenode.h</a> and <a href="http://cvs.tekkotsu.org/cgi-bin/viewcvs.cgi/Tekkotsu/project/templates/statemachine.h?rev=HEAD&content-type=text/vnd.viewcvs-markup">statemachine.h</a>.
00013  *  statenode.h is meant for leaf nodes, which directly implement the execution of a task.
00014  *  statemachine.h is meant for nodes which contain a network of transitions and subnodes, which together solve the task.
00015 */
00016 class StateNode  : public BehaviorBase {
00017 public:
00018   //!constructor, pass a name to use
00019   StateNode(const std::string& name) : BehaviorBase("StateNode",name), parent(NULL), transitions(), issetup(false), retain(true), startedTime(0), nodes() {}
00020 
00021   //!destructor, removes references to its outgoing transitions (be careful of incoming ones - they're still around!), and calls RemoveReference() on subnodes
00022   virtual ~StateNode();
00023 
00024   //!Adds the specified StateTransition to the transition table
00025   virtual Transition* addTransition(Transition* trans);
00026 
00027   //!Returns the std::vector of transitions so you can modify them yourself if need be
00028   std::vector<Transition*>& getTransitions() { return transitions; }
00029 
00030   //!Returns the const std::vector of transitions so you can read through them if need be
00031   const std::vector<Transition*>& getTransitions() const { return transitions; }
00032 
00033   //!Adds a StateNode to #nodes so it can be automatically dereferenced later, returns what it's passed (for convenience), calls AddReference() on @a node.  Also sets the node's parent to @c this if it is null.
00034   virtual StateNode* addNode(StateNode* node);
00035 
00036   //!Returns the std::vector of sub-nodes so you can modify them yourself if need be
00037   std::vector<StateNode*>& getNodes() { return nodes; }
00038 
00039   //!Returns the const std::vector of sub-nodes so you can read through them if need be
00040   const std::vector<StateNode*>& getNodes() const { return nodes; }
00041 
00042   //!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...
00043   void setRetain(bool f) { retain=f; }
00044 
00045   //!Transitions should call this when you are entering the state, so it can enable its transitions
00046   virtual void DoStart();
00047 
00048   //!This is called by DoStart() when you should setup the network of subnodes (if any)
00049   virtual void setup() {issetup=true;}
00050 
00051   //!Transitions should call this when you are leaving the state, so it can disable its transitions
00052   virtual void DoStop();
00053   
00054   //!This is called by DoStop() when you should destruct subnodes
00055   /*!Default implementation will take care of the subnodes and their
00056    * transitions, you only need to worry about any *other* memory
00057    * which may have been allocated.  If none, you may not need
00058    * implement this function at all. */
00059   virtual void teardown();
00060 
00061   //!returns #parent
00062   virtual StateNode* getParent() const { return parent; }
00063 
00064 protected:
00065   //!constructor, pass the class name and instance name to use 
00066   StateNode(const std::string& classname, const std::string& name) : BehaviorBase(classname,name), parent(NULL), transitions(), issetup(false), retain(true), startedTime(0), nodes() {}
00067 
00068   //!will throw an activation event through stateMachineEGID, used when DoStart() is called
00069   virtual void postStartEvent();
00070 
00071   //!will throw a status event through stateMachineEGID to signal "completion" of the node
00072   /*! "completion" is defined by your subclass - will mean different things to different
00073    *  nodes depending on the actions they are performing.  So call this yourself if there
00074    *  is a natural ending point for your state.
00075    * @param magnitude the value to use for EventBase::magnitude -- generally is 1 for status events, but since this is to signal completion, 0 (the default) may be more appropriate; if your node is doing something repetitive however, 1 (or the loop count) may be better */
00076   virtual void postCompletionEvent(float magnitude=0);
00077 
00078   //!will throw an deactivation event through stateMachineEGID, used when DoStop() is called
00079   /* @param duration the value to use for EventBase::duration -- nice but not generally used */
00080   virtual void postStopEvent();
00081 
00082   //Node Stuff:
00083   //! pointer to the machine that contains this node
00084   StateNode* parent;
00085   //! a vector of outgoing transitions
00086   std::vector<Transition*> transitions;
00087   
00088   //Machine Stuff:
00089   //! this is set to true if the network has been setup but not destroyed (i.e. don't need to call setupSubNodes again)
00090   bool issetup;
00091   //! 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)
00092   bool retain;
00093   //! the timestamp of last call to DoStart()
00094   unsigned int startedTime;
00095   //! vector of StateNodes, just so they can be dereferenced again on DoStop() (unless retained) or ~StateNode()
00096   std::vector<StateNode*> nodes;
00097 
00098 private:
00099   StateNode(const StateNode& node); //!< don't call this
00100   StateNode operator=(const StateNode& node); //!< don't call this
00101 };
00102 
00103 /*! @file
00104  * @brief Describes StateNode, which is both a state machine controller as well as a node within a state machine itself
00105  * @author ejt (Creator)
00106  *
00107  * $Author: ejt $
00108  * $Name: tekkotsu-2_4_1 $
00109  * $Revision: 1.20 $
00110  * $State: Exp $
00111  * $Date: 2005/08/07 04:11:02 $
00112  */
00113 
00114 #endif

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:49 2005 by Doxygen 1.4.4