@@ -770,42 +770,36 @@ private:
770770
771771 void EnterStatesFromDownToRecursive (StatePtr top, StatePtr target)
772772 {
773- if (top != target)
773+ // Don't enter topmost state
774+ if ((target != nullptr ) && (top != target))
774775 {
775- if ((target->parent_ != nullptr ) && (target->parent_ != top))
776- {
777- EnterStatesFromDownTo (top, target->parent_ );
778- }
776+ EnterStatesFromDownToRecursive (top, target->parent_ );
779777 EnterState (*target);
780778 }
781779 }
782780
783781 void EnterStatesFromDownTo (StatePtr top, StatePtr target)
784782 {
783+ // Enter all states up to parent
785784 if (top != target)
786785 {
787786 EnterStatesFromDownToRecursive (top, target->parent_ );
788787 }
789788
790- if (GetInitialState (target) != nullptr )
791- {
792- EnterState (*target);
789+ // Alywas enter target state (we may have exited it, possibly a self transition)
790+ EnterState (*target);
793791
794- const auto * state = GetInitialState (target);
795- const auto * current_target = state;
796- while (state != nullptr )
797- {
798- current_target = state;
799- EnterState (*state);
800- state = GetInitialState (state);
801- }
802- current_state_ = current_target;
803- }
804- else
792+ // Is target a hierarchical state? If so, enter initial state
793+ const auto * state = GetInitialState (target);
794+ while (state != nullptr )
805795 {
806- EnterState (*target);
807- current_state_ = target;
796+ target = state;
797+ EnterState (*state);
798+ state = GetInitialState (state);
808799 }
800+
801+ // We have reached the target state
802+ current_state_ = target;
809803 }
810804};
811805
0 commit comments