tinyfsm icon indicating copy to clipboard operation
tinyfsm copied to clipboard

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

Open Ricodan opened this issue 6 years ago • 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.

Ricodan avatar May 24 '19 15:05 Ricodan

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;
[...]

digint avatar May 25 '19 20:05 digint

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

einoj avatar Jun 26 '20 14:06 einoj

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;

einoj avatar Jul 10 '20 14:07 einoj

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>();

lFatality avatar Jan 07 '21 15:01 lFatality