tkem / fsmlite

Lightweight finite state machine framework for C++11
MIT License
161 stars 25 forks source link

Feature: adding a set function in fsm.h to set the current state #26

Closed yabbasi31415 closed 4 years ago

yabbasi31415 commented 4 years ago

Added a setter function to the fsm class to set the current state asynchronously in the user application code.

tkem commented 4 years ago

State transitions should only be triggered by events, not by simply writing to a member variable. This is a key principle of fsmlite's design.

yabbasi31415 commented 4 years ago

@tkem the motivation behind adding a setter function is that we want to use a single state machine for multiple objects. Each object stores it's last known state in a data base; and whenever needed, it can fetch that state, feed it into the state machine and continue with the state transitions.

tkem commented 4 years ago

@yabbasi31415: So you kind-of want to create/initialize a new state machine for each object with a given state, but want to re-use a state machine instance (to preserve memory or for whatever reasons)? What about this (using the player state machine from the tests):

    player p;
    assert(p.current_state() == player::Empty);
    p = player(player::Playing);
    assert(p.current_state() == player::Playing);

IMHO this is somewhat cleaner and more explicit than setting "just" the state, especially if your derived FSM also introduces other member variables that need to be taken care of.