2
在boost :: msm教程中,有一個例子顯示了我們如何檢查當前狀態。boost中的狀態ID :: msm
// Transition table for player
struct transition_table : mpl::vector<
// Start Event Next Action Guard
// +---------+-------------+---------+---------------------+----------------------+
a_row < Stopped , play , Playing , &p::start_playback >,
a_row < Stopped , open_close , Open , &p::open_drawer >,
a_row < Stopped , stop , Stopped , &p::stopped_again >,
// +---------+-------------+---------+---------------------+----------------------+
a_row < Open , open_close , Empty , &p::close_drawer >,
// +---------+-------------+---------+---------------------+----------------------+
a_row < Empty , open_close , Open , &p::open_drawer >,
a_row < Empty , cd_detected , Stopped , &p::store_cd_info >,
// +---------+-------------+---------+---------------------+----------------------+
a_row < Playing , stop , Stopped , &p::stop_playback >,
a_row < Playing , pause , Paused , &p::pause_playback >,
a_row < Playing , open_close , Open , &p::stop_and_open >,
// +---------+-------------+---------+---------------------+----------------------+
a_row < Paused , end_pause , Playing , &p::resume_playback >,
a_row < Paused , stop , Stopped , &p::stop_playback >,
a_row < Paused , open_close , Open , &p::stop_and_open >,
// +---------+-------------+---------+---------------------+----------------------+
a_row < AllOk , error_found ,ErrorMode, &p::report_error >,
a_row <ErrorMode,end_error ,AllOk , &p::report_end_error >
// +---------+-------------+---------+---------------------+----------------------+
> {};
// Replaces the default no-transition response.
template <class FSM,class Event>
void no_transition(Event const& e, FSM&,int state)
{
std::cout << "no transition from state " << state
<< " on event " << typeid(e).name() << std::endl;
}
};
// Pick a back-end
typedef msm::back::state_machine<player_> player;
//
// Testing utilities.
//
static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused","AllOk","ErrorMode" };
void pstate(player const& p)
{
// we have now several active states, which we show
for (unsigned int i=0;i<player::nr_regions::value;++i)
{
std::cout << " -> " << state_names[p.current_state()[i]] << std::endl;
}
}
作者使用數組state_names
。但是我沒有找到解釋如何確定州的順序。當然,我可以猜測它是轉換表「開始」列中狀態的順序。但是,有些州不在「開始」欄中的情況呢?