Boost logo

Boost Users :

From: Oliver.Kowalke_at_[hidden]
Date: 2006-03-10 08:41:50


Hello,
I want to pass data from outside into the state machine and pass the
data from one state to the next state during state transition.
But it seams that statechart doesn't support this.
Could this feature be added? What is the preferred way which statechart
suggests?
With best regards,
Oliver

#include <iostream>
#include <stdexcept>

#include <boost/statechart/custom_reaction.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/state_machine.hpp>

namespace sc = boost::statechart;

struct ev_activate
: public sc::event< ev_activate >
{
        int data;

        ev_activate( int d) : data( d) {}
};

struct inactive;

struct sm
: public sc::state_machine<
        sm,
        inactive
>
{};

struct active
: public sc::simple_state<
        active,
        sm
>
{
        active( int i)
        { std::cout << "active(): " << i << std::endl; }
};

struct inactive
: public sc::simple_state<
        inactive,
        sm
>
{
        typedef sc::custom_reaction< ev_activate > reactions;

        sc::result react( ev_activate const& ev)
        {
                return transit< active >( ev.data); // passing data to
new state during transition
        }
};

int main()
{
        try
        {
                sm fsm;
                fsm.initiate();
                fsm.process_event( ev_activate( 1) ); // passing data
from outside

                return 0;
        }
        catch ( std::runtime_error const& e)
        { std::cerr << e.what() << std::endl; }
        catch (...)
        { std::cerr << "unhandled exception" << std::endl; }
        
        return -1;
}


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