PageRenderTime 40ms CodeModel.GetById 18ms 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
Possible License(s): LGPL-2.1
  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. #ifndef LL_LLSTATEMACHINE_H
  27. #define LL_LLSTATEMACHINE_H
  28. #include <string>
  29. #include "llerror.h"
  30. #include <map>
  31. class LLUniqueID
  32. {
  33. friend bool operator==(const LLUniqueID &a, const LLUniqueID &b);
  34. friend bool operator!=(const LLUniqueID &a, const LLUniqueID &b);
  35. protected:
  36. static U32 sNextID;
  37. U32 mId;
  38. public:
  39. LLUniqueID(){mId = sNextID++;}
  40. virtual ~LLUniqueID(){}
  41. U32 getID() {return mId;}
  42. };
  43. class LLFSMTransition : public LLUniqueID
  44. {
  45. public:
  46. LLFSMTransition() : LLUniqueID(){};
  47. virtual std::string getName()const { return "unnamed"; }
  48. };
  49. class LLFSMState : public LLUniqueID
  50. {
  51. public:
  52. LLFSMState() : LLUniqueID(){};
  53. virtual void onEntry(void *){};
  54. virtual void onExit(void *){};
  55. virtual void execute(void *){};
  56. virtual std::string getName() const { return "unnamed"; }
  57. };
  58. class LLStateDiagram
  59. {
  60. typedef std::map<LLFSMTransition*, LLFSMState*> Transitions;
  61. friend std::ostream& operator<<(std::ostream &s, LLStateDiagram &FSM);
  62. friend class LLStateMachine;
  63. protected:
  64. typedef std::map<LLFSMState*, Transitions> StateMap;
  65. StateMap mStates;
  66. Transitions mDefaultTransitions;
  67. LLFSMState* mDefaultState;
  68. BOOL mUseDefaultState;
  69. public:
  70. LLStateDiagram();
  71. virtual ~LLStateDiagram();
  72. protected:
  73. // add a state to the state graph, executed implicitly when adding transitions
  74. BOOL addState(LLFSMState *state);
  75. // add a directed transition between 2 states
  76. BOOL addTransition(LLFSMState& start_state, LLFSMState& end_state, LLFSMTransition& transition);
  77. // add an undirected transition between 2 states
  78. BOOL addUndirectedTransition(LLFSMState& start_state, LLFSMState& end_state, LLFSMTransition& transition);
  79. // add a transition that is taken if none other exist
  80. void addDefaultTransition(LLFSMState& end_state, LLFSMTransition& transition);
  81. // process a possible transition, and get the resulting state
  82. LLFSMState* processTransition(LLFSMState& start_state, LLFSMTransition& transition);
  83. // add a transition that exists for every state
  84. void setDefaultState(LLFSMState& default_state);
  85. // return total number of states with no outgoing transitions
  86. S32 numDeadendStates();
  87. // does this state exist in the state diagram?
  88. BOOL stateIsValid(LLFSMState& state);
  89. // get a state pointer by ID
  90. LLFSMState* getState(U32 state_id);
  91. public:
  92. // save the graph in a DOT file for rendering and visualization
  93. BOOL saveDotFile(const std::string& filename);
  94. };
  95. class LLStateMachine
  96. {
  97. protected:
  98. LLFSMState* mCurrentState;
  99. LLFSMState* mLastState;
  100. LLFSMTransition* mLastTransition;
  101. LLStateDiagram* mStateDiagram;
  102. public:
  103. LLStateMachine();
  104. virtual ~LLStateMachine();
  105. // set state diagram
  106. void setStateDiagram(LLStateDiagram* diagram);
  107. // process this transition
  108. void processTransition(LLFSMTransition &transition, void* user_data);
  109. // returns current state
  110. LLFSMState* getCurrentState() const;
  111. // execute current state
  112. void runCurrentState(void *data);
  113. // set state by state pointer
  114. BOOL setCurrentState(LLFSMState *initial_state, void* user_data, BOOL skip_entry = TRUE);
  115. // set state by unique ID
  116. BOOL setCurrentState(U32 state_id, void* user_data, BOOL skip_entry = TRUE);
  117. };
  118. #endif //_LL_LLSTATEMACHINE_H