Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost statechart pass arguments with transition

I'm trying to learn boost::statechart.

I want to make a little app which loads a file.

//  --------------------------------
// |                                |
// |           O     Project        |
// |           |                    |
// |           v                    |
// |  ----------------------------  |
// | |                            | |
// | |         Unloaded           | |
// |  ----------------------------  |
// |  |              ^              |
// |  | EvLoad       | EvUnload     |<-----O
// |  v              |              |
// |  ----------------------------  |
// | |                            | |
// | |         Loaded             | |
// |  ----------------------------  |
// |           |   ^                |
// |           |   | EvLoad         |
// |           -----                |
//  --------------------------------

But how do i transport arguments to the state, e.g. a filename? If i store the filename inside EvLoad i can access it easily for the in state reaction

struct Loaded : sc::simple_state< Loaded, Project>
{
    typedef sc::custom_reaction< EvLoad > reactions;
    sc::result react( const EvLoad & e )
    {
        //load file e.path()
        ...
        return discard_event();
    }
}

But when I'm in the Unloaded state then I'm invoking the constructor of Loaded and i can't pass arguments to it. The only workaround I came up with is reposting the event before doing the transition, but this looks a bit dirty to me.

struct Unloaded : sc::simple_state< Unloaded, Project >
{
    typedef sc::custom_reaction< EvLoad > reactions;
     sc::result react( const EvLoad & e )
     {
         post_event( e ); //workaround to pass the event to the loaded state
         return transit<Loaded>();
     }
};

Is there a better alternative?

like image 536
P3trus Avatar asked Dec 09 '22 06:12

P3trus


1 Answers

We use the triggering_event method to pull the triggering event, and then just attach the data as member variables to triggering event. It saves a lot of coding effort and keeps us from having to generate custom reactions or attaching transition variables to the statechart (two common approaches I've seen).

like image 81
trs8088 Avatar answered Dec 23 '22 05:12

trs8088