SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
StateMachine.h
Go to the documentation of this file.
1 /**
2  * \file StateMachinge.h
3  * \brief State Machine Class Declaration
4  * \authors Michael Göttlicher
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8  */
9 
10 #ifndef SM_STATE_MACHINE_H
11 #define SM_STATE_MACHINE_H
12 
13 #include <sm/EventData.h>
14 #include <sm/EventHandler.h>
15 #include <Utils.h>
16 #include <cassert>
17 
18 namespace sm
19 {
20 //-----------------------------------------------------------------------------
21 class StateMachine;
22 //-----------------------------------------------------------------------------
23 /// @brief Abstract state base class that all states inherit from.
24 class StateBase
25 {
26 public:
27  virtual ~StateBase() { ; }
28 
29  /*!
30  * Called by the state machine to execute a state action.
31  * \param sm A state machine instance
32  * \param data The event data
33  * \param stateEntry
34  * \param stateExit
35  */
37  const EventData* data,
38  const bool stateEntry,
39  const bool stateExit) const {};
40 };
41 //-----------------------------------------------------------------------------
42 /*!
43  * StateAction takes three template arguments: A state machine class,
44  * a state function event data type (derived from EventData) and a state machine
45  * member function pointer.
46  * \tparam SM
47  * \tparam Data
48  * \tparam Func
49  */
50 template<class SM,
51  class Data,
52  void (SM::*Func)(const Data*,
53  const bool,
54  const bool)>
55 class StateAction : public StateBase
56 {
57 public:
59  const EventData* data,
60  const bool stateEntry,
61  const bool stateExit) const
62  {
63  // Downcast the state machine and event data to the correct derived type
64  SM* derivedSM = static_cast<SM*>(sm);
65 
66  const Data* derivedData = dynamic_cast<const Data*>(data);
67 
68  // Call the state function
69  (derivedSM->*Func)(derivedData, stateEntry, stateExit);
70  }
71 };
72 //-----------------------------------------------------------------------------
73 /*!
74 - Transfer id of initial state in constructor of StateMachine
75 - Define state functions like: void <name>(const sm::EventData* data);
76 - call registerState in constructor which maps a state function to an state id
77 */
78 class StateMachine : public EventHandler
79 {
80 public:
81  explicit StateMachine(unsigned int initialStateId);
82  virtual ~StateMachine();
83 
84  //! process events and update current state
85  bool update();
86 
87  virtual std::string getPrintableState(unsigned int state) = 0;
88 
89 protected:
90  //! register state processing functions from deriving class
91  template<class SM,
92  class Data,
93  void (SM::*Func)(const Data*,
94  const bool,
95  const bool)>
96  void registerState(unsigned int stateId)
97  {
98  assert(_stateActions.find(stateId) == _stateActions.end());
99  _stateActions[stateId] = new StateAction<SM, Data, Func>();
100  }
101 
102 private:
103  unsigned int _currentStateId = 0;
104 
105  std::map<unsigned int, sm::StateBase*> _stateActions;
106 };
107 //-----------------------------------------------------------------------------
108 } // namespace SM
109 
110 #endif
Event class used in the state machine.
typedef void(SL_STDCALL *cbOnImGuiBuild)(SLScene *s
Callback function typedef for ImGui build function.
virtual void invokeStateAction(StateMachine *sm, const EventData *data, const bool stateEntry, const bool stateExit) const
Definition: StateMachine.h:58
Abstract state base class that all states inherit from.
Definition: StateMachine.h:25
virtual void invokeStateAction(StateMachine *sm, const EventData *data, const bool stateEntry, const bool stateExit) const
Definition: StateMachine.h:36
virtual ~StateBase()
Definition: StateMachine.h:27
void registerState(unsigned int stateId)
register state processing functions from deriving class
Definition: StateMachine.h:96
virtual std::string getPrintableState(unsigned int state)=0
std::map< unsigned int, sm::StateBase * > _stateActions
Definition: StateMachine.h:105
Collection of classes for a state machine implementation used in the Erleb-AR app.
Definition: Event.h:25