1
// Copyright 2010 Christophe Henry
2
// henry UNDERSCORE christophe AT hotmail DOT com
3
// This is an extended version of the state machine available in the boost::mpl library
4
// Distributed under the same license as the original.
5
// Copyright for the original version:
6
// Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
7
// under the Boost Software License, Version 1.0. (See accompanying
8
// file LICENSE_1_0.txt or copy at
9
// http://www.boost.org/LICENSE_1_0.txt)
14
#include <boost/msm/back/state_machine.hpp>
15
#include <boost/msm/front/euml/euml.hpp>
18
using namespace boost::msm::front::euml;
19
namespace msm = boost::msm;
21
// how long the timer will ring when countdown elapsed.
22
#define RINGING_TIME 5
24
namespace // Concrete FSM implementation
27
BOOST_MSM_EUML_DECLARE_ATTRIBUTE(int,m_timer)
28
BOOST_MSM_EUML_ATTRIBUTES((attributes_ << m_timer ), start_timer_attr)
29
BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(start_timer,start_timer_attr)
31
BOOST_MSM_EUML_EVENT(stop_timer)
33
BOOST_MSM_EUML_DECLARE_ATTRIBUTE(int,m_tick)
34
BOOST_MSM_EUML_ATTRIBUTES((attributes_ << m_tick ), tick_attr)
35
BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(tick,tick_attr)
37
BOOST_MSM_EUML_EVENT(start_ringing)
39
// Concrete FSM implementation
41
// The list of FSM states
42
BOOST_MSM_EUML_ACTION(Stopped_Entry)
44
template <class Event,class FSM,class STATE>
45
void operator()(Event const&,FSM&,STATE& )
47
std::cout << "entering: Stopped" << std::endl;
50
BOOST_MSM_EUML_STATE(( Stopped_Entry ),Stopped)
52
BOOST_MSM_EUML_ACTION(Started_Entry)
54
template <class Event,class FSM,class STATE>
55
void operator()(Event const&,FSM&,STATE& )
57
std::cout << "entering: Started" << std::endl;
61
BOOST_MSM_EUML_DECLARE_ATTRIBUTE(int,m_counter)
62
BOOST_MSM_EUML_STATE(( Started_Entry,
64
attributes_ << m_counter
68
BOOST_MSM_EUML_ACTION(Ringing_Entry)
70
template <class Event,class FSM,class STATE>
71
void operator()(Event const&,FSM&,STATE& )
73
std::cout << "entering: Ringing" << std::endl;
76
BOOST_MSM_EUML_DECLARE_ATTRIBUTE(int,m_ringing_cpt)
77
BOOST_MSM_EUML_STATE(( Ringing_Entry,
79
attributes_ << m_ringing_cpt
84
void do_ring(int ringing_time) {std::cout << "ringing " << ringing_time << " s" << std::endl;}
85
// create functor and eUML function
86
BOOST_MSM_EUML_FUNCTION(Ring_ , do_ring , ring_ , void , void )
88
// replaces the old transition table
89
BOOST_MSM_EUML_TRANSITION_TABLE((
90
// +------------------------------------------------------------------------------+
91
// When we start the countdown, the countdown value is not hardcoded but contained in the start_timer event.
92
// We copy this value into Started
93
Started == Stopped + start_timer /(target_(m_counter)= event_(m_timer)) ,
94
Stopped == Started + stop_timer ,
95
// internal transition
97
// we here use the message queue to move to Started when the countdown is finished
98
// to do this we put start_ringing into the message queue
99
/ if_then_( (source_(m_counter) -= event_(m_tick) ) <= Int_<0>(),
100
process_(start_ringing) ) ,
101
// when we start ringing, we give to the state its hard-coded ringing time.
102
Ringing == Started + start_ringing
103
/ (target_(m_ringing_cpt) = Int_<RINGING_TIME>(),
104
// call the external do_ring function
105
ring_(Int_<RINGING_TIME>())) ,
106
// to change a bit, we now do not use the message queue but a transition conflict to solve the same problem.
107
// When tick is fired, we have an internal transition Ringing -> Ringing, as long as Counter > 0
108
Ringing + tick [ source_(m_ringing_cpt) - event_(m_tick) > Int_<0>() ]
109
/(target_(m_ringing_cpt) -= event_(m_tick) ) ,
110
// And we move to Stopped when the counter is 0
111
Stopped == Ringing + tick[source_(m_ringing_cpt)-event_(m_tick) <= Int_<0>()] ,
112
// we let the user manually stop the ringing by pressing any button
113
Stopped == Ringing + stop_timer ,
114
Stopped == Ringing + start_timer
115
// +------------------------------------------------------------------------------+
118
// create a state machine "on the fly"
119
BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
120
init_ << Stopped // Init State
122
SimpleTimer_) //fsm name
124
// choice of back-end
125
typedef msm::back::state_machine<SimpleTimer_> SimpleTimer;
128
// Testing utilities.
130
static char const* const state_names[] = { "Stopped", "Started","Ringing" };
131
void pstate(SimpleTimer const& p)
133
std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
139
// needed to start the highest-level SM. This will call on_entry and mark the start of the SM
142
p.process_event(start_timer(5));pstate(p); //timer set to 5 ticks
143
p.process_event(tick(2));pstate(p);
144
p.process_event(tick(1));pstate(p);
145
p.process_event(tick(1));pstate(p);
146
p.process_event(tick(1));pstate(p);
147
// we are now ringing, let it ring a bit
148
p.process_event(tick(2));pstate(p);
149
p.process_event(tick(1));pstate(p);
150
p.process_event(tick(1));pstate(p);
151
p.process_event(tick(1));pstate(p);