How to give objects to states in a boost::statechart?

1.1k Views Asked by At

I am in the situation where I need to modify a state machine that I did not write. This state machine is written using Boost Statechart. Unfortunately, I find this code impenetrable, and the guy who did write it is on vacation.

The problem is simple, I think: I have a variable, thing, that I want to use in some of these states. It represents a singular application controller that needs to be informed of things. To what constructor (or whatever) can I give thing, making it available to states of the machine?

The states are declared as per this example:

struct Pumping : sc::state< Pumping, Purifier >
{
  Pumping( my_context ctx ) : my_base( ctx )
  {
    post_event( EvPumpingStarted() );
  }
  // ...
};

P.S. I would love a better title for this question; help appreciated.

1

There are 1 best solutions below

1
On

It sounds like thing should be passed to the machine constructor? If so, it's probably best to make it a data member of the machine. States can access the machine with the outermost_context() function. So, inside a state you'd write something like outermost_context().get_thing().