StateMachine.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-05
  6. * Changed: 2021-05-01
  7. *
  8. * */
  9. #ifndef LS_STD_STATE_MACHINE_HPP
  10. #define LS_STD_STATE_MACHINE_HPP
  11. #include <memory>
  12. #include <unordered_map>
  13. #include <string>
  14. #include <vector>
  15. #include <ls_std/base/Class.hpp>
  16. #include "State.hpp"
  17. #include "StateMachineTypes.hpp"
  18. namespace ls_std
  19. {
  20. class StateMachine : public ls_std::Class
  21. {
  22. public:
  23. explicit StateMachine(const std::string& _name);
  24. ~StateMachine() override = default;
  25. bool addState(const std::shared_ptr<ls_std::State> &_state);
  26. std::shared_ptr<ls_std::State> getCurrentState();
  27. std::vector<ls_std::StateId> getMemory();
  28. std::string getName();
  29. std::unordered_map<StateId, std::shared_ptr<ls_std::State>> getStates();
  30. bool hasState(const ls_std::StateId &_id);
  31. bool proceed();
  32. void setMemory(const std::vector<ls_std::StateId>& _memory);
  33. void setName(const std::string& _name);
  34. bool setStartState(const ls_std::StateId &_id);
  35. private:
  36. std::shared_ptr<State> currentState{};
  37. std::vector<ls_std::StateId> memory{};
  38. std::string name{};
  39. std::unordered_map<ls_std::StateId, std::shared_ptr<ls_std::State>> states{};
  40. void _assignMemory(const std::vector<ls_std::StateId>& _memory);
  41. void _assignName(const std::string& _name);
  42. std::vector<ls_std::StateId> _getNextValidStates();
  43. void _remember(const ls_std::StateId &_id);
  44. bool _hasState(const ls_std::StateId &_id);
  45. };
  46. }
  47. #endif