SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
sm::StateMachine Class Referenceabstract

#include <StateMachine.h>

Inheritance diagram for sm::StateMachine:
[legend]

Public Member Functions

 StateMachine (unsigned int initialStateId)
 
virtual ~StateMachine ()
 
bool update ()
 process events and update current state More...
 
virtual std::string getPrintableState (unsigned int state)=0
 
- Public Member Functions inherited from sm::EventHandler
void addEvent (Event *e)
 

Protected Member Functions

template<class SM , class Data , void(SM::*)(const Data *, const bool, const bool) Func>
void registerState (unsigned int stateId)
 register state processing functions from deriving class More...
 

Private Attributes

unsigned int _currentStateId = 0
 
std::map< unsigned int, sm::StateBase * > _stateActions
 

Additional Inherited Members

- Protected Attributes inherited from sm::EventHandler
std::queue< Event * > _events
 

Detailed Description

  • Transfer id of initial state in constructor of StateMachine
  • Define state functions like: void <name>(const sm::EventData* data);
  • call registerState in constructor which maps a state function to an state id

Definition at line 78 of file StateMachine.h.

Constructor & Destructor Documentation

◆ StateMachine()

sm::StateMachine::StateMachine ( unsigned int  initialStateId)
explicit

Definition at line 20 of file StateMachine.cpp.

21  : _currentStateId(initialStateId)
22 {
23 }
unsigned int _currentStateId
Definition: StateMachine.h:103

◆ ~StateMachine()

sm::StateMachine::~StateMachine ( )
virtual

Definition at line 25 of file StateMachine.cpp.

26 {
27  for (auto it : _stateActions)
28  {
29  delete it.second;
30  }
31 };
std::map< unsigned int, sm::StateBase * > _stateActions
Definition: StateMachine.h:105

Member Function Documentation

◆ getPrintableState()

virtual std::string sm::StateMachine::getPrintableState ( unsigned int  state)
pure virtual

◆ registerState()

template<class SM , class Data , void(SM::*)(const Data *, const bool, const bool) Func>
void sm::StateMachine::registerState ( unsigned int  stateId)
inlineprotected

register state processing functions from deriving class

Definition at line 96 of file StateMachine.h.

97  {
98  assert(_stateActions.find(stateId) == _stateActions.end());
99  _stateActions[stateId] = new StateAction<SM, Data, Func>();
100  }

◆ update()

bool sm::StateMachine::update ( )

process events and update current state

Definition at line 33 of file StateMachine.cpp.

34 {
35  sm::EventData* data = nullptr;
36  bool stateEntry = false;
37  bool stateWasUpdated = false;
38  // invoke state action for every valid event, but at least once
39  while (!_events.empty())
40  {
41  Event* e = _events.front();
42  _events.pop();
43 
44  unsigned int newState = e->getNewState(_currentStateId);
45  data = e->getEventData();
46 
47  LOG_STATEMACHINE_DEBUG("Event %s received sent by %s",
48  e->name(),
49  e->senderInfo());
50 
51  if (newState != Event::EVENT_IGNORED)
52  {
53  if (_currentStateId != newState)
54  {
55  stateEntry = true;
56  LOG_STATEMACHINE_DEBUG("State change: %s -> %s",
58  getPrintableState(newState).c_str());
59 
60  // inform old state that we will leave it soon
61  auto itStateAction = _stateActions.find(_currentStateId);
62  if (itStateAction != _stateActions.end())
63  {
64  itStateAction->second->invokeStateAction(this,
65  data,
66  false,
67  true);
68  }
69  else
70  {
71  std::stringstream ss;
72  ss << "You forgot to register state "
74  << "!";
75  Utils::exitMsg("StateMachine",
76  ss.str().c_str(),
77  __LINE__,
78  __FILE__);
79  }
80 
81  // update state
82  _currentStateId = newState;
83  }
84  auto itStateAction = _stateActions.find(_currentStateId);
85  if (itStateAction != _stateActions.end())
86  {
87  itStateAction->second->invokeStateAction(this,
88  data,
89  stateEntry,
90  false);
91  }
92  else
93  {
94  std::stringstream ss;
95  ss << "You forgot to register state "
97  << "!";
98  Utils::exitMsg("StateMachine",
99  ss.str().c_str(),
100  __LINE__,
101  __FILE__);
102  }
103  stateWasUpdated = true;
104  }
105  else
106  {
107  LOG_STATEMACHINE_DEBUG("Event %s ignored in state %s",
108  e->name(),
110  }
111 
112  delete e;
113  }
114 
115  if (!stateWasUpdated)
116  _stateActions[_currentStateId]->invokeStateAction(this, data, stateEntry, false);
117 
118  // ATTENTION: data ownership is not transferred to state
119  delete data;
120 
121  return true;
122 }
#define LOG_STATEMACHINE_DEBUG(...)
std::queue< Event * > _events
Definition: EventHandler.h:18
@ EVENT_IGNORED
Definition: Event.h:36
virtual std::string getPrintableState(unsigned int state)=0
void exitMsg(const char *tag, const char *msg, const int line, const char *file)
Terminates the application with a message. No leak checking.
Definition: Utils.cpp:1135

Member Data Documentation

◆ _currentStateId

unsigned int sm::StateMachine::_currentStateId = 0
private

Definition at line 103 of file StateMachine.h.

◆ _stateActions

std::map<unsigned int, sm::StateBase*> sm::StateMachine::_stateActions
private

Definition at line 105 of file StateMachine.h.


The documentation for this class was generated from the following files: