热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

BoostStateChart实现状态机秒表例程

Boost提供了状态机的实现接口,采用了CRTP技术实现,下面以秒表为例子实现一个状态机,这是一个官方的例子,也可以参考资料

 

Boost 提供了状态机的实现接口,采用了CRTP技术实现,下面以秒表为例子实现一个状态机,这是一个官方的例子,也可以参考资料:Boost Statechart 庫,状态机的状态转换图如下所示:

实现代码如下: 

#include
#include
#include // std::cout
#include // std::thread
#include <string>
#include

#include

#include

#include

#include
event.hpp>
#include

#include

#include

#include
#include using namespace std;
namespace sc &#61; boost::statechart;class EvStartStop : public sc::event
{
};
class EvReset : public sc::event
{
};
/*虚类&#xff0c;用于状态机获取状态*/
class IElapsedTime
{
public:virtual double elapsedTime() const &#61; 0;virtual ~IElapsedTime(){};
};
class Active;
/*秒表&#xff0c;状态机*/
class StopWatch : public sc::state_machine
{
public:double elapsedTime() const{return state_cast<const IElapsedTime &>().elapsedTime();}
};
class Stopped;
/*活动状态*/
class Active : public IElapsedTime, public sc::state
{
public:typedef sc::transition reactions;Active(my_context ctx): my_base(ctx),elapsed_time_(0){cout <<"Entry Active" << endl;}~Active(){cout <<"Exit Active" << endl;}double elapsedTime() const{return elapsed_time_;}double& elapsedTime(){return elapsed_time_;}private:double elapsed_time_;
};
/*计时状态*/
class Running : public IElapsedTime, public sc::state
{
public:typedef sc::transition reactions;Running(my_context ctx): my_base(ctx),start_time_(std::time(0)){cout <<"Entry Running" << endl;}~Running(){context().elapsedTime() &#61; elapsedTime();cout <<"Exit Running" << endl;}virtual double elapsedTime() const{return context().elapsedTime()&#43; std::difftime(std::time(0), start_time_);}private:std::time_t start_time_;
};
/*停止状态*/
class Stopped : public IElapsedTime, public sc::state
{
public:typedef sc::transition reactions;Stopped(my_context ctx): my_base(ctx){cout <<"Entry Stopped" << endl;}~Stopped(){cout <<"Exit Stopped" << endl;}virtual double elapsedTime() const{return context().elapsedTime();}};int main(int argc, char** argv)
{StopWatch myWatch;myWatch.initiate();cout
<<"---" << endl;myWatch.process_event(EvStartStop());for(int i &#61; 0; i <10; i&#43;&#43;){boost::this_thread::sleep(boost::posix_time::seconds(1)); //休眠1秒std::cout <<"time:" <"\n";}myWatch.process_event(EvStartStop());boost::this_thread::sleep(boost::posix_time::seconds(1));std::cout <<"current time:" <"\n";cout <<"---" << endl;myWatch.process_event(EvReset());std::cout <<"reset time:" <"\n";cout <<"---" << endl;return 0;
}

 运行效果如下&#xff1a;

Entry Active
Entry Stopped
---
Exit Stopped
Entry Running
time:
1
time:
2
time:
3
time:
4
time:
5
time:
6
time:
7
time:
8
time:
9
time:
10
Exit Running
Entry Stopped
current time:
10
---
Exit Stopped
Exit Active
Entry Active
Entry Stopped
reset time:
0
---
Exit Stopped
Exit Active

 

转:https://www.cnblogs.com/cv-pr/p/7598342.html



推荐阅读
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文介绍如何使用 NSTimer 实现倒计时功能,详细讲解了初始化方法、参数配置以及具体实现步骤。通过示例代码展示如何创建和管理定时器,确保在指定时间间隔内执行特定任务。 ... [详细]
  • 题目Link题目学习link1题目学习link2题目学习link3%%%受益匪浅!-----&# ... [详细]
  • 本文将深入探讨如何在不依赖第三方库的情况下,使用 React 处理表单输入和验证。我们将介绍一种高效且灵活的方法,涵盖表单提交、输入验证及错误处理等关键功能。 ... [详细]
  • Redux入门指南
    本文介绍Redux的基本概念和工作原理,帮助初学者理解如何使用Redux管理应用程序的状态。Redux是一个用于JavaScript应用的状态管理库,特别适用于React项目。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • C++: 实现基于类的四面体体积计算
    本文介绍如何使用C++编程语言,通过定义类和方法来计算由四个三维坐标点构成的四面体体积。文中详细解释了四面体体积的数学公式,并提供了两种不同的实现方式。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 如何高效创建和使用字体图标
    在Web和移动开发中,为什么选择字体图标?主要原因是其卓越的性能,可以显著减少HTTP请求并优化页面加载速度。本文详细介绍了从设计到应用的字体图标制作流程,并提供了专业建议。 ... [详细]
  • 本文探讨了在使用Azure Active Directory进行用户身份验证时,结合AddAuthentication和RequireAuthenticatedUser的必要性及其潜在冗余问题。 ... [详细]
author-avatar
phpxiaoxuesheng
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有