{"id":245,"date":"2023-06-06T18:02:16","date_gmt":"2023-06-06T10:02:16","guid":{"rendered":"https:\/\/www.346pro.club\/?p=245"},"modified":"2023-06-06T19:45:41","modified_gmt":"2023-06-06T11:45:41","slug":"boost-msm%e4%b8%8e%e7%8a%b6%e6%80%81%e6%9c%ba","status":"publish","type":"post","link":"https:\/\/www.346pro.club\/?p=245","title":{"rendered":"Boost.msm\u4e0e\u72b6\u6001\u673a"},"content":{"rendered":"\n<p>msm\u72b6\u6001\u673a\uff0c\u4e5f\u53ef\u4ee5\u88ab\u79f0\u4f5c\u662f\u6d41\u7a0b\u56fe\uff0c\u5b9e\u73b0\u8d77\u6765\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u73b0\u5728\u6211\u5728\u8fd9\u91cc\u8bf4\u4e00\u4e0bBoost.msm\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n\n\n<p>Foa\uff0c\u4f60\u9700\u8981\u4e00\u5f20\u6d41\u7a0b\u56fe\uff0c\u8fd9\u5f20\u6d41\u7a0b\u56fe\u51b3\u5b9a\u4e86\u4f60\u6574\u4e2a\u7a0b\u5e8f\u7684\u52a8\u4f5c\u6d41\u7a0b\uff0c\u501f\u7528\u4e00\u5f20boost\u5b98\u65b9\u4f8b\u5b50<\/p>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"726\" src=\"https:\/\/www.346pro.club\/wp-content\/uploads\/2023\/06\/image.png\" alt=\"\" class=\"wp-image-246\" srcset=\"https:\/\/www.346pro.club\/wp-content\/uploads\/2023\/06\/image.png 750w, https:\/\/www.346pro.club\/wp-content\/uploads\/2023\/06\/image-300x290.png 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n<p>\u8fd9\u5f20\u56fe\u91cc\u9762\u6709\u51e0\u4e2a\u70b9\u9700\u8981\u6ce8\u610f\u7684\uff0c\u4e5f\u662f\u8fd9\u5f20\u56fe\u7684\u5173\u952e\u70b9<\/p>\n\n\n<ol>\n<li>\u4e94\u4e2a\u72b6\u6001\uff0c\u5b9a\u4e49\u4e86\u4e94\u4e2a\u6d41\u7a0b<\/li>\n<li>\u72b6\u6001\u4e4b\u95f4\u7684\u8fde\u63a5\u7ebf\uff0c\u5b9a\u4e49\u4e86\u4ece\u4e00\u4e2a\u72b6\u6001\u8f6c\u6362\u5230\u53e6\u4e00\u4e2a\u72b6\u6001\u7684\u65b9\u5f0f<\/li>\n<li>\u521d\u59cb\u7684\u72b6\u6001\u4f4d\n<\/li>\n<\/ol>\n\n<p>\u4ece\u7b2c\u4e00\u9879\u5f00\u59cb\u8bf4\uff0c\u9996\u5148\u8981\u5b9a\u4e49\u4e00\u4e2a\u603b\u7684\u72b6\u6001\u673a\u7c7b\uff0c\u8fd9\u4e2a\u7c7b\u7ee7\u627f\u4e8eboost::msm::front::state_machine_def<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class player_ : public boost::msm::front::state_machine_def&lt;player_>\n{\npublic:\n    \/\/code...\n}<\/pre>\n\n\n<p>\u8fd9\u4e2a\u72b6\u6001\u673a\u7684\u5b9a\u4e49\u90fd\u653e\u5728\u8fd9\u4e2a\u7c7b\u91cc\u9762<\/p>\n\n\n<p>\u5728\u5176\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e0e\u8fd9\u4e2a\u72b6\u6001\u673a\u6709\u5173\u7684\u7ec4\u4ef6\uff0c\u5176\u4e2d\u5c31\u5305\u62ec\u4e86\u72b6\u6001\u672c\u4f53\u7684\u7ec4\u4ef6<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Open : public boost::msm::front::state&lt;>\n{\npublic:\n    template &lt;class Event, class FSM>\n    void on_entry(const Event&amp;, FSM&amp;) { std::cout &lt;&lt; \"entering: Open\" &lt;&lt; std::endl; }\n\n    template &lt;class Event, class FSM>\n    void on_exit(const Event&amp;, FSM&amp;) { std::cout &lt;&lt; \"leaving: Open\" &lt;&lt; std::endl; }\n};<\/pre>\n\n\n<p>\u672c\u4f53\u91cc\u9762\u5176\u5b9e\u53ea\u6709\u4e24\u4e2a\u51fd\u6570\uff0c\u4e00\u4e2a\u5b9a\u4e49\u8fdb\u5165\u72b6\u6001\u7684\u51fd\u6570\uff0c\u4e00\u4e2a\u5b9a\u4e49\u9000\u51fa\u72b6\u6001\u7684\u51fd\u6570\u3002<\/p>\n\n\n<p>\u5176\u6b21\u8981\u5b9a\u4e49\u7684\u5c31\u662f\u72b6\u6001\u4e0e\u72b6\u6001\u76f4\u63a5\u7684\u8fde\u63a5\u51fd\u6570\uff0c\u8fd9\u4e2a\u5176\u5b9e\u662f\u4f5c\u4e3a\u4e00\u4e2alist\u5199\u5728\u4ee3\u7801\u4e2d\u7684<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct transition_table : mpl::vector&lt;\n\/\/    Start     Event        Target      Action                      Guard \n\/\/   +---------+------------+-----------+---------------------------+----------------------------+ \na_row&lt; Stopped , play       ,  Playing  , &amp;player_::start_playback                               >,\na_row&lt; Stopped , open_close ,  Open     , &amp;player_::open_drawer                                  >,\n _row&lt; Stopped , stop       ,  Stopped                                                           >,\n\/\/   +---------+------------+-----------+---------------------------+----------------------------+ \na_row&lt; Open    , open_close ,  Empty    , &amp;player_::close_drawer                                 >,\n\/\/   +---------+------------+-----------+---------------------------+----------------------------+ \na_row&lt; Empty   , open_close ,  Open     , &amp;player_::open_drawer                                  >,\n  row&lt; Empty   , cd_detected,  Stopped  , &amp;player_::store_cd_info   , &amp;player_::good_disk_format >,\n  row&lt; Empty   , cd_detected,  Playing  , &amp;player_::store_cd_info   , &amp;player_::auto_start       >,\n\/\/   +---------+------------+-----------+---------------------------+----------------------------+ \na_row&lt; Playing , stop       ,  Stopped  , &amp;player_::stop_playback                                >,\na_row&lt; Playing , pause      ,  Paused   , &amp;player_::pause_playback                               >,\na_row&lt; Playing , open_close ,  Open     , &amp;player_::stop_and_open                                >,\n\/\/   +---------+------------+-----------+---------------------------+----------------------------+ \na_row&lt; Paused  , end_pause  ,  Playing  , &amp;player_::resume_playback                              >,\na_row&lt; Paused  , stop       ,  Stopped  , &amp;player_::stop_playback                                >,\na_row&lt; Paused  , open_close ,  Open     , &amp;player_::stop_and_open                                >\n\/\/   +---------+------------+-----------+---------------------------+----------------------------+ \n> {};<\/pre>\n\n\n<p>\u8fd9\u4e2avector\u6709\u4e94\u4e2a\u53c2\u6570\uff0cStart\u3001Event\u3001Target\u3001Action\u3001Guard\uff0cStart\u4ee3\u8868\u7bad\u5934\u7684\u8d77\u59cb\u4f4d\uff0c\u4e5f\u5c31\u662fstatus\u5f53\u524d\u6240\u5728\u7684\u4f4d\u7f6e\uff1bEvent\u4ee3\u8868\u7684\u662f\u4e00\u4e2a\u4fe1\u53f7\uff0c\u5f53\u72b6\u6001\u673a\u6536\u5230\u4e00\u4e2a\u4fe1\u53f7\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u5728\u8fd9\u4e2avector\u91cc\u9762\u641c\u7d22\u4ee5\u5f53\u524dstatus\u4e3a\u8d77\u59cb\u70b9\u65f6\u5339\u914d\u8fd9\u4e2a\u4fe1\u53f7\u7684row\uff1b\u800c\u6536\u5230Event\u4fe1\u53f7\u4ee5\u540e\uff0c\u5c31\u4f1a\u6267\u884c\u8fd9\u4e00\u4e2arow\u5bf9\u5e94\u7684Action\uff0cAction\u91cc\u9762\u4e00\u822c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570\u7684\u6307\u9488\uff1b\u6267\u884c\u5b8c\u4e86Action\u4e4b\u540e\uff0cstatus\u5c31\u4f1a\u53d8\u4e3a\u8fd9\u4e00\u4e2arow\u5bf9\u5e94\u7684Target\u3002Guard\u53c2\u6570\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u53c2\u6570\uff0c\u5b83\u662f\u4e00\u4e2a\u8fd4\u56de\u503c\u4e3abool\u7684\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u51b3\u5b9a\u4e86\u4e00\u70b9\uff0c\u5c31\u662f\u8fd9\u4e00\u884crow\u5e94\u4e0d\u5e94\u8be5\u6267\u884c\u3002<\/p>\n\n\n<p>\u68b3\u7406\u4e00\u4e0b\u6267\u884c\u7684\u5177\u4f53\u6b65\u9aa4\uff0c\u5206\u4e3a\u8fd9\u51e0\u6b65\uff1a<\/p>\n\n\n<ol>\n<li>\u5f53\u72b6\u6001\u673a\u5904\u4e8e\u67d0\u4e00\u72b6\u6001\u65f6\uff0c\u6536\u5230\u4e00\u4e2a\u7c7b\u578b\u4e3aEvent\u7684\u89e6\u53d1\u4fe1\u53f7<\/li>\n<li>\u6267\u884cGuard\uff08\u82e5\u6709\uff09\uff0c\u5982\u679c\u8fd4\u56de\u7ed3\u679c\u4e3afalse\uff0c\u4e2d\u65ad\u540e\u7eed\u6b65\u9aa4\uff0c\u4fdd\u7559\u73b0\u6709\u72b6\u6001<\/li>\n<li>\u79bb\u5f00\u73b0\u6709\u72b6\u6001\uff0c\u89e6\u53d1on_exit<\/li>\n<li>\u6267\u884cAction\uff08\u82e5\u6709\uff09<\/li>\n<li>\u8fdb\u5165Target\u72b6\u6001\uff0c\u89e6\u53d1on_entry\n<\/li>\n<\/ol>\n\n<p>\u7136\u540e\u5c31\u662fGuard\u548cAction\u51fd\u6570\u7684\u7ec4\u6210\u4e86\uff0c\u8fd9\u4e24\u4e2a\u51fd\u6570\u7684\u8f93\u5165\u90fd\u662f\u4e00\u81f4\u7684\uff0c\u90fd\u662f\u7531Event\u89c4\u5b9a\u7684\u7ed3\u6784\u4f53\u6765\u7ec4\u6210\u7684\uff0c\u533a\u522b\u5c31\u5728\u4e8e\u8fd4\u56de\u503c\uff0cAction\u51fd\u6570\u662f\u4e00\u4e2a\u65e0\u8fd4\u56de\u7684void\u51fd\u6570\uff0c\u800cGuard\u5219\u662f\u4e00\u4e2a\u8fd4\u56de\u503c\u4e3abool\u7684\u5224\u65ad\u51fd\u6570\uff0c\u4e3e\u4e2a\u6817\u5b50<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct cd_detected\n{\n    cd_detected(std::string name, DiskTypeEnum diskType)\n        : name(name),\n          disc_type(diskType)\n    {\n    }\n\n    std::string name;\n    DiskTypeEnum disc_type;\n};\n\nvoid store_cd_info(const cd_detected&) { std::cout << \"player::store_cd_info\\n\"; }<\/pre>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">bool good_disk_format(const cd_detected& evt)\n{\n    if (evt.disc_type != DISK_CD)\n    {\n        std::cout << \"wrong disk, sorry\" << std::endl;\n        return false;\n    }\n    return true;\n}<\/pre>\n\n\n<p>\u4e0a\u9762\u4e00\u4e2a\u4e3aAction\u51fd\u6570\uff0c\u4e0b\u9762\u7684\u4e3aGuard\u51fd\u6570\uff0c\u5b83\u4eec\u7684\u8f93\u5165\u90fd\u4e3a\u89c4\u5b9a\u597d\u7684Event\u7ed3\u6784\u4f53\u3002<\/p>\n\n\n<p>\u6700\u540e\u662f\u521d\u59cb\u72b6\u6001\u7684\u5b9a\u4e49\u51fd\u6570\uff0c\u5982\u679c\u4e0d\u5b9a\u4e49\u521d\u59cb\u72b6\u6001\uff0c\u4f1a\u5bfc\u81f4\u542f\u52a8\u65f6\u627e\u4e0d\u5230\u72b6\u6001\u800c\u62a5\u9519<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using initial_state = Empty;<\/pre>\n\n\n<p>\u8fd9\u6837\u5c31\u57fa\u672c\u7ed3\u675f\u4e86\uff0c\u4f60\u53ef\u4ee5\u5199\u4e00\u4e2atest\u51fd\u6570\u6765\u6d4b\u8bd5\u8fd9\u4e2a\u72b6\u6001\u673a<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void pstate(const player& p)\n{\n    std::cout << \" -> \" << state_names[p.current_state()[0]] << std::endl;\n}\n\nvoid test()\n{\n    player p;\n    \/\/ needed to start the highest-level SM. This will call on_entry and mark the start of the SM\n    p.start();\n    \/\/ go to Open, call on_exit on Empty, then action, then on_entry on Open\n    p.process_event(open_close());\n    pstate(p);\n    p.process_event(open_close());\n    pstate(p);\n    \/\/ will be rejected, wrong disk type\n    p.process_event(cd_detected(\"louie, louie\", DISK_DVD));\n    pstate(p);\n    p.process_event(cd_detected(\"louie, louie\", DISK_CD));\n    pstate(p);\n    p.process_event(play());\n\n    \/\/ at this point, Play is active      \n    p.process_event(pause());\n    pstate(p);\n    \/\/ go back to Playing\n    p.process_event(end_pause());\n    pstate(p);\n    p.process_event(pause());\n    pstate(p);\n    p.process_event(stop());\n    pstate(p);\n    \/\/ event leading to the same state\n    \/\/ no action method called as it is not present in the transition table\n    p.process_event(stop());\n    pstate(p);\n}\n\nint main()\n{\n    test();\n    return 0;\n}<\/pre>\n\n\n<p>TODO\uff1a\u9519\u8bef\u5904\u7406<\/p>\n","protected":false},"excerpt":{"rendered":"<p>msm\u72b6\u6001\u673a\uff0c\u4e5f\u53ef\u4ee5\u88ab\u79f0\u4f5c\u662f\u6d41\u7a0b\u56fe\uff0c\u5b9e\u73b0\u8d77\u6765\u7684\u65b9\u6cd5&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/www.346pro.club\/?p=245\">Continue reading<span class=\"screen-reader-text\">Boost.msm\u4e0e\u72b6\u6001\u673a<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-245","post","type-post","status-publish","format-standard","hentry","category-technology","entry"],"_links":{"self":[{"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.346pro.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=245"}],"version-history":[{"count":5,"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":254,"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/posts\/245\/revisions\/254"}],"wp:attachment":[{"href":"https:\/\/www.346pro.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.346pro.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.346pro.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}