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

Tekkotsu v2.3
Generated Sat Jan 29 02:25:23 2005 by Doxygen 1.4.0