zoukankan      html  css  js  c++  java
  • 状态机

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

    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 namespace sc = boost::statechart;
     9 namespace mpl = boost::mpl;
    10 
    11 struct lostMoney : sc::event<lostMoney> {};
    12 struct gotMoney : sc::event<gotMoney> {};
    13 
    14 struct HappyState;
    15 struct SadState;
    16 struct MoreHappyState;
    17 struct PersonSM : sc::state_machine<PersonSM, HappyState> {};
    18 
    19 struct HappyState : sc::simple_state<HappyState, PersonSM>
    20 {
    21     HappyState() { std::cout << "In Happy State..." << std::endl; }
    22 
    23     typedef mpl::list<
    24     sc::transition<lostMoney, SadState>,
    25     sc::transition<gotMoney, MoreHappyState>
    26     > reactions;
    27 
    28     ~HappyState() { std::cout << "In Hadppy State DESTRUCTION ..." << std::endl; }
    29 };
    30 
    31 struct MoreHappyState : sc::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 : sc::simple_state<SadState, PersonSM>
    38 {
    39     SadState() { std::cout << "In Sad State..." << std::endl; }
    40     typedef sc::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     sm.process_event(gotMoney());
    50     sm.process_event(gotMoney());
    51 }
    View Code

    输出结果:

    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 namespace sc = boost::statechart;
     9 namespace mpl = boost::mpl;
    10 
    11 struct lostMoney : sc::event<lostMoney> {};
    12 struct gotMoney : sc::event<gotMoney> {};
    13 
    14 struct HappyState;
    15 struct SadState;
    16 struct MoreHappyState;
    17 struct PersonSM : sc::state_machine<PersonSM, HappyState> {};
    18 
    19 struct HappyState : sc::simple_state<HappyState, PersonSM>
    20 {
    21     HappyState() { std::cout << "In Happy State..." << std::endl; }
    22 
    23     typedef sc::custom_reaction<lostMoney> reactions;
    24     sc::result react(const lostMoney &lm)
    25     {
    26         std::cout << "I receive lost money event" << std::endl;
    27         return transit<SadState>();
    28     }
    29     ~HappyState() { std::cout << "In Hadppy State DESTRUCTION ..." << std::endl; }
    30 };
    31 
    32 struct MoreHappyState : sc::simple_state<MoreHappyState, PersonSM>
    33 {
    34     MoreHappyState() { std::cout << "In More Happy State..." << std::endl; }
    35     ~MoreHappyState() { std::cout << "In More Happy State DESTRUCTION ..." << std::endl; }
    36 };
    37 
    38 struct SadState : sc::simple_state<SadState, PersonSM>
    39 {
    40     SadState() { std::cout << "In Sad State..." << std::endl; }
    41     typedef sc::transition<gotMoney, HappyState> reactions;
    42     ~SadState() { std::cout << "In Sad State DESTRUCTION ..." << std::endl; }
    43 };
    44 
    45 int main()
    46 {
    47     PersonSM sm;
    48     sm.initiate();
    49     sm.process_event(lostMoney()); 
    50 }#include <boost/statechart/transition.hpp>
    51 #include <boost/statechart/event.hpp>
    52 #include <boost/statechart/state_machine.hpp>
    53 #include <boost/statechart/simple_state.hpp>
    54 #include <boost/statechart/custom_reaction.hpp>
    55 #include <iostream>
    56 
    57 namespace sc = boost::statechart;
    58 namespace mpl = boost::mpl;
    59 
    60 struct lostMoney : sc::event<lostMoney> {};
    61 struct gotMoney : sc::event<gotMoney> {};
    62 
    63 struct HappyState;
    64 struct SadState;
    65 struct MoreHappyState;
    66 struct PersonSM : sc::state_machine<PersonSM, HappyState> {};
    67 
    68 struct HappyState : sc::simple_state<HappyState, PersonSM>
    69 {
    70     HappyState() { std::cout << "In Happy State..." << std::endl; }
    71 
    72     typedef sc::custom_reaction<lostMoney> reactions;
    73     sc::result react(const lostMoney &lm)
    74     {
    75         std::cout << "I receive lost money event" << std::endl;
    76         return transit<SadState>();
    77     }
    78     ~HappyState() { std::cout << "In Hadppy State DESTRUCTION ..." << std::endl; }
    79 };
    80 
    81 struct MoreHappyState : sc::simple_state<MoreHappyState, PersonSM>
    82 {
    83     MoreHappyState() { std::cout << "In More Happy State..." << std::endl; }
    84     ~MoreHappyState() { std::cout << "In More Happy State DESTRUCTION ..." << std::endl; }
    85 };
    86 
    87 struct SadState : sc::simple_state<SadState, PersonSM>
    88 {
    89     SadState() { std::cout << "In Sad State..." << std::endl; }
    90     typedef sc::transition<gotMoney, HappyState> reactions;
    91     ~SadState() { std::cout << "In Sad State DESTRUCTION ..." << std::endl; }
    92 };
    93 
    94 int main()
    95 {
    96     PersonSM sm;
    97     sm.initiate();
    98     sm.process_event(lostMoney()); 
    99 }
    View Code

    输出结果:

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

    参考资料:

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

  • 相关阅读:
    服务器建设问题
    JDBC --反射(二)
    Cookies
    http和https区别
    springboot常用注解
    线程池
    悲观锁和乐观锁
    java高并发下的数据安全
    idea解决mybatis逆向工程
    spring Cloud
  • 原文地址:https://www.cnblogs.com/sunbines/p/15084406.html
Copyright © 2011-2022 走看看