digint/tinyfsm

Is it possible to query the current state of the State Machine?

Ricodan opened this issue · 4 comments

I'm, not too savvy with C++ templates and would like some help with this topic.
I would like to print a stribg for example with the state of my SM.

You can print the address of the current state:

std::cout << MyFsm::current_state_ptr << std::endl;

The address of the states can be printed like this:

std::cout << &MyFsm::state<StateA>() << std::endl;
std::cout << &MyFsm::state<StateB>() << std::endl;
[...]
einoj commented

How would you print the addresses of the states if they are classes instead, like in the elevator example?

einoj commented

I figured out how to do it, at least for the first class in fsm_list. If you move the state class declaration to the header file you can do it like this:

  std::cout << fsm_list::fsmtype::current_state_ptr << std::endl;
  std::cout << &fsm_list::fsmtype::state<Up>() << std::endl;

You can also check if the state machine is in a certain state using the is_in_state function:

bool in_state_a = MyFsm::is_in_state<StateA>();
bool in_state_b = MyFsm::is_in_state<StateB>();