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.

41 lines
851B

  1. #if!defined STATEMACHINE_HPP
  2. #define STATEMACHINE_HPP
  3. #include <memory>
  4. #include <map>
  5. #include <limits>
  6. #include <functional>
  7. #include <rack.hpp>
  8. class StateMachine
  9. {
  10. public:
  11. using State = std::function<void(StateMachine&)>;
  12. using Notification = std::function<void()>;
  13. static unsigned int const NoOp;
  14. static void noOp(StateMachine&);
  15. StateMachine();
  16. void addState(unsigned int index, State&& state);
  17. void addStateBegin(unsigned index, Notification&& state);
  18. void addStateEnd(unsigned index, Notification&& state);
  19. void change(unsigned int index);
  20. void step();
  21. unsigned int currentIndex()const
  22. {
  23. return m_currentIndex;
  24. }
  25. private:
  26. std::map<unsigned int, State> m_states;
  27. std::map<unsigned int, Notification> m_begins;
  28. std::map<unsigned int, Notification> m_ends;
  29. State m_currentState;
  30. unsigned int m_currentIndex;
  31. };
  32. #endif