diff --git a/Inc/ST-LIB_LOW/StateMachine/StateMachine.hpp b/Inc/ST-LIB_LOW/StateMachine/StateMachine.hpp index 519c4578c..7ae89590b 100644 --- a/Inc/ST-LIB_LOW/StateMachine/StateMachine.hpp +++ b/Inc/ST-LIB_LOW/StateMachine/StateMachine.hpp @@ -259,6 +259,12 @@ class IStateMachine { virtual void force_change_state(size_t state) = 0; virtual size_t get_current_state_id() const = 0; constexpr bool operator==(const IStateMachine&) const = default; + protected: + virtual void enter() = 0; + virtual void exit() = 0; + virtual void start() = 0; + template + friend class StateMachine; }; template @@ -327,14 +333,14 @@ class StateMachine : public IStateMachine { state.get_transitions().size()}; } - inline void enter() + inline void enter() override { auto& state = states[static_cast(current_state)]; state.enter(); } - inline void exit() + inline void exit() override { auto& state = states[static_cast(current_state)]; state.exit(); @@ -391,11 +397,25 @@ class StateMachine : public IStateMachine { if (t.predicate()) { exit(); + for(auto& nested : nested_state_machine) + { + if(nested.state == current_state){ + nested.machine->exit(); + break; + } + } #ifdef STLIB_ETH remove_state_orders(); #endif current_state = t.target; enter(); + for(auto& nested : nested_state_machine) + { + if(nested.state == current_state){ + nested.machine->enter(); + break; + } + } #ifdef STLIB_ETH refresh_state_orders(); #endif @@ -412,9 +432,16 @@ class StateMachine : public IStateMachine { } } - void start() + void start() override { enter(); + for(auto& nested : nested_state_machine) + { + if(nested.state == current_state){ + nested.machine->start(); + break; + } + } } @@ -428,11 +455,25 @@ class StateMachine : public IStateMachine { } exit(); + for(auto& nested : nested_state_machine) + { + if(nested.state == current_state){ + nested.machine->exit(); + break; + } + } #ifdef STLIB_ETH remove_state_orders(); #endif current_state = new_state; enter(); + for(auto& nested : nested_state_machine) + { + if(nested.state == current_state){ + nested.machine->enter(); + break; + } + } #ifdef STLIB_ETH refresh_state_orders(); #endif