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