Boost logo

Boost Users :

Subject: Re: [Boost-users] Initialize inner state machine from outer state machine
From: Christophe Henry (christophe.j.henry_at_[hidden])
Date: 2013-04-15 15:34:37


>Hi,
>

>I have shared data between states which are state machines themselves. I've stored the shared data in the outer state machine
>and wish to initialize each of the inner state machines with a reference to the shared data. Is this the "right way" to do this? How would I >do this?
>

>Here's an example of what I'm doing:
>

>struct Outer_ : state_machine_def<Outer_> {
>int foo;
>struct Inner_ : state_machine_def<Inner_> {
>int& foo;
>Inner_(int& f) : foo(f) {}
>...
>}
>...
>}
>

>How do I get the inner state machines constructor called with data from the outer state machine? Or is this just the wrong way to do >things? Thanks

Hi,

sorry for my late answer. There are 2 ways to do this:
- Constructor with arguments (http://svn.boost.org/svn/boost/trunk/libs/msm/doc/HTML/ch03s05.html#d0e2502).
- add a default-ctor to Inner_, then after constructing your fsm, set the data (supposing foo would be a pointer):

Outer outer;
outer.get_state<Outer_::Inner&>().foo = outer.foo;

Alternatively, you can do this in the on_entry of Outer:
template <class Event,class FSM>
void on_entry(Event const&,FSM& fsm)
{
   // fsm is for Outer as we are in on_entry of Outer
   fsm.template get_state<Outer_::Inner&>().foo = fsm.foo;
}

HTH,
Christophe



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net