﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-jack-wang-随笔分类-Boost</title><link>http://www.cppblog.com/jack-wang/category/10927.html</link><description>小王</description><language>zh-cn</language><lastBuildDate>Sun, 27 Jun 2021 02:49:35 GMT</lastBuildDate><pubDate>Sun, 27 Jun 2021 02:49:35 GMT</pubDate><ttl>60</ttl><item><title>boost编译（Windows平台）</title><link>http://www.cppblog.com/jack-wang/archive/2021/06/26/217724.html</link><dc:creator>小王</dc:creator><author>小王</author><pubDate>Fri, 25 Jun 2021 23:45:00 GMT</pubDate><guid>http://www.cppblog.com/jack-wang/archive/2021/06/26/217724.html</guid><wfw:comment>http://www.cppblog.com/jack-wang/comments/217724.html</wfw:comment><comments>http://www.cppblog.com/jack-wang/archive/2021/06/26/217724.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jack-wang/comments/commentRss/217724.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jack-wang/services/trackbacks/217724.html</trackback:ping><description><![CDATA[1、下载最新版本，当前最新版本为1.76，下载后解压<br />2、进入根目录，运行下面的bootstrap.bat，生成b2.exe（以前的<span style="color: #333333; font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-size: 16px; background-color: #ffffff;">bjam.exe已经被淘汰了</span>）<br />3、打开VS工具x86 Native Tools Command Prompt，进入boost解压文件夹根目录，执行命令：<div><span style="font-size: 16px;">b2 install --toolset=msvc-14.2 --build-type=complete --prefix=&#8220;E:\Packet\boost_1_72_0\compile&#8221; link=shared runtime-link=shared runtime-link=static threading=multi debug release</span></div><div><br />漫长的等待，长长的站台</div><span style="background-color: #ffffff;"><font color="#4d4d4d" face="-apple-system, SF UI Text, Arial, PingFang SC, Hiragino Sans GB, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif, SimHei, SimSun"><span style="font-size: 16px;">O了！</span></font></span><img src ="http://www.cppblog.com/jack-wang/aggbug/217724.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jack-wang/" target="_blank">小王</a> 2021-06-26 07:45 <a href="http://www.cppblog.com/jack-wang/archive/2021/06/26/217724.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>编译boost1.54</title><link>http://www.cppblog.com/jack-wang/archive/2013/08/20/202675.html</link><dc:creator>小王</dc:creator><author>小王</author><pubDate>Tue, 20 Aug 2013 08:12:00 GMT</pubDate><guid>http://www.cppblog.com/jack-wang/archive/2013/08/20/202675.html</guid><wfw:comment>http://www.cppblog.com/jack-wang/comments/202675.html</wfw:comment><comments>http://www.cppblog.com/jack-wang/archive/2013/08/20/202675.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jack-wang/comments/commentRss/202675.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jack-wang/services/trackbacks/202675.html</trackback:ping><description><![CDATA[从boost官网上下载了1.54版本的压缩包，解压到本地磁盘。<br />我把它解压到这里D:\boost_1_54_0。<br />
从控制台进入D:\boost_1_54_0目录中，输入命令bootstrap
执行后启动编译程序，开始准备编译环境，稍等片刻，<br />提示再次输入编译参数，我输入
.\b2&nbsp;&nbsp;&nbsp;（生成vs2010工程文件）
<br />执行后开始编译源文件
<br />等待，，，<br />等待，，，
<br /><br /><br />

补充：
<br />使用VS2013工具集编译，命令如下:<br />
.\b2 --toolset=msvc-12.0<img src ="http://www.cppblog.com/jack-wang/aggbug/202675.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jack-wang/" target="_blank">小王</a> 2013-08-20 16:12 <a href="http://www.cppblog.com/jack-wang/archive/2013/08/20/202675.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>模板元编程 --- 用 MPL 解决实际问题</title><link>http://www.cppblog.com/jack-wang/archive/2010/10/04/128589.html</link><dc:creator>小王</dc:creator><author>小王</author><pubDate>Mon, 04 Oct 2010 10:40:00 GMT</pubDate><guid>http://www.cppblog.com/jack-wang/archive/2010/10/04/128589.html</guid><wfw:comment>http://www.cppblog.com/jack-wang/comments/128589.html</wfw:comment><comments>http://www.cppblog.com/jack-wang/archive/2010/10/04/128589.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jack-wang/comments/commentRss/128589.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jack-wang/services/trackbacks/128589.html</trackback:ping><description><![CDATA[<p>转：<a href="http://blog.csdn.net/ralph623/archive/2005/10/15/504369.aspx">http://blog.csdn.net/ralph623/archive/2005/10/15/504369.aspx</a><br><br>在 STL 中有各种容器，而 STL 算法允许我们对容器中的元素做各种操作，下面的程序对于每一个当代的 C++ 程序员都应该是轻而易举的：</p>
<p>#include &lt;iostream&gt;<br>#include &lt;list&gt;<br>#include &lt;algorithm&gt;<br>#include &lt;string&gt;</p>
<p>using namespace std;</p>
<p>struct print<br>{<br>&nbsp;&nbsp;&nbsp; void operator()(const string&amp; _str)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; _str &lt;&lt; endl;<br>&nbsp;&nbsp;&nbsp; }<br>};</p>
<p>int main()<br>{<br>&nbsp;&nbsp;&nbsp; list&lt;string&gt; str_list;<br>&nbsp;&nbsp;&nbsp; str_list.push_front("hello");<br>&nbsp;&nbsp;&nbsp; str_list.push_front("world");<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; list&lt;string&gt; another_list;<br>&nbsp;&nbsp;&nbsp; another_list.push_back("hello");<br>&nbsp;&nbsp;&nbsp; another_list.push_back("world");<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; for_each(str_list.begin(), str_list.end(), print());<br>&nbsp;&nbsp;&nbsp; for_each(another_list.begin(), another_list.end(), print());<br>}</p>
<p>运行结果：</p>
<p>world<br>hello<br>hello<br>world</p>
<p>简单的东西往往能说明深刻的道理，在这个程序里，我们遇到的本质问题是什么？首先，我们有一个容器；其次，我们可以往容器里面放东西，最后，我们可以通过算法把一个操作施加于这个容器中的每一个（也可以是部分）元素中。这就是上面程序中凝结的本质问题。</p>
<p>MPL 可以看成是 STL 的编译期版本，或者说元编程版本。它同样也提供了各种容器，只不过容纳的对象不是数据，而是类型。它们的构造方式语法上比较类似，或者甚至，我以为，更有趣一点：</p>
<p>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>#include &lt;boost/mpl/at.hpp&gt;<br>#include &lt;boost/mpl/list.hpp&gt;<br>#include &lt;boost/mpl/push_front.hpp&gt;</p>
<p>using namespace boost;</p>
<p>int main()<br>{<br>&nbsp;&nbsp;&nbsp; typedef mpl::list&lt;&gt; type_list1;<br>&nbsp;&nbsp;&nbsp; typedef mpl::push_front&lt;type_list1, int&gt;::type type_list2;<br>&nbsp;&nbsp;&nbsp; typedef mpl::push_front&lt;type_list2, std::string&gt;::type type_list;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; // 或者这样更好<br>&nbsp;&nbsp;&nbsp; typedef mpl::list&lt;int, std::string&gt; another_list;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; typeid(mpl::at_c&lt;type_list, 0&gt;::type).name() &lt;&lt; std::endl;<br>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; typeid(mpl::at_c&lt;type_list, 1&gt;::type).name() &lt;&lt; std::endl;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; typeid(mpl::at_c&lt;another_list, 0&gt;::type).name() &lt;&lt; std::endl;<br>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; typeid(mpl::at_c&lt;another_list, 1&gt;::type).name() &lt;&lt; std::endl;<br>}</p>
<p>稍微解释一下。mpl::list 就是 std::list 的元编程版本，而 mpl::push_front 是什么就不用我说了。mpl::at_c 是一个元编程算法，作用相当于运行期的 [ ] 运算符，也就是得到一个容器中在某个位置上的元素。在 VC7.1 下面，执行结果是</p>
<p>class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;<br>int<br>int<br>class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;</p>
<p>这跟运行期的 list 的行为几乎完全一致。</p>
<p>当然，mpl 也有 for_each ，而且我们也可以为 for_each 提供一个元编程 functor 。什么是元编程 functor ？运行时的 functor 是一个提供了 operator() 重载的 struct ，而元编程 functor 就是一个提供了 operator() 模板的 struct ：</p>
<p>#include &lt;string&gt;<br>#include &lt;iostream&gt;<br>#include &lt;boost/mpl/at.hpp&gt;<br>#include &lt;boost/mpl/list.hpp&gt;<br>#include &lt;boost/mpl/push_front.hpp&gt;<br>#include &lt;boost/mpl/for_each.hpp&gt;</p>
<p>using namespace boost;</p>
<p>struct print<br>{<br>&nbsp;&nbsp;&nbsp; template &lt;class T&gt;<br>&nbsp;&nbsp;&nbsp; void operator()(const T&amp;)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; typeid(T).name() &lt;&lt; std::endl;<br>&nbsp;&nbsp;&nbsp; }<br>};</p>
<p>int main()<br>{<br>&nbsp;&nbsp;&nbsp; typedef mpl::list&lt;&gt; type_list1;<br>&nbsp;&nbsp;&nbsp; typedef mpl::push_front&lt;type_list1, int&gt;::type type_list2;<br>&nbsp;&nbsp;&nbsp; typedef mpl::push_front&lt;type_list2, std::string&gt;::type type_list;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; typedef mpl::list&lt;int, std::string&gt; another_list;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; mpl::for_each&lt;type_list&gt;(print());<br>&nbsp;&nbsp;&nbsp; mpl::for_each&lt;another_list&gt;(print());<br>}</p>
<p>输出与上面使用 mpl::at_c 的程序完全相同。</p>
<p>当然，到现在为止，这些程序都还是只停留在纯粹的玩具程序上，能不能做点稍微有用的事情呢？当然可以。假定我们有这样一个继承体系：根是一个抽象类 Product ，它有一些派生类，例如 PC ， Printer 等等，它们的公共方法 SerialNo 会返回自己的产品序列号，而这个序列号是在构造的时候决定的：</p>
<p>class Product<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; virtual std::string SerialNo()const = 0;<br>};</p>
<p>class PC : public Product<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; PC(const std::string&amp; _sn)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : sn_(_sn)<br>&nbsp;&nbsp;&nbsp; {}</p>
<p>&nbsp;&nbsp;&nbsp; std::string SerialNo()const<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return sn_;<br>&nbsp;&nbsp;&nbsp; }<br>private:<br>&nbsp;&nbsp;&nbsp; std::string sn_;<br>};</p>
<p>class Printer : public Product<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; Printer(const std::string&amp; _sn)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : sn_(_sn)<br>&nbsp;&nbsp;&nbsp; {}<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; std::string SerialNo()const<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return sn_;<br>&nbsp;&nbsp;&nbsp; }<br>private:<br>&nbsp;&nbsp;&nbsp; std::string sn_;<br>};</p>
<p>用 mpl::list 把这些类型放在同一个 list 里面当然不在话下，但是我们希望有一个类似 factory 模式的实现，让我们可以自由创建它们。下面的程序用 mpl::for_each 为 list 中的每一个类型创建一个实例，它当然可以被扩展来做些很有用的事情。</p>
<p>#include &lt;string&gt;<br>#include &lt;sstream&gt;<br>#include &lt;iostream&gt;<br>#include &lt;algorithm&gt;<br>#include &lt;list&gt;<br>#include &lt;boost/shared_ptr.hpp&gt;<br>#include &lt;boost/mpl/at.hpp&gt;<br>#include &lt;boost/mpl/list.hpp&gt;<br>#include &lt;boost/mpl/for_each.hpp&gt;</p>
<p>using namespace boost;</p>
<p>class Product<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; virtual std::string SerialNo()const = 0;<br>};</p>
<p>class PC : public Product<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; PC(const std::string&amp; _sn)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : sn_(_sn)<br>&nbsp;&nbsp;&nbsp; {}</p>
<p>&nbsp;&nbsp;&nbsp; std::string SerialNo()const<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return sn_;<br>&nbsp;&nbsp;&nbsp; }<br>private:<br>&nbsp;&nbsp;&nbsp; std::string sn_;<br>};</p>
<p>class Printer : public Product<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; Printer(const std::string&amp; _sn)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : sn_(_sn)<br>&nbsp;&nbsp;&nbsp; {}<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; std::string SerialNo()const<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return sn_;<br>&nbsp;&nbsp;&nbsp; }<br>private:<br>&nbsp;&nbsp;&nbsp; std::string sn_;<br>};</p>
<p>struct print<br>{<br>&nbsp;&nbsp;&nbsp; template &lt;class T&gt;<br>&nbsp;&nbsp;&nbsp; void operator()(const T&amp; product)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "Type: " &lt;&lt; typeid(T).name()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; " SerialNo: " &lt;&lt; product.SerialNo() &lt;&lt; std::endl;<br>&nbsp;&nbsp;&nbsp; }<br>};</p>
<p>// 由于 PC 和 Print 都没有默认的 constructor ，必须加上这个<br>template &lt;class T&gt;<br>struct wrap {};</p>
<p>struct Create<br>{<br>&nbsp;&nbsp;&nbsp; Create(const std::string&amp; _line)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : line_(_line)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , serial_(0)<br>&nbsp;&nbsp;&nbsp; {}<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; template &lt;class T&gt;<br>&nbsp;&nbsp;&nbsp; void operator()(wrap&lt;T&gt;)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::stringstream ss;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ss &lt;&lt; line_ &lt;&lt; '_' &lt;&lt; serial_++;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shared_ptr&lt;T&gt; product(new T(ss.str()));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print()(*product);<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; std::string line_;<br>&nbsp;&nbsp;&nbsp; unsigned long serial_;<br>};</p>
<p>int main()<br>{&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; typedef mpl::list&lt;Printer, PC&gt; product_list;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; mpl::for_each&lt;product_list, wrap&lt;mpl::_1&gt; &gt;(Create("line1"));<br>}</p>
<p>输出：</p>
<p>Type: class Printer SerialNo: line1_0<br>Type: class PC SerialNo: line1_1</p>
<p><br>本文来自CSDN博客，转载请标明出处：<a href="http://blog.csdn.net/ralph623/archive/2005/10/15/504369.aspx">http://blog.csdn.net/ralph623/archive/2005/10/15/504369.aspx</a></p>
<img src ="http://www.cppblog.com/jack-wang/aggbug/128589.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jack-wang/" target="_blank">小王</a> 2010-10-04 18:40 <a href="http://www.cppblog.com/jack-wang/archive/2010/10/04/128589.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Boost的状态机库教程 补充 </title><link>http://www.cppblog.com/jack-wang/archive/2010/10/03/128502.html</link><dc:creator>小王</dc:creator><author>小王</author><pubDate>Sun, 03 Oct 2010 11:10:00 GMT</pubDate><guid>http://www.cppblog.com/jack-wang/archive/2010/10/03/128502.html</guid><wfw:comment>http://www.cppblog.com/jack-wang/comments/128502.html</wfw:comment><comments>http://www.cppblog.com/jack-wang/archive/2010/10/03/128502.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jack-wang/comments/commentRss/128502.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jack-wang/services/trackbacks/128502.html</trackback:ping><description><![CDATA[<div class=postTitle>转：<a href="http://www.cppblog.com/shaker/archive/2010/08/16/123601.html">http://www.cppblog.com/shaker/archive/2010/08/16/123601.html</a></div>
<p>来自:<a href="http://www.cppblog.com/shanoa/archive/2009/05/30/86143.html"><font color=#1a8bc8>http://www.cppblog.com/shanoa/archive/2009/05/30/86143.html</font></a><br>接触了boost的状态机，发现不是想象中的那么好用，在一些地方还得用上mpl库里的东西，由于对模板元编程不是很熟练，搞了好些天才算弄明白这该死的mpl::list的原理和用法。<br>boost的状态机是属于静态链接的状态机，也就是说，它的图结构是编译期间就确定了的，在运行时不可以动态配置。所以，它的用途是有一定局限性的，但在一般情况下，它不仅很通用，而且在你会用并熟练地情况下，还会很好用，用起来很舒服，逻辑也很合理。下面就是一段代码，当然也是借鉴了别人的东西，自己修改了一下，在MainState中添加了一个Transition做了测试，因为此前我还不知道一个状态如何包含多个Transition，呵呵，原来是用mpl::list来做。至于这个状态机的入门教程，网上随处可见的三部曲：《boost 状态机入门教程》说得很清楚。<br></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;&nbsp;1</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;&nbsp;2</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">ctime</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;&nbsp;3</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;&nbsp;4</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">boost</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">statechart</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">transition.hpp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;&nbsp;5</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">boost</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">statechart</span><span style="COLOR: #000000">/</span><span style="COLOR: #0000ff">event</span><span style="COLOR: #000000">.hpp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;&nbsp;6</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">boost</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">statechart</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">state_machine.hpp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;&nbsp;7</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">boost</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">statechart</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">simple_state.hpp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;&nbsp;8</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;&nbsp;9</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;sc&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;boost::statechart;<br></span><span style="COLOR: #008080">&nbsp;10</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;11</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;12</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;13</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;EvtStartStop&nbsp;:&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;sc::</span><span style="COLOR: #0000ff">event</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">EvtStartStop</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">{};<br></span><span style="COLOR: #008080">&nbsp;14</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;EvtReset&nbsp;:&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;sc::</span><span style="COLOR: #0000ff">event</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">EvtReset</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">{};<br></span><span style="COLOR: #008080">&nbsp;15</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;EvtGo&nbsp;:&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;sc::</span><span style="COLOR: #0000ff">event</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">EvtGo</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">{};<br></span><span style="COLOR: #008080">&nbsp;16</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;17</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;18</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;MainState;<br></span><span style="COLOR: #008080">&nbsp;19</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;StopState;<br></span><span style="COLOR: #008080">&nbsp;20</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;RunState;<br></span><span style="COLOR: #008080">&nbsp;21</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;TwoState;<br></span><span style="COLOR: #008080">&nbsp;22</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;23</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Machine&nbsp;:&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;sc::state_machine</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Machine,&nbsp;MainState</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;24</span>&nbsp;<span style="COLOR: #000000">{};<br></span><span style="COLOR: #008080">&nbsp;25</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;26</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;27</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;28</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;29</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;30</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;31</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;MainState&nbsp;:&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">MainState,&nbsp;Machine,&nbsp;StopState</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;32</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;33</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">&nbsp;34</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;sc::transition</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">EvtReset,&nbsp;MainState</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;reactReset;<br></span><span style="COLOR: #008080">&nbsp;35</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;sc::transition</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">EvtGo,&nbsp;TwoState</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;reactGo;<br></span><span style="COLOR: #008080">&nbsp;36</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;boost::mpl::list</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">reactReset,&nbsp;reactGo</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;reactions;<br></span><span style="COLOR: #008080">&nbsp;37</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;38</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;MainState(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">){<br></span><span style="COLOR: #008080">&nbsp;39</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">进入MainState</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">std::endl;<br></span><span style="COLOR: #008080">&nbsp;40</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mTime&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;41</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">&nbsp;42</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;43</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">MainState(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">){<br></span><span style="COLOR: #008080">&nbsp;44</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">退出MainState</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">std::endl;<br></span><span style="COLOR: #008080">&nbsp;45</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">&nbsp;46</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;47</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;mTime;<br></span><span style="COLOR: #008080">&nbsp;48</span>&nbsp;<span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">&nbsp;49</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;50</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;51</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;该状态属于无用状态，用于测试mpl::list的多transition用法</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;52</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;TwoState&nbsp;:&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">TwoState,&nbsp;Machine</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;53</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;54</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">&nbsp;55</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;sc::transition</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">EvtGo,&nbsp;MainState</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;reactions;<br></span><span style="COLOR: #008080">&nbsp;56</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;57</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;TwoState(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">){<br></span><span style="COLOR: #008080">&nbsp;58</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">进入TwoState</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">std::endl;<br></span><span style="COLOR: #008080">&nbsp;59</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">&nbsp;60</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;61</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">TwoState(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">){<br></span><span style="COLOR: #008080">&nbsp;62</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">退出TwoState</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">std::endl;<br></span><span style="COLOR: #008080">&nbsp;63</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">&nbsp;64</span>&nbsp;<span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">&nbsp;65</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;66</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;67</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;StopState&nbsp;:&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">StopState,&nbsp;MainState</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;68</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;69</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">&nbsp;70</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;sc::transition</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">EvtStartStop,&nbsp;RunState</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;reactions;<br></span><span style="COLOR: #008080">&nbsp;71</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;StopState(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">){<br></span><span style="COLOR: #008080">&nbsp;72</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">进入StopState</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">std::endl;<br></span><span style="COLOR: #008080">&nbsp;73</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">&nbsp;74</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;75</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">StopState(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">){<br></span><span style="COLOR: #008080">&nbsp;76</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">退出StopState</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">std::endl;<br></span><span style="COLOR: #008080">&nbsp;77</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">&nbsp;78</span>&nbsp;<span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">&nbsp;79</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;80</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;RunState&nbsp;:&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">RunState,&nbsp;MainState</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;81</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;82</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">&nbsp;83</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;sc::transition</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">EvtStartStop,&nbsp;StopState</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;reactions;<br></span><span style="COLOR: #008080">&nbsp;84</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;RunState(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">){<br></span><span style="COLOR: #008080">&nbsp;85</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">进入RunState</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">std::endl;<br></span><span style="COLOR: #008080">&nbsp;86</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mStartTime&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;87</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">&nbsp;88</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;89</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">RunState(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">){<br></span><span style="COLOR: #008080">&nbsp;90</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">退出RunState</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">std::endl;<br></span><span style="COLOR: #008080">&nbsp;91</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">MainState</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">().mTime&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;std::difftime(std::time(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">),&nbsp;mStartTime);<br></span><span style="COLOR: #008080">&nbsp;92</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">&nbsp;93</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;94</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;std::time_t&nbsp;mStartTime;<br></span><span style="COLOR: #008080">&nbsp;95</span>&nbsp;<span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">&nbsp;96</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;97</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;98</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_tmain(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;argc,&nbsp;_TCHAR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;argv[])<br></span><span style="COLOR: #008080">&nbsp;99</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">100</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;Machine&nbsp;mc;<br></span><span style="COLOR: #008080">101</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;mc.initiate();<br></span><span style="COLOR: #008080">102</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">103</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;mc.process_event(EvtStartStop());<br></span><span style="COLOR: #008080">104</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;mc.process_event(EvtStartStop());<br></span><span style="COLOR: #008080">105</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;mc.process_event(EvtReset());<br></span><span style="COLOR: #008080">106</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;mc.process_event(EvtGo());<br></span><span style="COLOR: #008080">107</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;mc.process_event(EvtGo());<br></span><span style="COLOR: #008080">108</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">109</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">110</span>&nbsp;<span style="COLOR: #000000">}</span></div>
<img src ="http://www.cppblog.com/jack-wang/aggbug/128502.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jack-wang/" target="_blank">小王</a> 2010-10-03 19:10 <a href="http://www.cppblog.com/jack-wang/archive/2010/10/03/128502.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Boost的状态机库教程(3) </title><link>http://www.cppblog.com/jack-wang/archive/2010/10/03/128501.html</link><dc:creator>小王</dc:creator><author>小王</author><pubDate>Sun, 03 Oct 2010 11:09:00 GMT</pubDate><guid>http://www.cppblog.com/jack-wang/archive/2010/10/03/128501.html</guid><wfw:comment>http://www.cppblog.com/jack-wang/comments/128501.html</wfw:comment><comments>http://www.cppblog.com/jack-wang/archive/2010/10/03/128501.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jack-wang/comments/commentRss/128501.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jack-wang/services/trackbacks/128501.html</trackback:ping><description><![CDATA[<div class=postTitle>转：<a href="http://www.cppblog.com/shaker/archive/2010/08/16/123599.html">http://www.cppblog.com/shaker/archive/2010/08/16/123599.html</a></div>
<div class=Section0>
<p style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><strong><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体">1.2 增加动作</span> </span></strong></p>
<p style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><br><strong></strong><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; 此时我们将只用一种动作：transitions，我们在下面的代码中插入了黑体的部分。</span> </p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><strong><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">boost</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">statechart</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">transition.hpp</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;<img src="http://www.cppblog.com/Images/dot.gif"></span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;&nbsp;Stopped;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;&nbsp;Active&nbsp;:&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;Active,&nbsp;StopWatch,&nbsp;Stopped&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_133_192_Open_Image onclick="this.style.display='none'; Codehighlighter1_133_192_Open_Text.style.display='none'; Codehighlighter1_133_192_Closed_Image.style.display='inline'; Codehighlighter1_133_192_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_133_192_Closed_Image onclick="this.style.display='none'; Codehighlighter1_133_192_Closed_Text.style.display='none'; Codehighlighter1_133_192_Open_Image.style.display='inline'; Codehighlighter1_133_192_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_133_192_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_133_192_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;<strong>typedef&nbsp;&nbsp;sc::transition</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;EvReset,&nbsp;Active&nbsp;</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><strong>&nbsp;reactions;</strong><br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;&nbsp;Running&nbsp;:&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;Running,&nbsp;Active&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img id=Codehighlighter1_250_314_Open_Image onclick="this.style.display='none'; Codehighlighter1_250_314_Open_Text.style.display='none'; Codehighlighter1_250_314_Closed_Image.style.display='inline'; Codehighlighter1_250_314_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_250_314_Closed_Image onclick="this.style.display='none'; Codehighlighter1_250_314_Closed_Text.style.display='none'; Codehighlighter1_250_314_Open_Image.style.display='inline'; Codehighlighter1_250_314_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_250_314_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_250_314_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;<strong>typedef&nbsp;&nbsp;sc::transition</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;EvStartStop,&nbsp;Stopped&nbsp;</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><strong>&nbsp;reactions;</strong><br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;&nbsp;Stopped&nbsp;:&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;Stopped,&nbsp;Active&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img id=Codehighlighter1_372_436_Open_Image onclick="this.style.display='none'; Codehighlighter1_372_436_Open_Text.style.display='none'; Codehighlighter1_372_436_Closed_Image.style.display='inline'; Codehighlighter1_372_436_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_372_436_Closed_Image onclick="this.style.display='none'; Codehighlighter1_372_436_Closed_Text.style.display='none'; Codehighlighter1_372_436_Open_Image.style.display='inline'; Codehighlighter1_372_436_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_372_436_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_372_436_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;<strong>typedef&nbsp;&nbsp;sc::transition</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;EvStartStop,&nbsp;Running&nbsp;</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><strong>&nbsp;reactions;</strong><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">一个状态可以定义任意数量的动作。这就是为什么当多于一个时，<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">我们不得不将它们放到一个mpl::list&lt;&gt;&nbsp;里。</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;&nbsp;main()<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img id=Codehighlighter1_514_737_Open_Image onclick="this.style.display='none'; Codehighlighter1_514_737_Open_Text.style.display='none'; Codehighlighter1_514_737_Closed_Image.style.display='inline'; Codehighlighter1_514_737_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_514_737_Closed_Image onclick="this.style.display='none'; Codehighlighter1_514_737_Closed_Text.style.display='none'; Codehighlighter1_514_737_Open_Image.style.display='inline'; Codehighlighter1_514_737_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_514_737_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_514_737_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;StopWatch&nbsp;myWatch;<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;myWatch.initiate();<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;<strong>myWatch.process_event(&nbsp;EvStartStop()&nbsp;);<br></strong></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;<strong>myWatch.process_event(&nbsp;EvStartStop()&nbsp;);</strong><br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;<strong>myWatch.process_event(&nbsp;EvStartStop()&nbsp;);<br></strong></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;<strong>myWatch.process_event(&nbsp;EvReset()&nbsp;);<br></strong></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<p style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><br></span></p>
<p style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; 现在我们有了所有的状态，并在适当的位置增加了所有的迁移动作，同时我们也向StopWatch发送了一些事件。这个状态机会尽职尽责的按我们的希望进行状态迁移，但依然现在还没有其它的动作。</span> </span><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><br></span></p>
<p style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><strong><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt">1.3 State-local<span style="FONT-FAMILY: 宋体">存储</span> </span></strong></p>
<p style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><br><strong></strong><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; 下一步我们将让这个Stop&nbsp;watch真正的记录时间了。根据stop&nbsp;watch所处不同的状态，我们需要不同的变量。</span> </span><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="MARGIN-TOP: 0pt; TEXT-INDENT: -21pt; MARGIN-BOTTOM: 0pt; MARGIN-LEFT: 21pt" class=p0><span style="FONT-FAMILY: 'Wingdings'; FONT-SIZE: 10.5pt">l&nbsp;</span> <span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt">Stopped<span style="FONT-FAMILY: 宋体">状态：需要一个保存逝去时间的变量。</span> </span><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="MARGIN-TOP: 0pt; TEXT-INDENT: -21pt; MARGIN-BOTTOM: 0pt; MARGIN-LEFT: 21pt" class=p0><span style="FONT-FAMILY: 'Wingdings'; FONT-SIZE: 10.5pt">l </span><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt">Running<span style="FONT-FAMILY: 宋体">状态：需要一个保存逝去时间的变量，还需要一个保存上一次启动的时间点的变量。</span> </span><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="TEXT-ALIGN: justify; MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; 无论状态机在什么状态下，我们都必须观察逝去时间这个变量。此外，当我们向状态机发送EvReSet事件时，这个变量应该被置为0。其它的变量只是状态机在Running状态时需要。无论何时我们进入Running状态时，它应该被置为系统时钟的当前时间。当我们退出Running状态时，我们仅仅从系统时钟的当前时间减去开始时间（进入时记录的时间），将结果加到逝去时间里就可以了。</span> </p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><strong><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">ctime</span><span style="COLOR: #000000">&gt;</span></strong><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;<img src="http://www.cppblog.com/Images/dot.gif"></span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;&nbsp;Stopped;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;&nbsp;Active&nbsp;:&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;Active,&nbsp;StopWatch,&nbsp;Stopped&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_107_378_Open_Image onclick="this.style.display='none'; Codehighlighter1_107_378_Open_Text.style.display='none'; Codehighlighter1_107_378_Closed_Image.style.display='inline'; Codehighlighter1_107_378_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_107_378_Closed_Image onclick="this.style.display='none'; Codehighlighter1_107_378_Closed_Text.style.display='none'; Codehighlighter1_107_378_Open_Image.style.display='inline'; Codehighlighter1_107_378_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_107_378_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_107_378_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff"><strong>public</strong></span><span style="COLOR: #000000"><strong>&nbsp;:<br></strong></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;&nbsp;sc::transition</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;EvReset,&nbsp;Active&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;reactions;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_216_217_Open_Image onclick="this.style.display='none'; Codehighlighter1_216_217_Open_Text.style.display='none'; Codehighlighter1_216_217_Closed_Image.style.display='inline'; Codehighlighter1_216_217_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_216_217_Closed_Image onclick="this.style.display='none'; Codehighlighter1_216_217_Closed_Text.style.display='none'; Codehighlighter1_216_217_Open_Image.style.display='inline'; Codehighlighter1_216_217_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<strong>Active()&nbsp;:&nbsp;elapsedTime_(&nbsp;</strong></span><span style="COLOR: #000000"><strong>0.0</strong></span><span style="COLOR: #000000"><strong>&nbsp;)</strong>&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_216_217_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_216_217_Open_Text><span style="COLOR: #000000"><strong>{}</strong></span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img id=Codehighlighter1_254_279_Open_Image onclick="this.style.display='none'; Codehighlighter1_254_279_Open_Text.style.display='none'; Codehighlighter1_254_279_Closed_Image.style.display='inline'; Codehighlighter1_254_279_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_254_279_Closed_Image onclick="this.style.display='none'; Codehighlighter1_254_279_Closed_Text.style.display='none'; Codehighlighter1_254_279_Open_Image.style.display='inline'; Codehighlighter1_254_279_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;&nbsp;ElapsedTime()&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span></strong><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_254_279_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_254_279_Open_Text><strong><span style="COLOR: #000000">{&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;&nbsp;elapsedTime_;&nbsp;}</span></strong></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img id=Codehighlighter1_310_335_Open_Image onclick="this.style.display='none'; Codehighlighter1_310_335_Open_Text.style.display='none'; Codehighlighter1_310_335_Closed_Image.style.display='inline'; Codehighlighter1_310_335_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_310_335_Closed_Image onclick="this.style.display='none'; Codehighlighter1_310_335_Closed_Text.style.display='none'; Codehighlighter1_310_335_Open_Image.style.display='inline'; Codehighlighter1_310_335_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #000000">&amp;</span></strong><span style="COLOR: #000000"><strong>&nbsp;ElapsedTime()</strong>&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_310_335_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_310_335_Open_Text><strong><span style="COLOR: #000000">{&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;&nbsp;elapsedTime_;&nbsp;}</span></strong></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff"><strong>private</strong></span><span style="COLOR: #000000"><strong>&nbsp;:<br></strong></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff"><strong>double</strong></span><span style="COLOR: #000000"><strong>&nbsp;&nbsp;elapsedTime_&nbsp;;<br></strong></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;&nbsp;Running&nbsp;:&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;Running,&nbsp;Active&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_436_875_Open_Image onclick="this.style.display='none'; Codehighlighter1_436_875_Open_Text.style.display='none'; Codehighlighter1_436_875_Closed_Image.style.display='inline'; Codehighlighter1_436_875_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_436_875_Closed_Image onclick="this.style.display='none'; Codehighlighter1_436_875_Closed_Text.style.display='none'; Codehighlighter1_436_875_Open_Image.style.display='inline'; Codehighlighter1_436_875_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_436_875_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_436_875_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff"><strong>public</strong></span><span style="COLOR: #000000"><strong>&nbsp;:<br></strong></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;&nbsp;sc::transition</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;EvStartStop,&nbsp;Stopped&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;reactions;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img id=Codehighlighter1_560_561_Open_Image onclick="this.style.display='none'; Codehighlighter1_560_561_Open_Text.style.display='none'; Codehighlighter1_560_561_Closed_Image.style.display='inline'; Codehighlighter1_560_561_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_560_561_Closed_Image onclick="this.style.display='none'; Codehighlighter1_560_561_Closed_Text.style.display='none'; Codehighlighter1_560_561_Open_Image.style.display='inline'; Codehighlighter1_560_561_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<strong>Running()&nbsp;:&nbsp;startTime_(&nbsp;std::time(&nbsp;</strong></span><strong><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&nbsp;)&nbsp;)&nbsp;</span></strong><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_560_561_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_560_561_Open_Text><span style="COLOR: #000000"><strong>{}</strong></span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;<strong>&nbsp;</strong></span><span style="COLOR: #000000"><strong>~</strong></span><span style="COLOR: #000000"><strong>Running()<br></strong></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img id=Codehighlighter1_582_830_Open_Image onclick="this.style.display='none'; Codehighlighter1_582_830_Open_Text.style.display='none'; Codehighlighter1_582_830_Closed_Image.style.display='inline'; Codehighlighter1_582_830_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_582_830_Closed_Image onclick="this.style.display='none'; Codehighlighter1_582_830_Closed_Text.style.display='none'; Codehighlighter1_582_830_Open_Image.style.display='inline'; Codehighlighter1_582_830_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_582_830_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_582_830_Open_Text><span style="COLOR: #000000"><strong>{<br></strong></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000"><strong>//</strong></span><span style="COLOR: #008000"><strong>&nbsp;与派生类可以访问它的基类相似，<br></strong></span><span style="COLOR: #008080">27</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000"><strong>//</strong></span><span style="COLOR: #008000"><strong>context&lt;&gt;()&nbsp;用来获得一个状态的直接或间接的上下文的访问权。<br></strong></span><span style="COLOR: #008080">28</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>&nbsp;</strong></span><span style="COLOR: #008000"><strong>//</strong></span><span style="COLOR: #008000"><strong>&nbsp;这可以是直接或间接的外层状态或状态机本身<br></strong></span><span style="COLOR: #008080">29</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;(例如，像这样:&nbsp;context&lt;&nbsp;StopWatch&nbsp;&gt;()).</span></strong><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">30</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>context</strong></span><strong><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;Active&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">().ElapsedTime()&nbsp;</span><span style="COLOR: #000000">+=</span></strong><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>std::difftime(&nbsp;std::time(&nbsp;</strong></span><span style="COLOR: #000000"><strong>0</strong></span><span style="COLOR: #000000"><strong>&nbsp;),&nbsp;startTime_&nbsp;);<br></strong></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;<strong>}</strong></span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff"><strong>private</strong></span><span style="COLOR: #000000"><strong>&nbsp;:<br></strong></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;<strong>&nbsp;std::&nbsp;time_t&nbsp;&nbsp;startTime_;<br></strong></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;</span></div>
<p style="TEXT-ALIGN: justify; MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0></span><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="TEXT-ALIGN: justify; MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; 这个状态机现在可以测量时间了，但是我们还不能看到结果。</span> </span><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="TEXT-ALIGN: justify; MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体">在这里，State-local&nbsp;storage的优势还没有完成显现出来。在FAQ项目&#8220;State-local&nbsp;storage酷在哪里？&#8221;中，会通过与一个没有用State-local&nbsp;storage的Stop&nbsp;Watch的比较来说明。</span> </span><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="TEXT-ALIGN: justify; MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="TEXT-ALIGN: justify; MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体"><br></span></span></p>
<p style="TEXT-ALIGN: justify; MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><strong><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体">1.4 在状态机外得到状态信息</span> </span></strong><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="TEXT-ALIGN: justify; MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体"><br></span></span></p>
<p style="TEXT-ALIGN: justify; MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; 为了取得测量的时间，我们需要一个从状态机外得到状态信息的机制。按我们现在的状态机设计，可以有两种方法。为简单起见，我们在这里用一个低效的方式：state_cast&lt;&gt;()(在StopWatch2.cpp中我们会用一个稍复杂一点的替代方法)（<span style="COLOR: #000080">译者注：在StopWatch2.cpp中是向状态机发送一个取得逝去时间的事件，从事件成员量中将逝去时间带回来</span> ），从字面意思就可以看出，它在语义上与dynamic_cast有点相似。例如，当我们调用myWatch.state_cast&lt;const&nbsp;Stpped&amp;&gt;()时，当状态机在Stopped状态时，我们会得到一个Stopped状态类的引用。否则，会抛出std::bad_cast异常。我们可以利用这个功能来实现一个StopWatch的成员函数，让它的结果返回逝去的时间。然而，我们不是先问一下状态机在什么状态，然后再去用不同的方法计算逝去时间，而是将计算放到Stopped和Running状态中，用一个接口来获得逝去逝去时间。</span> </span><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="TEXT-ALIGN: justify; MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt" class=p0>&nbsp;</p>
<div class=highlighter>
<ol class=highlighter-cpp>
    <li><strong><span class=preprocessor>#include&nbsp;&lt;iostream&gt;&nbsp;</span> </strong>
    <li class=alt><span></span>
    <li><span class=comment>//&nbsp;...&nbsp;</span>
    <li class=alt><span></span>
    <li><strong><span class=keyword>struct</span> <span>&nbsp;IElapsedTime&nbsp;</span> </strong>
    <li class=alt><strong><span>{&nbsp;</span> </strong>
    <li><strong><span>&nbsp;&nbsp;</span> <span class=keyword>virtual</span> <span>&nbsp;</span> <span class=datatypes>double</span> <span>&nbsp;ElapsedTime()&nbsp;</span> <span class=keyword>const</span> <span>&nbsp;=&nbsp;0;&nbsp;</span> </strong>
    <li class=alt><strong><span>};&nbsp;</span> </strong>
    <li><span></span>
    <li class=alt><span class=keyword>struct</span> <span>&nbsp;Active;&nbsp;</span>
    <li><span class=keyword>struct</span> <span>&nbsp;StopWatch&nbsp;:&nbsp;sc::state_machine&lt;&nbsp;StopWatch,&nbsp;Active&nbsp;&gt;&nbsp;</span>
    <li class=alt><span>{&nbsp;</span>
    <li><span>&nbsp;&nbsp;</span> <strong><span class=datatypes>double</span> <span>&nbsp;ElapsedTime()&nbsp;</span> <span class=keyword>const</span> <span>&nbsp;</span> </strong>
    <li class=alt><strong><span>&nbsp;&nbsp;{&nbsp;</span> </strong>
    <li><strong><span>&nbsp;&nbsp;&nbsp;&nbsp;</span> <span class=keyword>return</span> <span>&nbsp;state_cast&lt;&nbsp;</span> <span class=keyword>const</span> <span>&nbsp;IElapsedTime&nbsp;&amp;&nbsp;&gt;().ElapsedTime();&nbsp;</span> </strong>
    <li class=alt><strong><span>&nbsp;&nbsp;}&nbsp;</span> </strong>
    <li><span>};&nbsp;</span>
    <li class=alt><span></span>
    <li><span class=comment>//&nbsp;...&nbsp;</span>
    <li class=alt><span></span>
    <li><span class=keyword>struct</span> <span>&nbsp;Running&nbsp;:&nbsp;<strong>IElapsedTime,</strong> &nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;sc::simple_state&lt;&nbsp;Running,&nbsp;Active&nbsp;&gt;&nbsp;</span>
    <li><span>{&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;</span> <span class=keyword>public</span> <span>:&nbsp;</span>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;</span> <span class=keyword>typedef</span> <span>&nbsp;sc::transition&lt;&nbsp;EvStartStop,&nbsp;Stopped&nbsp;&gt;&nbsp;reactions;&nbsp;</span>
    <li class=alt><span></span>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;Running()&nbsp;:&nbsp;startTime_(&nbsp;std::time(&nbsp;0&nbsp;)&nbsp;)&nbsp;{}&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;~Running()&nbsp;</span>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>context&lt;&nbsp;Active&nbsp;&gt;().ElapsedTime()&nbsp;=&nbsp;ElapsedTime();&nbsp;</strong> </span>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;}</span>
    <li class=alt><strong><span>&nbsp;&nbsp;&nbsp;&nbsp;</span> <span class=keyword>virtual</span> <span>&nbsp;</span> <span class=datatypes>double</span> <span>&nbsp;ElapsedTime()&nbsp;</span> <span class=keyword>const</span> <span>&nbsp;</span> </strong>
    <li><strong><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;</span> </strong>
    <li class=alt><strong><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span class=keyword>return</span> <span>&nbsp;context&lt;&nbsp;Active&nbsp;&gt;().ElapsedTime()&nbsp;+&nbsp;</span> </strong>
    <li><strong><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::difftime(&nbsp;std::time(&nbsp;0&nbsp;),&nbsp;startTime_&nbsp;);&nbsp;</span> </strong>
    <li class=alt><strong><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span> </strong>
    <li><strong><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span class=keyword>private</span> <span>:&nbsp;</span> </strong>
    <li class=alt><strong><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::</span> <span class=datatypes>time_t</span> <span>&nbsp;startTime_;&nbsp;</span> </strong>
    <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;};</strong> &nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;</span> <span class=keyword>struct</span> <span>&nbsp;Stopped&nbsp;:<strong>&nbsp;IElapsedTime,</strong> &nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sc::simple_state&lt;&nbsp;Stopped,&nbsp;Active&nbsp;&gt;&nbsp;</span>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span class=keyword>typedef</span> <span>&nbsp;sc::transition&lt;&nbsp;EvStartStop,&nbsp;Running&nbsp;&gt;&nbsp;reactions;&nbsp;</span>
    <li><strong><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> </strong>
    <li class=alt><strong><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span class=keyword>virtual</span> <span>&nbsp;</span> <span class=datatypes>double</span> <span>&nbsp;ElapsedTime()&nbsp;</span> <span class=keyword>const</span> <span>&nbsp;</span> </strong>
    <li><strong><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;</span> </strong>
    <li class=alt><strong><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span class=keyword>return</span> <span>&nbsp;context&lt;&nbsp;Active&nbsp;&gt;().ElapsedTime();&nbsp;</span> </strong>
    <li><span><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</strong> &nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;};&nbsp;</span>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span> <span class=datatypes>int</span> <span>&nbsp;main()&nbsp;</span>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StopWatch&nbsp;myWatch;&nbsp;</span>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myWatch.initiate();&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>std::cout&nbsp;&lt;&lt;&nbsp;myWatch.ElapsedTime()&nbsp;&lt;&lt;&nbsp;</strong> </span><strong><span class=string>"\n"</span> </strong><span><strong>;</strong> &nbsp;</span>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myWatch.process_event(&nbsp;EvStartStop()&nbsp;);&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>std::cout&nbsp;&lt;&lt;&nbsp;myWatch.ElapsedTime()&nbsp;&lt;&lt;&nbsp;</strong> </span><strong><span class=string>"\n"</span> <span>;&nbsp;</span> </strong>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myWatch.process_event(&nbsp;EvStartStop()&nbsp;);&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>std::cout&nbsp;&lt;&lt;&nbsp;myWatch.ElapsedTime()&nbsp;&lt;&lt;&nbsp;</strong> </span><strong><span class=string>"\n"</span> <span>;&nbsp;</span> </strong>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myWatch.process_event(&nbsp;EvStartStop()&nbsp;);&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>std::cout&nbsp;&lt;&lt;&nbsp;myWatch.ElapsedTime()&nbsp;&lt;&lt;&nbsp;</strong> </span><strong><span class=string>"\n"</span> <span>;&nbsp;</span> </strong>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myWatch.process_event(&nbsp;EvReset()&nbsp;);&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>std::cout&nbsp;&lt;&lt;&nbsp;myWatch.ElapsedTime()&nbsp;&lt;&lt;&nbsp;</strong> </span><strong><span class=string>"\n"</span> <span>;&nbsp;</span> </strong>
    <li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span class=keyword>return</span> <span>&nbsp;0;&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;}</span> </li>
</ol>
</div>
<p style="TEXT-ALIGN: justify; MARGIN-TOP: 0pt; TEXT-INDENT: 21pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"></span></p>
<p style="TEXT-ALIGN: justify; MARGIN-TOP: 0pt; TEXT-INDENT: 21pt; MARGIN-BOTTOM: 0pt" class=p0><span style="FONT-FAMILY: '宋体'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体">为了确实看到被测量的时间，你应该想办法在main()中单步执行。StopWatch例子将这个程序扩展为一个交互式的终端程序了。</span></span></p>
</div>
<img src ="http://www.cppblog.com/jack-wang/aggbug/128501.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jack-wang/" target="_blank">小王</a> 2010-10-03 19:09 <a href="http://www.cppblog.com/jack-wang/archive/2010/10/03/128501.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Boost的状态机库教程(2) </title><link>http://www.cppblog.com/jack-wang/archive/2010/10/03/128500.html</link><dc:creator>小王</dc:creator><author>小王</author><pubDate>Sun, 03 Oct 2010 11:07:00 GMT</pubDate><guid>http://www.cppblog.com/jack-wang/archive/2010/10/03/128500.html</guid><wfw:comment>http://www.cppblog.com/jack-wang/comments/128500.html</wfw:comment><comments>http://www.cppblog.com/jack-wang/archive/2010/10/03/128500.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jack-wang/comments/commentRss/128500.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jack-wang/services/trackbacks/128500.html</trackback:ping><description><![CDATA[<div class=postTitle>转：<a href="http://www.cppblog.com/shaker/archive/2010/08/16/123598.html">http://www.cppblog.com/shaker/archive/2010/08/16/123598.html</a></div>
<p><span style="COLOR: #000000"><strong>1 基础主题：秒表</strong><br>&nbsp;&nbsp;&nbsp; 下面我们要为一个机械秒表建模一个状态机。这样一个秒表通常会有两个按钮。<br>&nbsp;&nbsp;&nbsp;&nbsp; * Start/Stop<br>&nbsp;&nbsp;&nbsp;&nbsp; * Reset<br>&nbsp;&nbsp;&nbsp;&nbsp; 同时有两种状态：<br>&nbsp;&nbsp;&nbsp;&nbsp; * Stoped: 表针停留在上次停止时的位置：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; o 按下Reset按钮，表针回退到0的位置。秒表保持在Stoped状态不变。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; o 按下Start/Stop按钮，秒表转到Running状态。<br>&nbsp;&nbsp;&nbsp;&nbsp; * Running: 表针在移动，并持续显示过去的时间：</span><br><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; o 按下Reset按钮，表针回退到0的位置，秒表转到停止状态。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; o 按下Start/Stop按钮，转到Stoped状态。<br>&nbsp;&nbsp;&nbsp; 下面是其UML图：</span><br><br><span style="COLOR: #800000"></span><img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/shaker/boost_statechart_pic2.jpg" width=682 height=231><br><br><span style="COLOR: #800000"><strong><span style="COLOR: #000000">1.1 定义状态和事件</span></strong><br><span style="COLOR: #000000">两个按钮可以建模为两个事件。进而，定义出必要的状态和初始状态。我们从下面的代码开始，以前的代码片段会陆续加入其中：</span></span></p>
<p><span style="COLOR: #800000"><span style="COLOR: #000000">&nbsp;</p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">boost</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">statechart</span><span style="COLOR: #000000">/</span><span style="COLOR: #0000ff">event</span><span style="COLOR: #000000">.hpp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">boost</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">statechart</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">state_machine.hpp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">boost</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">statechart</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">simple_state.hpp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;sc&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;boost::statechart;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img id=Codehighlighter1_211_212_Open_Image onclick="this.style.display='none'; Codehighlighter1_211_212_Open_Text.style.display='none'; Codehighlighter1_211_212_Closed_Image.style.display='inline'; Codehighlighter1_211_212_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_211_212_Closed_Image onclick="this.style.display='none'; Codehighlighter1_211_212_Closed_Text.style.display='none'; Codehighlighter1_211_212_Open_Image.style.display='inline'; Codehighlighter1_211_212_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;EvStartStop&nbsp;:&nbsp;sc::</span><span style="COLOR: #0000ff">event</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;EvStartStop&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_211_212_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_211_212_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000">;<br><img id=Codehighlighter1_253_254_Open_Image onclick="this.style.display='none'; Codehighlighter1_253_254_Open_Text.style.display='none'; Codehighlighter1_253_254_Closed_Image.style.display='inline'; Codehighlighter1_253_254_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_253_254_Closed_Image onclick="this.style.display='none'; Codehighlighter1_253_254_Closed_Text.style.display='none'; Codehighlighter1_253_254_Open_Image.style.display='inline'; Codehighlighter1_253_254_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;EvReset&nbsp;:&nbsp;sc::</span><span style="COLOR: #0000ff">event</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;EvReset&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_253_254_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_253_254_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;Active;<br><img id=Codehighlighter1_331_332_Open_Image onclick="this.style.display='none'; Codehighlighter1_331_332_Open_Text.style.display='none'; Codehighlighter1_331_332_Closed_Image.style.display='inline'; Codehighlighter1_331_332_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_331_332_Closed_Image onclick="this.style.display='none'; Codehighlighter1_331_332_Closed_Text.style.display='none'; Codehighlighter1_331_332_Open_Image.style.display='inline'; Codehighlighter1_331_332_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;StopWatch&nbsp;:&nbsp;sc::state_machine</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;StopWatch,&nbsp;Active&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_331_332_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_331_332_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;Stopped;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;这里的simple_state类模板可以接受4个参数：<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;-&nbsp;第3个参数指定内部的初始状态，如果有一个这样的状态的话。<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;在这里，Active有一个内部状态（Stoped），&nbsp;所以将这个内部<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;初始状态传给它的基类。<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;-&nbsp;第4个参数指定是否保留和保留什么类型历史<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;Active是最外层的状态，因此要把它所属的状态机类传给它</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;Active&nbsp;:&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000"><br><img id=Codehighlighter1_600_601_Open_Image onclick="this.style.display='none'; Codehighlighter1_600_601_Open_Text.style.display='none'; Codehighlighter1_600_601_Closed_Image.style.display='inline'; Codehighlighter1_600_601_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_600_601_Closed_Image onclick="this.style.display='none'; Codehighlighter1_600_601_Closed_Text.style.display='none'; Codehighlighter1_600_601_Open_Image.style.display='inline'; Codehighlighter1_600_601_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;Active,&nbsp;StopWatch,&nbsp;Stopped&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_600_601_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_600_601_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;Stopped&nbsp;和&nbsp;Running&nbsp;都把Active作为它们的上下文，这使他们嵌入到了Active状态中。</span><span style="COLOR: #008000"><br><img id=Codehighlighter1_715_716_Open_Image onclick="this.style.display='none'; Codehighlighter1_715_716_Open_Text.style.display='none'; Codehighlighter1_715_716_Closed_Image.style.display='inline'; Codehighlighter1_715_716_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_715_716_Closed_Image onclick="this.style.display='none'; Codehighlighter1_715_716_Closed_Text.style.display='none'; Codehighlighter1_715_716_Open_Image.style.display='inline'; Codehighlighter1_715_716_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;Running&nbsp;:&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;Running,&nbsp;Active&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_715_716_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_715_716_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000">;<br><img id=Codehighlighter1_772_773_Open_Image onclick="this.style.display='none'; Codehighlighter1_772_773_Open_Text.style.display='none'; Codehighlighter1_772_773_Closed_Image.style.display='inline'; Codehighlighter1_772_773_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_772_773_Closed_Image onclick="this.style.display='none'; Codehighlighter1_772_773_Closed_Text.style.display='none'; Codehighlighter1_772_773_Open_Image.style.display='inline'; Codehighlighter1_772_773_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;Stopped&nbsp;:&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;Stopped,&nbsp;Active&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_772_773_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_772_773_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;因为状态的上下文必须是一个完整的类型（不能单单是声明），<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;所以状态机必须要在&#8220;外层状态&#8221;之间先定义。<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;也就是说，我们需要从状态机开始，然后是最外层的状态，然后是其内部的状态，如此反复。<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;我们可以用广度或深度方式，再或是以两都混合的方式来进行定义。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_925_982_Open_Image onclick="this.style.display='none'; Codehighlighter1_925_982_Open_Text.style.display='none'; Codehighlighter1_925_982_Closed_Image.style.display='inline'; Codehighlighter1_925_982_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_925_982_Closed_Image onclick="this.style.display='none'; Codehighlighter1_925_982_Closed_Text.style.display='none'; Codehighlighter1_925_982_Open_Image.style.display='inline'; Codehighlighter1_925_982_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_925_982_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_925_982_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;StopWatch&nbsp;myWatch;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;myWatch.initiate();<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<p>这个代码已经可以编译了，但不会发生任何可察觉的事件。</p>
</span></span>
<img src ="http://www.cppblog.com/jack-wang/aggbug/128500.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jack-wang/" target="_blank">小王</a> 2010-10-03 19:07 <a href="http://www.cppblog.com/jack-wang/archive/2010/10/03/128500.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Boost的状态机库教程(1) </title><link>http://www.cppblog.com/jack-wang/archive/2010/10/03/128499.html</link><dc:creator>小王</dc:creator><author>小王</author><pubDate>Sun, 03 Oct 2010 11:06:00 GMT</pubDate><guid>http://www.cppblog.com/jack-wang/archive/2010/10/03/128499.html</guid><wfw:comment>http://www.cppblog.com/jack-wang/comments/128499.html</wfw:comment><comments>http://www.cppblog.com/jack-wang/archive/2010/10/03/128499.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jack-wang/comments/commentRss/128499.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jack-wang/services/trackbacks/128499.html</trackback:ping><description><![CDATA[<div class=postTitle>转：<a href="http://www.cppblog.com/shaker/archive/2010/08/16/123597.html">http://www.cppblog.com/shaker/archive/2010/08/16/123597.html</a><a id=viewpost1_TitleUrl class=postTitle2 href="http://www.cppblog.com/shaker/archive/2010/08/16/123597.html"><font color=#1a8bc8></font></a></div>
<strong>介绍</strong> <br>&nbsp;&nbsp;&nbsp; Boost状态机库一个应用程序框架，你可以用它将UML状态图快速的转换为可执行的c++代码，而不需要任何的代码生成器。它支持几乎所有的UML特征，可以直接了当的转换，并且转换后的c++代码就像对状态机进行一次文本描述一样具体可读性。<br><br><strong>如何阅读这个教程</strong> <br>&nbsp; &nbsp; 这个教程是以线性阅读的方式进行的章节设计。如果你是第一次看这个教程的话，你可以从头开始读，到你觉得了解的东西对你手头的任务来说已经足够时就停止。具体可以这样：<br>&nbsp; &nbsp; * 如果你的任务是要实现一个小的、简单的，并且有很少几个状态的状态机，那么下面的&#8220;初级主题：秒表&#8221;里所讲的就差不多够你用的了。<br>&nbsp; &nbsp; * 如果你要做一个有很多状态的大型状态机，你可以看一下&#8220;中级主题：数码相机&#8221;，那里的讲解可能对你有帮助。<br>&nbsp;&nbsp;&nbsp; * 最后，如果你是一个要创建异常复杂状态机的用户，或者是一个想要评估一个Boost状态机的设计师的话，你就要看一下&#8220;高级主题&#8221;部分。并且，我还强烈建议你看一下Rationle里的Limitions部分。<br><br><strong>Hello World!</strong> <br>&nbsp;&nbsp;&nbsp; 我们将要从一个最简单程序开始我们的第一步，状态图如下：<br><img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/shaker/boost_statechart_pic1.jpg" width=456 height=115><br>对于这个状态图，我们的实现代码如下： <br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">boost</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">statechart</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">state_machine.hpp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">boost</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">statechart</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">simple_state.hpp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;&nbsp;sc&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;boost::statechart;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;为了避免写public，下面声明的类型全部为struct。<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;如果你不在乎的话可以把它们都改成class。<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">10</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;我们需要先声明一下初始状态，这是因为我们要在定义状态机时使用它<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;但又不得不在状态机这后定义它。</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">12</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;&nbsp;Greeting;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;Boost.Statechart大量应用模板模式。<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;派生类必须将自己做为基类模板的第一个参数。<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;状态机必须要知道当其初始化后进行的第一个状态。<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;这就是为什么Greeting要做为每二个模板参数。<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;（译者注：也就是说Greeting状态是Machine状态机初始化后进入的第一个状态）</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">21</span><span style="COLOR: #008000"><img id=Codehighlighter1_499_500_Open_Image onclick="this.style.display='none'; Codehighlighter1_499_500_Open_Text.style.display='none'; Codehighlighter1_499_500_Closed_Image.style.display='inline'; Codehighlighter1_499_500_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_499_500_Closed_Image onclick="this.style.display='none'; Codehighlighter1_499_500_Closed_Text.style.display='none'; Codehighlighter1_499_500_Open_Image.style.display='inline'; Codehighlighter1_499_500_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;&nbsp;Machine&nbsp;:&nbsp;sc::state_machine</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;Machine,&nbsp;Greeting&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_499_500_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_499_500_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;对于每一个状态，我们需要为其指明：它属于哪一个状态机，它位于状态图的哪个位置。<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;我们用simple_state&lt;&gt;的上下文参数就可以完成这些指定了。<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;对于我们目前的这个简单的状态机来说，上下文就是状态机（Machine)<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;所以，Machine必须要做为simple_state的第二个模块参数。<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;（关于上下文参数的详细解释在下一个例子中有）</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;&nbsp;Greeting&nbsp;:&nbsp;sc::simple_state</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;Greeting,&nbsp;Machine&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img id=Codehighlighter1_747_1012_Open_Image onclick="this.style.display='none'; Codehighlighter1_747_1012_Open_Text.style.display='none'; Codehighlighter1_747_1012_Closed_Image.style.display='inline'; Codehighlighter1_747_1012_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_747_1012_Closed_Image onclick="this.style.display='none'; Codehighlighter1_747_1012_Closed_Text.style.display='none'; Codehighlighter1_747_1012_Open_Image.style.display='inline'; Codehighlighter1_747_1012_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_747_1012_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_747_1012_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;一旦状态机进行一个状态的时候，它就要创建一个相应状态类的对象（类实例）<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;只要状态机保持在这个状态下，这个对象就会一直存在。<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;最后，当状态机离开这个状态时，对象被销毁。<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;所以，一个状态的进入动作就是这个状态类的构造器，而它的退出动作则是它的析构类。&nbsp;&nbsp;&nbsp;Greeting()&nbsp;{&nbsp;std::cout&nbsp;&lt;&lt;&nbsp;&nbsp;"Hello&nbsp;World!\n"&nbsp;;&nbsp;}&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;进入</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">34</span><span style="COLOR: #008000"><img id=Codehighlighter1_965_1003_Open_Image onclick="this.style.display='none'; Codehighlighter1_965_1003_Open_Text.style.display='none'; Codehighlighter1_965_1003_Closed_Image.style.display='inline'; Codehighlighter1_965_1003_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_965_1003_Closed_Image onclick="this.style.display='none'; Codehighlighter1_965_1003_Closed_Text.style.display='none'; Codehighlighter1_965_1003_Open_Image.style.display='inline'; Codehighlighter1_965_1003_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">Greeting()&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_965_1003_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_965_1003_Open_Text><span style="COLOR: #000000">{&nbsp;std::cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Bye&nbsp;Bye&nbsp;World!\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;;&nbsp;}</span></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;退出</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">35</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="COLOR: #000000">}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;&nbsp;main()<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img id=Codehighlighter1_1028_1485_Open_Image onclick="this.style.display='none'; Codehighlighter1_1028_1485_Open_Text.style.display='none'; Codehighlighter1_1028_1485_Closed_Image.style.display='inline'; Codehighlighter1_1028_1485_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_1028_1485_Closed_Image onclick="this.style.display='none'; Codehighlighter1_1028_1485_Closed_Text.style.display='none'; Codehighlighter1_1028_1485_Open_Image.style.display='inline'; Codehighlighter1_1028_1485_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_1028_1485_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1028_1485_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;Machine&nbsp;myMachine;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;构造完状态机后，它并未开始运行。我们要通过调用它的initiate()来启动它。<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;同时，它也将触发它的初始状态（Greeting）的构造。</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">42</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;myMachine.initiate();<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;当我们离开main()函数时，myMachine将被销毁，这将导致它销毁它内部的所有活动的状态类。<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;（译者注：为什么会说所有？这是因为一个状态机可以同时</span><span style="COLOR: #008000">保持在多个状态中，可以参考&#8220;高级主题&#8221;部分）</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">45</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
这个程序会显示&#8220;<span class=string>Hello&nbsp;World!&#8221;和&#8220;</span> <span class=string>Bye&nbsp;Bye&nbsp;World!</span> <span class=string>&#8221;，然后退出。</span>
<img src ="http://www.cppblog.com/jack-wang/aggbug/128499.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jack-wang/" target="_blank">小王</a> 2010-10-03 19:06 <a href="http://www.cppblog.com/jack-wang/archive/2010/10/03/128499.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>vc7编译boost 1.33.1记录(转)</title><link>http://www.cppblog.com/jack-wang/archive/2008/10/27/65217.html</link><dc:creator>小王</dc:creator><author>小王</author><pubDate>Mon, 27 Oct 2008 08:53:00 GMT</pubDate><guid>http://www.cppblog.com/jack-wang/archive/2008/10/27/65217.html</guid><wfw:comment>http://www.cppblog.com/jack-wang/comments/65217.html</wfw:comment><comments>http://www.cppblog.com/jack-wang/archive/2008/10/27/65217.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jack-wang/comments/commentRss/65217.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jack-wang/services/trackbacks/65217.html</trackback:ping><description><![CDATA[在Visual Studio .NET 2003 命令提示下进入boost目录，如何进入tools\build\jam_src\，执行build.bat，编译成功得到bjam.exe<br><br>接着copy bjam.exe到 boost目录，执行bjam -sBOOST_ROOT=. -sTOOLS=vc7 "-sBUILD=debug release &lt;runtime-link&gt;static/dynamic"<br><br>漫长的等待后会在boost目录下的bin文件夹中生产一堆lib,复制到sdk的lib路径下，将boost目录添加的工程中。ok<br><br><br><br>上面是偶的经历，如果你不能编译成功，请参考下面的文章<br><br>原文出自：<a href="http://unknown-error.spaces.live.com/blog/cns!9B12A9BDE11A3428!142.entry#comment"><u><font color=#0000ff>http://unknown-error.spaces.live.com/blog/cns!9B12A9BDE11A3428!142.entry#comment</font></u></a><br><br><br><br>
<h4 class=TextColor1 id=subjcns!9B12A9BDE11A3428!142 style="MARGIN-BOTTOM: 0px">如何开始使用boost的跨平台thread库(Windows)</h4>
<div id=msgcns!9B12A9BDE11A3428!142>
<div><font size=3>boost主页：</font><a href="http://www.boost.org/"><font color=#0066a7 size=3><u>http://www.boost.org/</u></font></a></div>
<div><font size=3>在主页点击download进入sourceforge页面下载，当前最新版本为boost_1_33_1，有多种文件格式可供下载(包括.exe, .tar.gz等)，内容相同，都是boost_1_33_1的全部源代码。下载后解压(假设解压目录为D:\boost\boost_1_33_1)。</font></div>
<div><font size=3></font>&nbsp;</div>
<div><font size=3>boost中的大部分内容都可以直接源代码使用，而thread则需要首先编译出对应的库。</font></div>
<div><font size=3></font>&nbsp;</div>
<div><strong><font size=3>Windows XP平台：</font></strong></div>
<div><font size=3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 我的机器上安装了vc2003和vc2005，分别在D:\devenv\vs2003和D:\devenv\vs2005目录下</font></div>
<div><font size=3><strong>1. 编译jam（JAM是编译其他库的基础）<br></strong>启动命令行进入D:\boost\boost_1_33_1\tools\build\jam_src目录</font></div>
<div><font size=3>运行build即可</font></div>
<div><font size=3>运行结束后，将新出现bin.ntx86目录，我们所需要的bjam.exe就在该目录下。</font></div>
<div><font size=3></font>&nbsp;</div>
<div><font size=3>查看一下build文件的内容，发现其自动检测vc2003(vc7.1)的安装目录，然后调用vc7.1编译出的bjam.exe。（使用了VS71COMNTOOLS宏）</font></div>
<div><font size=3></font>&nbsp;</div>
<div><strong><font size=3>2. 编译thread库</font></strong></div>
<div><font size=3>启动命令行进入D:\boost\boost_1_33_1目录</font></div>
<div><font size=3>set VC71_ROOT=D:\devenv\vs2003\Vc7&nbsp; (此时没有自动检测，所以需要人工指定)</font></div>
<div><font size=3>tools\build\jam_src\bin.ntx86\bjam.exe --with-thread stage (只编译thread库)</font></div>
<div><font size=3></font>&nbsp;</div>
<div><font size=3>编译完成后，结果在D:\boost\boost_1_33_1\bin\boost\libs\thread\build目录下（包括debug/relase, dll/lib等）；另外，由于我们在编译时使用了stage选项，所以所有的结果都将被拷贝到D:\boost\boost_1_33_1\stage\lib目录下。</font></div>
<div><font size=3></font>&nbsp;</div>
<div><font size=3>总结一下，</font></div>
<div>动态库<br>libboost_thread-vc71-mt-gd-1_33_1.lib&nbsp; +&nbsp; boost_thread-vc71-mt-gd-1_33_1.dll (debug)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 42.4k + 88.0k<br>libboost_thread-vc71-mt-1_33_1.lib&nbsp;&nbsp;&nbsp;&nbsp; +&nbsp; boost_thread-vc71-mt-1_33_1.dll&nbsp;&nbsp;&nbsp; (release)&nbsp;&nbsp;&nbsp; 42.0k + 44.0k</div>
<div>静态库<br>libboost_thread-vc71-mt-gd-1_33_1.lib&nbsp; (debug)&nbsp;&nbsp;&nbsp; 2.61M<br>libboost_thread-vc71-mt-1_33_1.lib&nbsp;&nbsp;&nbsp;&nbsp; (release)&nbsp; 782k</div>
<div>静态库(runtime-link-static)<br>libboost_thread-vc71-mt-sgd-1_33_1.lib (debug)&nbsp;&nbsp;&nbsp; 2.19M<br>libboost_thread-vc71-mt-s-1_33_1.lib&nbsp;&nbsp; (release)&nbsp; 692k</div>
<div><br>s Static link to runtime. <br>g Debug runtime. <br>d Debug enabled code. </div>
<div>&nbsp;</div>
<div><font size=3></font>&nbsp;</div>
<div><strong><font size=3>3. 准备使用thread库</font></strong></div>
<div><font size=3>&nbsp;&nbsp;&nbsp; 选用编译得到的thread动态库。</font></div>
<div><font size=3>&nbsp;&nbsp;&nbsp; 将boost_thread-vc71-mt-gd-1_33_1.lib重命名为libboost_thread-vc71-mt-gd-1_33_1.lib(前面加了lib)</font></div>
<div><font size=3>&nbsp;&nbsp;&nbsp;&nbsp;将boost_thread-vc71-mt-1_33_1.lib重命名为libboost_thread-vc71-mt-1_33_1.lib(前面加了lib)</font></div>
<div><font size=3>&nbsp;&nbsp;&nbsp; 将这两个文件拷贝到D:\devenv\vs2003\Vc7\PlatformSDK\Lib</font></div>
<div><font size=3></font>&nbsp;</div>
<div><font size=3>&nbsp;&nbsp;&nbsp; 将boost_thread-vc71-mt-gd-1_33_1.dll拷贝到windows的system32目录下</font></div>
<div><font size=3>&nbsp;&nbsp;&nbsp; 将boost_thread-vc71-mt-1_33_1.dll拷贝到windows的system32目录下</font></div>
<div><font size=3></font>&nbsp;</div>
<div><font size=3>以上将thread的debug/release版本的共享库拷贝到系统目录下，接下去就可以使用了。</font></div>
<div><font size=3></font>&nbsp;</div>
<div><strong><font size=3>4. 使用thread库</font></strong></div>
<div><font size=3>&nbsp;&nbsp;&nbsp; 在vc2003中创建一个空的Win32 Console Project，代码：</font></div>
<div><font size=3>#include &lt;boost/thread/thread.hpp&gt;<br>#include &lt;iostream&gt;</font></div>
<div><font size=3>void hello()<br>{<br>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "Hello world, I'm a thread!" &lt;&lt; std::endl;<br>}</font></div>
<div><font size=3>main()<br>{<br>&nbsp;&nbsp;&nbsp; boost::thread thrd(&amp;hello);<br>&nbsp;&nbsp;&nbsp; thrd.join();<br>}</font></div>
<div><font size=3></font>&nbsp;</div>
<div><font size=3>&nbsp;&nbsp;&nbsp; 设置Project属性的Code Geneartion------Runtime Library 为 /MDd 或者 /MD</font></div>
<div><font size=3>&nbsp;&nbsp;&nbsp; 设置Project属性的General-------Additional Include Directories为 D:\boost\boost_1_33_1</font></div>
<div><font size=3></font>&nbsp;</div>
<div><font size=3>&nbsp;&nbsp;&nbsp; 编译运行即可。</font></div>
</div>
<img src ="http://www.cppblog.com/jack-wang/aggbug/65217.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jack-wang/" target="_blank">小王</a> 2008-10-27 16:53 <a href="http://www.cppblog.com/jack-wang/archive/2008/10/27/65217.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>