Skip to content

Commit 48434de

Browse files
committed
Cleanup state entry handling
This also fixes a bug in entry handling in rare cases
1 parent 044bad9 commit 48434de

1 file changed

Lines changed: 15 additions & 21 deletions

File tree

include/cpp_event_framework/Statemachine.hxx

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)