boost状态机学习二(秒表)
基础主题:秒表
下面我们要为一个机械秒表建模一个状态机。这样一个秒表通常会有两个按钮。
* Start/Stop
* Reset
同时有两种状态:
* Stoped: 表针停留在上次停止时的位置:
o 按下Reset按钮,表针回退到0的位置。秒表保持在Stoped状态不变。
o 按下Start/Stop按钮,秒表转到Running状态。
* Running: 表针在移动,并持续显示过去的时间:
o 按下Reset按钮,表针回退到0的位置,秒表转到停止状态。
o 按下Start/Stop按钮,转到Stoped状态。
下面是其UML图:1.1 定义状态和事件
两个按钮可以建模为两个事件。进而,定义出必要的状态和初始状态。我们从下面的代码开始,以前的代码片段会陆续加入其中: #include <boost/statechart/event.hpp>
#include <boost/statechart/state_machine.hpp>
#include <boost/statechart/simple_state.hpp> namespace sc = boost::statechart; struct EvStartStop : sc::event< EvStartStop > {};
struct EvReset : sc::event< EvReset > {}; struct Active;
struct StopWatch : sc::state_machine< StopWatch, Active > {}; struct Stopped; // 这里的simple_state类模板可以接受4个参数:
// - 第3个参数指定内部的初始状态,如果有一个这样的状态的话。
// 在这里,Active有一个内部状态(Stoped), 所以将这个内部
// 初始状态传给它的基类。
// - 第4个参数指定是否保留和保留什么类型历史 // Active是最外层的状态,因此要把它所属的状态机类传给它
struct Active : sc::simple_state<
Active, StopWatch, Stopped > {}; // Stopped 和 Running 都把Active作为它们的上下文,这使他们嵌入到了Active状态中。struct Running : sc::simple_state< Running, Active > {};
struct Stopped : sc::simple_state< Stopped, Active > {}; // 因为状态的上下文必须是一个完整的类型(不能单单是声明),
// 所以状态机必须要在“外层状态”之间先定义。
// 也就是说,我们需要从状态机开始,然后是最外层的状态,然后是其内部的状态,如此反复。
// 我们可以用广度或深度方式,再或是以两都混合的方式来进行定义。 int main()
{
StopWatch myWatch;
myWatch.initiate();
return ;
}
这个代码已经可以编译了,但不会发生任何可察觉的事件。
boost状态机学习二(秒表)的更多相关文章
- boost asio 学习(二)了解boost::bind
2.了解boost::bind使用boost::bind封装一个函数,考虑以下例子示例2a #include <iostream> #include <boost/bind.hpp& ...
- c++ boost库学习二:内存管理->智能指针
写过C++的人都知道申请和释放内存组合new/delete,但同时很多人也会在写程序的时候忘记释放内存导致内存泄漏.如下所示: int _tmain(int argc, _TCHAR* argv[]) ...
- boost库学习之开篇
本系列文章使用boost_1.58.0版本. 一.欢迎使用boost C++库 boost致力于提供一个免费的.便携的源代码级的库. 我们重视那些与C++标准一起工作良好的库.boost库将要成为一个 ...
- emberjs学习二(ember-data和localstorage_adapter)
emberjs学习二(ember-data和localstorage_adapter) 准备工作 首先我们加入ember-data和ember-localstorage-adapter两个依赖项,使用 ...
- ReactJS入门学习二
ReactJS入门学习二 阅读目录 React的背景和基本原理 理解React.render() 什么是JSX? 为什么要使用JSX? JSX的语法 如何在JSX中如何使用事件 如何在JSX中如何使用 ...
- TweenMax动画库学习(二)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- Hbase深入学习(二) 安装hbase
Hbase深入学习(二) 安装hbase This guidedescribes setup of a standalone hbase instance that uses the local fi ...
- Struts2框架学习(二) Action
Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...
- Python学习二:词典基础详解
作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...
随机推荐
- WPF RadioButton 转换
模型 public class people { public string name{get;set;} public bool? sex{get;set;} } 转换器 namespace Hel ...
- Word Ladder 未完成
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- C语言 百炼成钢18
//题目52:用递归打印以下图形 //* //*.*. //*..*..*.. //*...*...*...*... //*....*....*....*....*.... #include<s ...
- Difference Between TIF and TIFF
TIF vs TIFF Many people are confused with similar file extensions that only differ by a single lette ...
- LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree
LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...
- 移动统计工具Flurry
网址:http://www.flurry.com/ 1注册和下载对应SDK 2集成SDK 3自定义统计项 至于怎么使用,后期会更新
- 蓝牙技术BlueTooth
转载网址:http://blog.csdn.net/dxdxsmy/article/details/7790568 蓝牙核心架构概念的理解请参考上面的网址.
- webstorm调试Node的时候配置
点击Edit Configurations的这个的配置:(不能点击是因为目前你选中的不是项目)
- [BZOJ3504][CQOI2014]危桥(最大流)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3504 分析:很容易想到最大流,但如果S-a1,S-b1,a2-T,b2-T这样跑S-T最大流判 ...
- jQuery基础之(六)jQuery浏览器的兼容性(以CSS3特写兼容举例)
CSS3充分吸收多年了web发展的需求,吸收了很多新颖的特性.例如border-radius css3特性中最令人兴奋的莫过于选择器的增强,例如属性选择器可以根据某个属性值来选择标记,位置选择器可以根 ...