PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcharacter/llstatemachine.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 147 lines | 77 code | 29 blank | 41 comment | 0 complexity | 14aab4f81562cda493c54871c6f7fc82 MD5 | raw file
  1/** 
  2 * @file llstatemachine.h
  3 * @brief LLStateMachine class header file.
  4 *
  5 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_LLSTATEMACHINE_H
 28#define LL_LLSTATEMACHINE_H
 29
 30#include <string>
 31
 32#include "llerror.h"
 33#include <map>
 34
 35class LLUniqueID
 36{
 37	friend bool	operator==(const LLUniqueID &a, const LLUniqueID &b);
 38	friend bool	operator!=(const LLUniqueID &a, const LLUniqueID &b);
 39protected:
 40	static U32	sNextID;
 41	U32			mId;
 42public:
 43	LLUniqueID(){mId = sNextID++;}
 44	virtual ~LLUniqueID(){}
 45	U32		getID() {return mId;}
 46};
 47
 48class LLFSMTransition : public LLUniqueID
 49{
 50public:
 51	LLFSMTransition() : LLUniqueID(){};
 52	virtual std::string getName()const { return "unnamed"; }
 53};
 54
 55class LLFSMState : public LLUniqueID
 56{
 57public:
 58	LLFSMState() : LLUniqueID(){};
 59	virtual void onEntry(void *){};
 60	virtual void onExit(void *){};
 61	virtual void execute(void *){};
 62	virtual std::string getName() const { return "unnamed"; }
 63};
 64
 65class LLStateDiagram
 66{
 67typedef std::map<LLFSMTransition*, LLFSMState*> Transitions;
 68
 69friend std::ostream& operator<<(std::ostream &s, LLStateDiagram &FSM);
 70friend class LLStateMachine;
 71
 72protected:
 73	typedef std::map<LLFSMState*, Transitions> StateMap;
 74	StateMap			mStates;
 75	Transitions			mDefaultTransitions;
 76	LLFSMState*			mDefaultState;
 77	BOOL				mUseDefaultState;
 78
 79public:
 80	LLStateDiagram();
 81	virtual ~LLStateDiagram();
 82
 83protected:
 84	// add a state to the state graph, executed implicitly when adding transitions
 85	BOOL addState(LLFSMState *state);
 86
 87	// add a directed transition between 2 states
 88	BOOL addTransition(LLFSMState& start_state, LLFSMState& end_state, LLFSMTransition& transition);
 89
 90	// add an undirected transition between 2 states
 91	BOOL addUndirectedTransition(LLFSMState& start_state, LLFSMState& end_state, LLFSMTransition& transition);
 92
 93	// add a transition that is taken if none other exist
 94	void addDefaultTransition(LLFSMState& end_state, LLFSMTransition& transition);
 95
 96	// process a possible transition, and get the resulting state
 97	LLFSMState* processTransition(LLFSMState& start_state, LLFSMTransition& transition);
 98
 99	// add a transition that exists for every state
100	void setDefaultState(LLFSMState& default_state);
101
102	// return total number of states with no outgoing transitions
103	S32 numDeadendStates();
104
105	// does this state exist in the state diagram?
106	BOOL stateIsValid(LLFSMState& state);
107
108	// get a state pointer by ID
109	LLFSMState* getState(U32 state_id);
110
111public:
112	// save the graph in a DOT file for rendering and visualization
113	BOOL saveDotFile(const std::string& filename);
114};
115
116class LLStateMachine
117{
118protected:
119	LLFSMState*			mCurrentState;
120	LLFSMState*			mLastState;
121	LLFSMTransition*	mLastTransition;
122	LLStateDiagram*		mStateDiagram;
123
124public:
125	LLStateMachine();
126	virtual ~LLStateMachine();
127
128	// set state diagram
129	void setStateDiagram(LLStateDiagram* diagram);
130
131	// process this transition
132	void processTransition(LLFSMTransition &transition, void* user_data);
133
134	// returns current state
135	LLFSMState*	getCurrentState() const;
136
137	// execute current state
138	void runCurrentState(void *data);
139
140	// set state by state pointer
141	BOOL setCurrentState(LLFSMState *initial_state, void* user_data, BOOL skip_entry = TRUE);
142
143	// set state by unique ID
144	BOOL setCurrentState(U32 state_id, void* user_data, BOOL skip_entry = TRUE);
145};
146
147#endif //_LL_LLSTATEMACHINE_H