状态机

1. 安装boost库

yum install boost
yum install boost-devel
yum install boost-doc

 

2. 例子1

 1 #include <boost/statechart/transition.hpp>
 2 #include <boost/statechart/event.hpp>
 3 #include <boost/statechart/state_machine.hpp>
 4 #include <boost/statechart/simple_state.hpp>
 5 #include <boost/statechart/custom_reaction.hpp>
 6 #include <iostream>
 7 
 8 namespace sc = boost::statechart;
 9 
10 struct lostMoney : sc::event<lostMoney> {};
11 struct gotMoney : sc::event<gotMoney> {};
12 
13 struct HappyState;
14 struct SadState;
15 struct PersonSM : sc::state_machine<PersonSM, HappyState> {};
16 
17 struct HappyState : sc::simple_state<HappyState, PersonSM>
18 {
19     HappyState() { std::cout << "In Happy State..." << std::endl; }
20     typedef sc::transition<lostMoney, SadState> reactions;
21     ~HappyState() { std::cout << "In Hadppy State DESTRUCTION ..." << std::endl; }
22 };
23 
24 struct SadState : sc::simple_state<SadState, PersonSM>
25 {
26     SadState() { std::cout << "In Sad State..." << std::endl; }
27     typedef sc::transition<gotMoney, HappyState> reactions;
28     ~SadState() { std::cout << "In Sad State DESTRUCTION ..." << std::endl; }
29 };
30 
31 int main()
32 {
33     PersonSM sm;
34     sm.initiate();
35     sm.process_event(lostMoney());
36     sm.process_event(gotMoney());
37 }

输出结果:

In Happy State...
In Hadppy State DESTRUCTION ...
In Sad State...
In Sad State DESTRUCTION ...
In Happy State...
In Hadppy State DESTRUCTION ...

 

上一篇:简陋RPC理解实现


下一篇:Boost Asio要点概述(一)