You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1022B

  1. #include "StateMachine.hpp"
  2. unsigned int const StateMachine::NoOp = std::numeric_limits<unsigned int>::max();
  3. void StateMachine::noOp(StateMachine&)
  4. {
  5. }
  6. StateMachine::StateMachine()
  7. {
  8. m_states[NoOp] = &StateMachine::noOp;
  9. m_currentIndex = NoOp;
  10. }
  11. void StateMachine::addState(unsigned int index, State&& state)
  12. {
  13. m_states[index] = std::move(state);
  14. }
  15. void StateMachine::addStateBegin(unsigned index, Notification&& state)
  16. {
  17. m_begins[index] = std::move(state);
  18. }
  19. void StateMachine::addStateEnd(unsigned index, Notification&& state)
  20. {
  21. m_ends[index] = std::move(state);
  22. }
  23. void StateMachine::change(unsigned int index)
  24. {
  25. auto const it = m_states.find(index);
  26. assert (it != m_states.end());
  27. auto const beginIt = m_begins.find(index);
  28. auto const endIt = m_ends.find(m_currentIndex);
  29. if (endIt != m_ends.end())
  30. {
  31. endIt->second();
  32. }
  33. m_currentState = it->second;
  34. m_currentIndex = index;
  35. if (beginIt != m_begins.end())
  36. {
  37. beginIt->second();
  38. }
  39. }
  40. void StateMachine::step()
  41. {
  42. m_currentState(*this);
  43. }