123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- // 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
|