Annotation of /trunk/indra/llcharacter/llstatemachine.h
Parent Directory
|
Revision Log
Revision 137 - (view) (download)
| 1 : | mjm | 135 | /** |
| 2 : | * @file llstatemachine.h | ||
| 3 : | * @brief LLStateMachine class header file. | ||
| 4 : | * | ||
| 5 : | * $LicenseInfo:firstyear=2001&license=viewergpl$ | ||
| 6 : | * | ||
| 7 : | mjm | 137 | * Copyright (c) 2001-2010, Linden Research, Inc. |
| 8 : | mjm | 135 | * |
| 9 : | * Second Life Viewer Source Code | ||
| 10 : | * The source code in this file ("Source Code") is provided by Linden Lab | ||
| 11 : | * to you under the terms of the GNU General Public License, version 2.0 | ||
| 12 : | * ("GPL"), unless you have obtained a separate licensing agreement | ||
| 13 : | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
| 14 : | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
| 15 : | * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2 | ||
| 16 : | * | ||
| 17 : | * There are special exceptions to the terms and conditions of the GPL as | ||
| 18 : | * it is applied to this Source Code. View the full text of the exception | ||
| 19 : | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
| 20 : | * online at | ||
| 21 : | * http://secondlifegrid.net/programs/open_source/licensing/flossexception | ||
| 22 : | * | ||
| 23 : | * By copying, modifying or distributing this software, you acknowledge | ||
| 24 : | * that you have read and understood your obligations described above, | ||
| 25 : | * and agree to abide by those obligations. | ||
| 26 : | * | ||
| 27 : | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
| 28 : | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
| 29 : | * COMPLETENESS OR PERFORMANCE. | ||
| 30 : | * $/LicenseInfo$ | ||
| 31 : | */ | ||
| 32 : | |||
| 33 : | #ifndef LL_LLSTATEMACHINE_H | ||
| 34 : | #define LL_LLSTATEMACHINE_H | ||
| 35 : | |||
| 36 : | #include <string> | ||
| 37 : | |||
| 38 : | #include "llerror.h" | ||
| 39 : | #include <map> | ||
| 40 : | |||
| 41 : | class LLUniqueID | ||
| 42 : | { | ||
| 43 : | friend bool operator==(const LLUniqueID &a, const LLUniqueID &b); | ||
| 44 : | friend bool operator!=(const LLUniqueID &a, const LLUniqueID &b); | ||
| 45 : | protected: | ||
| 46 : | static U32 sNextID; | ||
| 47 : | U32 mId; | ||
| 48 : | public: | ||
| 49 : | LLUniqueID(){mId = sNextID++;} | ||
| 50 : | virtual ~LLUniqueID(){} | ||
| 51 : | U32 getID() {return mId;} | ||
| 52 : | }; | ||
| 53 : | |||
| 54 : | class LLFSMTransition : public LLUniqueID | ||
| 55 : | { | ||
| 56 : | public: | ||
| 57 : | LLFSMTransition() : LLUniqueID(){}; | ||
| 58 : | virtual std::string getName()const { return "unnamed"; } | ||
| 59 : | }; | ||
| 60 : | |||
| 61 : | class LLFSMState : public LLUniqueID | ||
| 62 : | { | ||
| 63 : | public: | ||
| 64 : | LLFSMState() : LLUniqueID(){}; | ||
| 65 : | virtual void onEntry(void *){}; | ||
| 66 : | virtual void onExit(void *){}; | ||
| 67 : | virtual void execute(void *){}; | ||
| 68 : | virtual std::string getName() const { return "unnamed"; } | ||
| 69 : | }; | ||
| 70 : | |||
| 71 : | class LLStateDiagram | ||
| 72 : | { | ||
| 73 : | typedef std::map<LLFSMTransition*, LLFSMState*> Transitions; | ||
| 74 : | |||
| 75 : | friend std::ostream& operator<<(std::ostream &s, LLStateDiagram &FSM); | ||
| 76 : | friend class LLStateMachine; | ||
| 77 : | |||
| 78 : | protected: | ||
| 79 : | typedef std::map<LLFSMState*, Transitions> StateMap; | ||
| 80 : | StateMap mStates; | ||
| 81 : | Transitions mDefaultTransitions; | ||
| 82 : | LLFSMState* mDefaultState; | ||
| 83 : | BOOL mUseDefaultState; | ||
| 84 : | |||
| 85 : | public: | ||
| 86 : | LLStateDiagram(); | ||
| 87 : | virtual ~LLStateDiagram(); | ||
| 88 : | |||
| 89 : | protected: | ||
| 90 : | // add a state to the state graph, executed implicitly when adding transitions | ||
| 91 : | BOOL addState(LLFSMState *state); | ||
| 92 : | |||
| 93 : | // add a directed transition between 2 states | ||
| 94 : | BOOL addTransition(LLFSMState& start_state, LLFSMState& end_state, LLFSMTransition& transition); | ||
| 95 : | |||
| 96 : | // add an undirected transition between 2 states | ||
| 97 : | BOOL addUndirectedTransition(LLFSMState& start_state, LLFSMState& end_state, LLFSMTransition& transition); | ||
| 98 : | |||
| 99 : | // add a transition that is taken if none other exist | ||
| 100 : | void addDefaultTransition(LLFSMState& end_state, LLFSMTransition& transition); | ||
| 101 : | |||
| 102 : | // process a possible transition, and get the resulting state | ||
| 103 : | LLFSMState* processTransition(LLFSMState& start_state, LLFSMTransition& transition); | ||
| 104 : | |||
| 105 : | // add a transition that exists for every state | ||
| 106 : | void setDefaultState(LLFSMState& default_state); | ||
| 107 : | |||
| 108 : | // return total number of states with no outgoing transitions | ||
| 109 : | S32 numDeadendStates(); | ||
| 110 : | |||
| 111 : | // does this state exist in the state diagram? | ||
| 112 : | BOOL stateIsValid(LLFSMState& state); | ||
| 113 : | |||
| 114 : | // get a state pointer by ID | ||
| 115 : | LLFSMState* getState(U32 state_id); | ||
| 116 : | |||
| 117 : | public: | ||
| 118 : | // save the graph in a DOT file for rendering and visualization | ||
| 119 : | BOOL saveDotFile(const std::string& filename); | ||
| 120 : | }; | ||
| 121 : | |||
| 122 : | class LLStateMachine | ||
| 123 : | { | ||
| 124 : | protected: | ||
| 125 : | LLFSMState* mCurrentState; | ||
| 126 : | LLFSMState* mLastState; | ||
| 127 : | LLFSMTransition* mLastTransition; | ||
| 128 : | LLStateDiagram* mStateDiagram; | ||
| 129 : | |||
| 130 : | public: | ||
| 131 : | LLStateMachine(); | ||
| 132 : | virtual ~LLStateMachine(); | ||
| 133 : | |||
| 134 : | // set state diagram | ||
| 135 : | void setStateDiagram(LLStateDiagram* diagram); | ||
| 136 : | |||
| 137 : | // process this transition | ||
| 138 : | void processTransition(LLFSMTransition &transition, void* user_data); | ||
| 139 : | |||
| 140 : | // returns current state | ||
| 141 : | LLFSMState* getCurrentState() const; | ||
| 142 : | |||
| 143 : | // execute current state | ||
| 144 : | void runCurrentState(void *data); | ||
| 145 : | |||
| 146 : | // set state by state pointer | ||
| 147 : | BOOL setCurrentState(LLFSMState *initial_state, void* user_data, BOOL skip_entry = TRUE); | ||
| 148 : | |||
| 149 : | // set state by unique ID | ||
| 150 : | BOOL setCurrentState(U32 state_id, void* user_data, BOOL skip_entry = TRUE); | ||
| 151 : | }; | ||
| 152 : | |||
| 153 : | #endif //_LL_LLSTATEMACHINE_H |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

