一个很有代表性的boost状态机实例

#include <boost/statechart/event.hpp>
#include <boost/statechart/state_machine.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/transition.hpp>
#include <iostream>

namespace sc = boost::statechart;

struct Event1 : sc::event<Event1>
{
};

struct Event2 : sc::event<Event2>
{
};

struct SimpleState1;

struct StateMachine1 : sc::state_machine<StateMachine1, SimpleState1>
{
	StateMachine1(void)
	{
		std::cout << "construct StateMachine1" << std::endl;
	}

	~StateMachine1(void)
	{
		std::cout << "distruct StateMachine1" << std::endl;
	}
};

struct SimpleState1_1;

struct SimpleState1 : sc::simple_state<SimpleState1, StateMachine1, SimpleState1_1>
{
	typedef sc::transition<Event1, SimpleState1> reactions;

	SimpleState1(void)
	{
		std::cout << "enter SimpleState1" << std::endl;
	}

	~SimpleState1(void)
	{
		std::cout << "leave SimpleState1" << std::endl;
	}
};

struct SimpleState1_2;

struct SimpleState1_1 : sc::simple_state<SimpleState1_1, SimpleState1>
{
	typedef sc::transition<Event2, SimpleState1_2> reactions;

	SimpleState1_1(void)
	{
		std::cout << "enter SimpleState1_1" << std::endl;
	}

	~SimpleState1_1(void)
	{
		std::cout << "leave SimpleState1_1" << std::endl;
	}
};

struct SimpleState1_2 : sc::simple_state<SimpleState1_2, SimpleState1>
{
	typedef sc::transition<Event2, SimpleState1_1> reactions;

	SimpleState1_2(void)
	{
		std::cout << "enter SimpleState1_2" << std::endl;
	}

	~SimpleState1_2(void)
	{
		std::cout << "leave SimpleState1_2" << std::endl;
	}
};

int main(int argc, char ** argv)
{
	StateMachine1 myMachine;
	myMachine.initiate();
	std::cout << "----- before process Event2" << std::endl;
	myMachine.process_event(Event2());
	std::cout << "----- after process Event2" << std::endl;
	std::cout << "----- before process Event1" << std::endl;
	myMachine.process_event(Event1());
	std::cout << "----- after process Event1" << std::endl;
	return 0;
}

运行结果

construct StateMachine1
enter SimpleState1
enter SimpleState1_1
----- before process Event2
leave SimpleState1_1
enter SimpleState1_2
----- after process Event2
----- before process Event1
leave SimpleState1_2
leave SimpleState1
enter SimpleState1
enter SimpleState1_1
----- after process Event1
distruct StateMachine1
leave SimpleState1_1
leave SimpleState1
请按任意键继续. . .



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值