3

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.

Andres Jaan Tack
  • 22,566
  • 11
  • 59
  • 78

1 Answers1

5

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

  • For bonus points (if I can be permitted to hijack the answer?), how would you do this using `simple_state`s, which can't access the outer context from their constructor? (Would there be any point to attempting this, or should `state`s be used instead?) – drfrogsplat Jun 30 '11 at 06:07