1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// Copyright 2010 Christophe Henry
// henry UNDERSCORE christophe AT hotmail DOT com
// This is an extended version of the state machine available in the boost::mpl library
// Distributed under the same license as the original.
// Copyright for the original version:
// Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
// under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef PLAYING_MODE_HPP
#define PLAYING_MODE_HPP
#include <iostream>
#include <boost/any.hpp>
#define FUSION_MAX_VECTOR_SIZE 20
#include "Events.hpp"
#include <boost/msm/back/favor_compile_time.hpp>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/euml/euml.hpp>
using namespace std;
namespace msm = boost::msm;
namespace euml = boost::msm::front::euml;
struct PlayingMode_ : public msm::front::state_machine_def<PlayingMode_>
{
//flags
struct NoFastFwd{};
struct Playing : public msm::front::state<default_base_state,msm::front::sm_ptr>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& )
{
std::cout << "starting: PlayingMode::Playing" << std::endl;
std::cout << "playing song:" << m_fsm->get_current_song() << std::endl;
}
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::Playing" << std::endl;}
void set_sm_ptr(PlayingMode_* pl)
{
m_fsm = pl;
}
private:
PlayingMode_* m_fsm;
};
struct Paused : public msm::front::state<>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::Paused" << std::endl;}
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::Paused" << std::endl;}
};
struct WaitingForNextPrev : public msm::front::state<>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::WaitingForNextPrev" << std::endl;}
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::WaitingForNextPrev" << std::endl;}
};
struct WaitingForEnd : public msm::front::state<>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::WaitingForEnd" << std::endl;}
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::WaitingForEnd" << std::endl;}
};
struct NoForward : public msm::front::state<>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::NoForward" << std::endl;}
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::NoForward" << std::endl;}
};
struct ForwardPressed : public msm::front::state<>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& )
{
std::cout << "starting: PlayingMode::ForwardPressed," << "start timer" << std::endl;
}
template <class Event,class FSM>
void on_exit(Event const&,FSM& )
{
std::cout << "finishing: PlayingMode::ForwardPressed," << "stop timer" << std::endl;
}
};
struct FastForward : public msm::front::state<>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& )
{
std::cout << "starting: PlayingMode::FastForward," << "start timer" << std::endl;
}
template <class Event,class FSM>
void on_exit(Event const&,FSM& )
{
std::cout << "finishing: PlayingMode::FastForward," << "stop timer" << std::endl;
}
};
struct StdDisplay : public msm::front::state<>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::StdDisplay" << std::endl;}
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::StdDisplay" << std::endl;}
};
struct SetPosition : public msm::front::state<>
{
typedef mpl::vector1<NoFastFwd> flag_list;
template <class Event,class FSM>
void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::SetPosition" << std::endl;}
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::SetPosition" << std::endl;}
};
struct SetMark : public msm::front::state<>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::SetMark" << std::endl;}
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::SetMark" << std::endl;}
};
struct PlayingExit : public msm::front::exit_pseudo_state<EndPlay>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::PlayingExit" << std::endl;}
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::PlayingExit" << std::endl;}
};
// transition action methods
struct inc_song_counter : euml::euml_action<inc_song_counter>
{
template <class FSM,class EVT,class SourceState,class TargetState>
void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
{
if (++fsm.m_SongIndex <= fsm.m_NumberOfSongs )
{
std::cout << "playing song:" << fsm.m_SongIndex << std::endl;
}
else
{
// last song => end playing, next play will start at the beginning
fsm.m_SongIndex = 1;
fsm.process_event(EndPlay());
}
}
};
void select_song(StartSong const& evt)
{
if ((evt.m_Selected>0) && (evt.m_Selected<=m_NumberOfSongs))
{
m_SongIndex = evt.m_Selected;
std::cout << "selecting song:" << m_SongIndex << std::endl;
}
else
{
// play current song
std::cout << "selecting song:" << m_SongIndex << std::endl;
}
}
struct dec_song_counter : euml::euml_action<dec_song_counter>
{
template <class FSM,class EVT,class SourceState,class TargetState>
void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
{
if (--fsm.m_SongIndex >0 )
{
std::cout << "playing song:" << fsm.m_SongIndex << std::endl;
}
else
{
// before first song => end playing
fsm.m_SongIndex = 1;
fsm.process_event(EndPlay());
}
}
};
struct send_NextSong : euml::euml_action<send_NextSong>
{
template <class FSM,class EVT,class SourceState,class TargetState>
void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
{
fsm.process_event(NextSong());
}
};
void do_fast_forward(ForwardTimer const&)
{
std::cout << "moving song forward..." << std::endl;
}
// transition guard methods
struct fast_fwd_ok : euml::euml_action<fast_fwd_ok>
{
template <class FSM,class EVT,class SourceState,class TargetState>
bool operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
{
// guard accepts only if fast forward is possible (No SetPosition mode)
return !fsm.is_flag_active<NoFastFwd>();
}
};
// initial states / orthogonal zones
typedef mpl::vector5<Playing,WaitingForNextPrev,WaitingForEnd,NoForward,StdDisplay>
initial_state;
typedef PlayingMode_ fsm; // makes transition table cleaner
// Transition table for player
struct transition_table : mpl::vector19<
// Start Event Next Action Guard
// +--------------------+---------------------+--------------------+--------------------------+----------------------+
_row < Playing , PlayPause , Paused >,
_row < Playing , Off , Paused >,
a_row < Playing , StartSong , Playing , &fsm::select_song >,
_row < Paused , PlayPause , Playing >,
msm::front::Row < Playing , SongFinished , Playing , inc_song_counter , msm::front::none >,
a_row < Paused , StartSong , Playing , &fsm::select_song >,
// +--------------------+---------------------+--------------------+--------------------------+----------------------+
msm::front::Row < WaitingForNextPrev , PreviousSong , WaitingForNextPrev , dec_song_counter , msm::front::none >,
msm::front::Row < WaitingForNextPrev , NextSong , WaitingForNextPrev , inc_song_counter , msm::front::none >,
// +--------------------+---------------------+--------------------+--------------------------+----------------------+
_row < WaitingForEnd , EndPlay , PlayingExit >,
// +--------------------+---------------------+--------------------+--------------------------+----------------------+
msm::front::Row < NoForward , EastPressed , ForwardPressed , msm::front::none , fast_fwd_ok >,
msm::front::Row < ForwardPressed , EastReleased , NoForward , send_NextSong , msm::front::none >,
a_row < ForwardPressed , ForwardTimer , FastForward , &fsm::do_fast_forward >,
a_row < FastForward , ForwardTimer , FastForward , &fsm::do_fast_forward >,
_row < FastForward , EastReleased , NoForward >,
// +--------------------+---------------------+---------------------+--------------------------+----------------------+
_row < StdDisplay , PlayingMiddleButton , SetPosition >,
_row < SetPosition , StartSong , StdDisplay >,
_row < SetPosition , PlayingMiddleButton , SetMark >,
_row < SetMark , PlayingMiddleButton , StdDisplay >,
_row < SetMark , StartSong , StdDisplay >
// +--------------------+---------------------+---------------------+--------------------------+----------------------+
> {};
PlayingMode_():
m_SongIndex(1),
// for simplicity we decide there are 5 songs
m_NumberOfSongs(5){}
int get_current_song(){return m_SongIndex;}
int m_SongIndex;
int m_NumberOfSongs;
};
typedef msm::back::state_machine<PlayingMode_> PlayingMode;
#endif // PLAYING_MODE_HPP
|