0

I want to share something among states, for example, a mode variable. I save it to the state machine. However, I can not even access its value.

struct InitState;
struct MyFsm : boost::statechart::state_machine<MyFsm, InitState> {
  uint8_t mode;

  MyFsm() : mode(0) {
    std::cout << "mode = " << mode << std::endl;
  }
};

The above ctor never prints the value.

struct InitState : boost::statechart::simple_state<InitState, MyFsm > {
  typedef boost::mpl::list<
    boost::statechart::custom_reaction<Event1>
  > reactions;

  boost::statechart::result react(const Event1& e) {
     context< MyFsm >().mode = 2; // This does NOT work!
  }
};

The assignment also fails above. Does anyone know how to get it working?

Or maybe what is the best way to share variables among states?

Or if boost::statechart is too bad to use? Boost's doc is so useless on explaining its APIs.

Gabriel
  • 237
  • 3
  • 9
  • The first code snippet does not seep to be valid, because `PresentationControlFsm` constructor name does not match class name. – user7860670 Dec 04 '17 at 08:08
  • sharing data between states (but not all states) is always a problematic. Ideally you'll want a state to manage the shared data and then two substates of that state, no? Otherwise you could pass a pointer to shared data between states – Richard Hodges Dec 04 '17 at 08:11
  • Sorry for the naming issue. Even with the ctor fixed, the mode still can't be accessed. – Gabriel Dec 04 '17 at 18:06

0 Answers0