【C++编程】boost::statechart状态机

 boost::statechart

  如果是自定义了react消息,表示当前状态接受并处理了EvBingo消息,他有权抛弃事件(discard_event),抛出其他消息但是会延迟到本函数执行完毕后抛出(post_event(xxx)),立即抛出消息(process_event(xxx)),继续向上层状态抛出同一事件(forward_event),或者直接跳转 (transit),但是要注意的是,如果使用的是transit或者process_event这种可能导致状态即时切换的函数时,最好在之后的流程中不要对当前state进行操作了,因为它相当于你delete了一个类对象但是还在继续使用对象的数据,这个操作是相当危险的。所以 transit和process_event一般是放在 react函数流程的最后执行的。

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 ...

 

2. 例子:

 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 struct lostMoney : boost::statechart::event<lostMoney> {};
 9 struct gotMoney : boost::statechart::event<gotMoney> {};
10 
11 struct HappyState;
12 struct SadState;
13 struct MoreHappyState;
14 struct PersonSM : boost::statechart::state_machine<PersonSM, HappyState> {};
15 
16 struct HappyState : boost::statechart::simple_state<HappyState, PersonSM>
17 {
18     HappyState() { std::cout << "In Happy State..." << std::endl; }
19 
20     typedef boost::mpl::list<boost::statechart::transition<lostMoney, SadState>,
21                              boost::statechart::transition<gotMoney, MoreHappyState>> reactions;
22 
23     ~HappyState() { std::cout << "In Hadppy State DESTRUCTION ..." << std::endl; }
24 };
25 
26 struct MoreHappyState : boost::statechart::simple_state<MoreHappyState, PersonSM>
27 {
28     MoreHappyState() { std::cout << "In More Happy State..." << std::endl; }
29     ~MoreHappyState() { std::cout << "In More Happy State DESTRUCTION ..." << std::endl; }
30 };
31 
32 struct SadState : boost::statechart::simple_state<SadState, PersonSM>
33 {
34     SadState() { std::cout << "In Sad State..." << std::endl; }
35     typedef sc::transition<gotMoney, HappyState> reactions;
36     ~SadState() { std::cout << "In Sad State DESTRUCTION ..." << std::endl; }
37 };
38 
39 int main()
40 {
41     PersonSM sm;
42     sm.initiate();
43     sm.process_event(lostMoney());
44     sm.process_event(gotMoney());
45     sm.process_event(gotMoney());
46 }

输出结果:

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

 

3. 例子3

 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 struct lostMoney : sc::event<lostMoney> {};
 9 struct gotMoney : sc::event<gotMoney> {};
10 
11 struct HappyState;
12 struct SadState;
13 struct MoreHappyState;
14 struct PersonSM : boost::statechart::state_machine<PersonSM, HappyState>
15 {
16 };
17 
18 struct HappyState : boost::statechart::simple_state<HappyState, PersonSM>
19 {
20     HappyState() { std::cout << "In Happy State..." << std::endl; }
21 
22     typedef boost::statechart::custom_reaction<lostMoney> reactions;
23     sc::result react(const lostMoney &lm)
24     {
25         std::cout << "I receive lost money event" << std::endl;
26         return transit<SadState>();
27     }
28     ~HappyState() { std::cout << "In Hadppy State DESTRUCTION ..." << std::endl; }
29 };
30 
31 struct MoreHappyState : boost::statechart::simple_state<MoreHappyState, PersonSM>
32 {
33     MoreHappyState() { std::cout << "In More Happy State..." << std::endl; }
34     ~MoreHappyState() { std::cout << "In More Happy State DESTRUCTION ..." << std::endl; }
35 };
36 
37 struct SadState : boost::statechart::simple_state<SadState, PersonSM>
38 {
39     SadState() { std::cout << "In Sad State..." << std::endl; }
40     typedef boost::statechart::transition<gotMoney, HappyState> reactions;
41     ~SadState() { std::cout << "In Sad State DESTRUCTION ..." << std::endl; }
42 };
43 
44 int main()
45 {
46     PersonSM sm;
47     sm.initiate();
48     sm.process_event(lostMoney());
49 }

输出结果:

In Happy State...
I receive lost money event
In Sad State...
In Sad State DESTRUCTION ...

 

参考资料

1. C++学习 Boost状态机说明statechart

2. boost::statechart研究报告

posted @ 2021-07-31 15:53  苏格拉底的落泪  阅读(1004)  评论(0编辑  收藏  举报