标签:
一个简单的有限状态机如图所示:
这个状态机有两个,State1状态,和结束状态。
下列代码可以描述这个状态机:
#include <iostream>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
namespace {
namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;
// ----- Events
struct Event1 {};
// ----- State machine
struct Sm1_:msmf::state_machine_def<Sm1_>
{
// States
struct State1:msmf::state<>
{
// Entry action
template <class Event,class Fsm>
void on_entry(Event const&, Fsm&) const {
std::cout << "State1::on_entry()" << std::endl;
}
// Exit action
template <class Event,class Fsm>
void on_exit(Event const&, Fsm&) const {
std::cout << "State1::on_exit()" << std::endl;
}
};
struct End:msmf::terminate_state<> {};
// Set initial state
typedef State1 initial_state;
// Transition table
struct transition_table:mpl::vector<
// Start Event Next Action Guard
msmf::Row < State1, Event1, End, msmf::none, msmf::none >
> {};
};
// Pick a back-end
typedef msm::back::state_machine<Sm1_> Sm1;
void test()
{
Sm1 sm1;
sm1.start();
std::cout << "> Send Event1" << std::endl;
sm1.process_event(Event1());
}
}
int main()
{
test();
return 0;
}
// Output:
//
// State1::on_entry()
// > Send Event1
// State1::on_exit()
Sm1_ 对象表示这个状态机. 在 Sm1_状态机中有两个state,分别为:State1和End. 初始伪状态通过如下语句定义:
// Set initial state
typedef State1 initial_state;
typedef xxx initial_state 表示状态机Sm1_从状态State1开始。
标签:
原文地址:http://blog.csdn.net/gw569453350game/article/details/47272833