Closed love1angel closed 1 year ago
#include <boost/mpl/list.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/state_machine.hpp>
#include <boost/statechart/custom_reaction.hpp>
#include <boost/statechart/transition.hpp>
#include <boost/statechart/deferral.hpp>
#include <iostream>
namespace sc = boost::statechart;
struct event_go : sc::event<event_go> { };
struct event_green : sc::event<event_green> { };
struct event_blue : sc::event<event_blue> { };
class InitialState;
class WorkingState;
class StateMachine : public sc::state_machine<StateMachine, InitialState> {
};
class InitialState : public sc::simple_state<InitialState, StateMachine> {
public:
using reactions = boost::mpl::list<sc::transition<event_go, WorkingState>, sc::deferral<event_green>, sc::deferral<event_blue>>;
};
class WorkingState : public sc::simple_state<WorkingState, StateMachine> {
public:
using reactions = boost::mpl::list < sc::custom_reaction<event_green>, sc::custom_reaction<event_blue>>;
sc::result react(const event_green& event)
{
std::cout << "WorkingState : event_green" << std::endl;
return this->discard_event();
}
sc::result react(const event_blue& event)
{
std::cout << "WorkingState : event_blue" << std::endl;
return this->discard_event();
}
void exit()
{
std::cout << "exit test" << std::endl;
}
};
int main()
{
StateMachine sm;
sm.initiate();
sm.process_event(event_blue{});
sm.process_event(event_blue{});
sm.process_event(event_green{});
sm.process_event(event_go{});
sm.terminate();
return 0;
}
output
WorkingState : event_blue
WorkingState : event_blue
WorkingState : event_green
exit test
I do think if aims at green hand, at least example illustrated should be right. right?
e.x.
namespace sc = boost::statechart;
struct event_go : sc::event { };
struct event_green : sc::event { };
struct event_blue : sc::event { };
class InitialState; class WorkingState;
class StateMachine : public sc::state_machine<StateMachine, InitialState> { };
class InitialState : public sc::simple_state<InitialState, StateMachine> { public: using reactions = boost::mpl::list<sc::transition<event_go, WorkingState>, sc::deferral, sc::deferral>;
};
class WorkingState : public sc::simple_state<WorkingState, StateMachine> { public: using reactions = boost::mpl::list < sc::custom_reaction, sc::custom_reaction>;
};
int main() { StateMachine sm; sm.initiate(); sm.process_event(event_blue{}); sm.process_event(event_blue{}); sm.process_event(event_green{}); sm.process_event(event_go{}); return 0; }
environment