#pragma once #include #include #include #include "Transition.h" #include "Action.h" using namespace std; /// /// 表示状态。 /// /// template class State { public: State() { } ~State() { for_each(m_trans.begin(), m_trans.end(), [](Transition* item) {delete item; }); m_trans.clear(); } /// /// 添加一个转换。 /// /// /// Transition* addTransition(const TType& type, State* dest) { Transition* transition = new Transition(type, dest); m_trans.push_back(transition); return transition; } /// /// 执行操作。 /// /// /// 返回操作对应的状态。 State* doAction(const Action& action) { Transition* transition = nullptr; for(auto item = m_trans.begin(); item != m_trans.end(); item++) { if ((*item)->type() == action.type()) { transition = (*item); break; } } State* state = nullptr; if (transition) { state = transition->state(); } return state; } public: /// /// 设置进入状态的执行回调。 /// /// void setEnter(function enterCall) { m_enter = enterCall; } /// /// 设置执行状态的回调。 /// /// void setExec(function execCall) { m_exec = execCall; } /// /// 设置退出状态的回调。 /// /// void setExit(function exitCall) { m_exit = exitCall; } /// /// 进入状态。 /// void onEnter() { if (m_enter != nullptr) { m_enter(); } } /// /// 执行状态。 /// void onExec() { if (m_exec) { m_exec(); } } /// /// 退出状态。 /// void onExit() { if (m_exit) { m_exit(); } } private: list *> m_trans; function m_enter; function m_exec; function m_exit; };