boost 事件状态机事件是自己定义的吗

trackbacks-0
介绍 &&& Boost状态机库一个应用程序框架,你可以用它将UML状态图快速的转换为可执行的c++代码,而不需要任何的代码生成器。它支持几乎所有的UML特征,可以直接了当的转换,并且转换后的c++代码就像对状态机进行一次文本描述一样具体可读性。如何阅读这个教程 & & 这个教程是以线性阅读的方式进行的章节设计。如果你是第一次看这个教程的话,你可以从头开始读,到你觉得了解的东西对你手头的任务来说已经足够时就停止。具体可以这样:& & * 如果你的任务是要实现一个小的、简单的,并且有很少几个状态的状态机,那么下面的“初级主题:秒表”里所讲的就差不多够你用的了。& & * 如果你要做一个有很多状态的大型状态机,你可以看一下“中级主题:数码相机”,那里的讲解可能对你有帮助。&&& * 最后,如果你是一个要创建异常复杂状态机的用户,或者是一个想要评估一个Boost状态机的设计师的话,你就要看一下“高级主题”部分。并且,我还强烈建议你看一下Rationle里的Limitions部分。Hello World! &&& 我们将要从一个最简单程序开始我们的第一步,状态图如下:对于这个状态图,我们的实现代码如下:
&1#include&&boost/statechart/state_machine.hpp&&2#include&&boost/statechart/simple_state.hpp&&3#include&&iostream&&4&5namespace&&sc&=&boost::&6&7//&为了避免写public,下面声明的类型全部为struct。&8//&如果你不在乎的话可以把它们都改成class。&9<span style="COLOR: #//&我们需要先声明一下初始状态,这是因为我们要在定义状态机时使用它<span style="COLOR: #//&但又不得不在状态机这后定义它。<span style="COLOR: #<span style="COLOR: #struct&&G<span style="COLOR: #<span style="COLOR: #//&Boost.Statechart大量应用模板模式。<span style="COLOR: #//&派生类必须将自己做为基类模板的第一个参数。<span style="COLOR: #//<span style="COLOR: #//&状态机必须要知道当其初始化后进行的第一个状态。<span style="COLOR: #//&这就是为什么Greeting要做为每二个模板参数。<span style="COLOR: #//&(译者注:也就是说Greeting状态是Machine状态机初始化后进入的第一个状态)<span style="COLOR: #struct&&Machine&:&sc::state_machine&&Machine,&Greeting&&&{};<span style="COLOR: #<span style="COLOR: #//&对于每一个状态,我们需要为其指明:它属于哪一个状态机,它位于状态图的哪个位置。<span style="COLOR: #//&我们用simple_state&&的上下文参数就可以完成这些指定了。<span style="COLOR: #//&对于我们目前的这个简单的状态机来说,上下文就是状态机(Machine)<span style="COLOR: #//&所以,Machine必须要做为simple_state的第二个模块参数。<span style="COLOR: #//&(关于上下文参数的详细解释在下一个例子中有)<span style="COLOR: #struct&&Greeting&:&sc::simple_state&&Greeting,&Machine&&<span style="COLOR: #{<span style="COLOR: #&&//&一旦状态机进行一个状态的时候,它就要创建一个相应状态类的对象(类实例)<span style="COLOR: #&&//&只要状态机保持在这个状态下,这个对象就会一直存在。<span style="COLOR: #&&//&最后,当状态机离开这个状态时,对象被销毁。<span style="COLOR: #&&//&所以,一个状态的进入动作就是这个状态类的构造器,而它的退出动作则是它的析构类。&&&Greeting()&{&std::cout&&&&&"Hello&World!\n"&;&}&&//&进入<span style="COLOR: #&&~Greeting()&{&std::cout&&&&&"Bye&Bye&World!\n"&;&}&&//&退出<span style="COLOR: #};<span style="COLOR: #<span style="COLOR: #int&&main()<span style="COLOR: #{<span style="COLOR: #&&Machine&myM<span style="COLOR: #&&//&构造完状态机后,它并未开始运行。我们要通过调用它的initiate()来启动它。<span style="COLOR: #&&//&同时,它也将触发它的初始状态(Greeting)的构造。<span style="COLOR: #&&myMachine.initiate();<span style="COLOR: #&&//&当我们离开main()函数时,myMachine将被销毁,这将导致它销毁它内部的所有活动的状态类。<span style="COLOR: #&&//&(译者注:为什么会说所有?这是因为一个状态机可以同时保持在多个状态中,可以参考&#8220;高级主题&#8221;部分)<span style="COLOR: #&&return&&<span style="COLOR: #;<span style="COLOR: #}
这个程序会显示&#8220;Hello&World!&#8221;和&#8220; Bye&Bye&World! &#8221;,然后退出。
阅读(2328)
25262728293031123456789101112131415171920212223242526272829301234
留言簿(25)
随笔分类(52)
随笔档案(60)
系统设计系列文章
笑笑小生的博客
有一些HGE的技巧
就是这里啦
大名鼎鼎的网站 全e文
感觉是个大杂烩 不专精 不过但凡国内的程序员大多也会去那看看的
VC资料网站 收集的还算多的 不过还需要向CodeProject学习
算是曾今国内众多外挂高手的根据地吧 不过人心散了... 难现往日辉煌
积分与排名
阅读排行榜
评论排行榜原文见http://www.cs.washington.edu/research/projects/uns/F9/src/boost_1_37_0/libs/statechart/doc/tutorial.html
原文以Camera为例,讲解了Boost状态机模型的使用方法;而本文的主要:
对原文中的Camera模型略加修改与完善;写出了完整代码;可编译运行;演示各个对象(事件对象和状态对象)的生命周期;更清晰的模块化各个组件(事件、状态); 故可以当作一个简单的代码框架;
另外:这里只涵盖了中级topics,对于高级内容,后续再写。
Camera的按键:
配置键(Config Button): &按此键会产生一个EvConfig事件,EvConfig事件用于在空闲状态(Idle)和配置状态(Configuring)之间切换;快门键(Shutter Button): 可以半按(半按时产生一个EvShutterHalf事件)、全按(全按时产生一个EvShutterFull事件)以及释放(释放时产生一个EvShutterRelease事件);
Camera的状态:
为了提高模块化(不同模块之间松耦合,不同team可以同时开发),将Camera分为两个大的状态:
非拍摄状态(NotShooting): &包含两个子状态,空闲状态(Idle)和配置状态(Configuring);EvConfig事件用于二者之间的切换;拍摄状态(Shooting): & & & 包含对焦状态(Focusing)、聚焦状态(Focused)和照片存储状态(Storing);
Camera工作过程:
Camera初始状态是NotShooting,NotShooting的初始状态是Idle,所以Camera启动时会自动进入Idle状态;处于Idle状态时,若User按下Config Button(产生一个EvConfig事件),Camera进入配置状态(Configuring);反之,处于Configuring状态时,若User按下Config Button,Camera进入Idle状态并启用省电模式(powerSavingMode);处于NotShooting(Idle或Configuring)状态时,若用户半按快门键(产生一个EvShutterHalf事件)并且电量充足(isBatteryLow为非)Camera进入对焦状态(Focusing),镜头前后移动以自动对焦;处于Focusing状态时,若自动对焦成功,Camera显示聚焦的物体(displayFocused),并在自己内部产生一个EvConfig事件,然后Camera进入聚焦状态(Focused);处于Focused状态时,User全按快门(产生一个EvShutterFull事件),若内存充足则分配内存(allocMem)并进入照片存储状态(Storing);若内存不足则停在Foused状态;
事件的延迟处理:
在Camera的工作过程中,当Camera处于Focusing状态时User全按快门(产生一个EvShutterFull事件),由于对焦尚未完成,不能立即处理EvShutterFull事件。若丢弃此事件,那么在对焦完成后(进入Focused状态),User不得不再全按一次快门,造成不必要的麻烦;一个解决方案是延迟处理这个事件,其过程是这样的:
当Camera处于Focusing状态User全按快门时,产生一个EvShutterFull事件,拷贝此事件(拷贝构造函数);把事件的拷贝放入一个单独的queue中;销毁原EvShutterFull事件(析构函数);当Camera离开Focusing状态进入Focused状态时,从queue中取出事件(EvShutterFull的拷贝)并处理;销毁EvShutterFull事件的拷贝(析构函数);
这一切都是通过一个deferral完成的;
完整的状态图:
[root@localhost camera]# tree
├── bin
├── Makefile
└── src
& & ├── camera
& & │ & ├── camera.cpp
& & │ & └── camera.hpp
& & ├── event
& & │ & ├── events.cpp
& & │ & └── events.hpp
& & └── state
& & & & ├── istate.hpp
& & & & ├── notshoot
& & & & │ & ├── configuring.cpp
& & & & │ & ├── configuring.hpp
& & & & │ & ├── idle.cpp
& & & & │ & ├── idle.hpp
& & & & │ & ├── notshooting.cpp
& & & & │ & └── notshooting.hpp
& & & & └── shoot
& & & & & & ├── focused.cpp
& & & & & & ├── focused.hpp
& & & & & & ├── focusing.cpp
& & & & & & ├── focusing.hpp
& & & & & & ├── shooting.cpp
& & & & & & ├── shooting.hpp
& & & & & & ├── storing.cpp
& & & & & & └── storing.hpp
src/camera/camera.cpp
src/event/*.cpp
src/state/notshoot/idle.cpp
src/state/notshoot/notshooting.cpp
src/state/notshoot/configuring.cpp
src/state/shoot/shooting.cpp
src/state/shoot/focusing.cpp
src/state/shoot/focused.cpp
src/state/shoot/storing.cpp
EXE=bin/run
all: clean build
mkdir -p bin
g++ -g -o $(EXE) $(INCLUDES) $(SOURCES) $(LIBLINKS)
rm -f $(EXE)
src/event/events.hpp
#ifndef __EVENTS_HPP__
#define __EVENTS_HPP__
#include &boost/statechart/event.hpp&
using namespace boost::
class EvConfig : public event& EvConfig &
EvConfig();
~EvConfig();
class EvShutterHalf : public event& EvShutterHalf &
EvShutterHalf();
~EvShutterHalf();
class EvShutterFull : public event& EvShutterFull &
EvShutterFull();
EvShutterFull(const EvShutterFull & other);
~EvShutterFull();
class EvShutterRelease : public event& EvShutterRelease &
EvShutterRelease();
~EvShutterRelease();
class EvInFocus : public event& EvInFocus &
EvInFocus();
~EvInFocus();
class EvStored : public event& EvStored &
EvStored();
~EvStored();
src/event/events.cpp
#include &iostream&
#include &events.hpp&
//============EvConfig============//
EvConfig::EvConfig()
cout&&&Construct EvConfig&&&
EvConfig::~EvConfig()
cout&&&Destruct EvConfig&&&
//============EvShutterHalf============//
EvShutterHalf::EvShutterHalf()
cout&&&Construct EvShutterHalf&&&
EvShutterHalf::~EvShutterHalf()
cout&&&Destruct EvShutterHalf&&&
//============EvShutterFull============//
EvShutterFull::EvShutterFull()
cout&&&Construct EvShutterFull&&&
EvShutterFull::EvShutterFull(const EvShutterFull & other)
cout&&&Copy Construct EvShutterFull&&&
EvShutterFull::~EvShutterFull()
cout&&&Destruct EvShutterFull&&&
//============EvShutterRelease============//
EvShutterRelease::EvShutterRelease()
cout&&&Construct EvShutterRelease&&&
EvShutterRelease::~EvShutterRelease()
cout&&&Destruct EvShutterRelease&&&
//============EvInFocus============//
EvInFocus::EvInFocus()
cout&&&Construct EvInFocus&&&
EvInFocus::~EvInFocus()
cout&&&Destruct EvInFocus&&&
//============EvStored============//
EvStored::EvStored()
cout&&&Construct EvStored&&&
EvStored::~EvStored()
cout&&&Destruct EvStored&&&
src/camera/camera.hpp
#ifndef __CAMERA_HPP__
#define __CAMERA_HPP__
#include &boost/statechart/state_machine.hpp&
#include &../event/events.hpp&
using namespace boost::
//Yuanguo: forward declaration
class NotS
class Camera : public state_machine& Camera, NotShooting &
~Camera();
bool isMemAvail()
bool isBatteryLow()
string getCurState()
//transition actions
void displayFocused(const EvInFocus & evInFocus);
void allocMem(const EvShutterFull & evShutterFull);
void powerSavingMode(const EvConfig & evConfig);
src/camera/camera.cpp
#include &iostream&
#include &camera.hpp&
#include &../state/istate.hpp&
//Yuanguo: although the full definition of the initial state &NotShooting& is not needed when
//declare Camera (thus only forward declaration is needed), the full definition of the states
//are necessary for
camera.initiate();
//thus, include NotShooting and its inner st
#include &../state/notshoot/notshooting.hpp&
#include &../state/notshoot/idle.hpp&
#include &../state/shoot/shooting.hpp&
Camera::Camera()
cout&&&Construct Camera&&&
Camera::~Camera()
cout&&&Destruct Camera&&&
bool Camera::isMemAvail() const
bool Camera::isBatteryLow() const
string Camera::getCurState() const
return string(&CurrentState ------& &) + state_cast& const IState & &().getStateName();
void Camera::displayFocused(const EvInFocus & evInFocus)
cout&&&[Transition Action]: Camera focused on objects&&&
void Camera::allocMem(const EvShutterFull & evShutterFull)
cout&&&[Transition Action]: Memory allocated for storing the picture&&&
void Camera::powerSavingMode(const EvConfig & evConfig)
cout&&&[Transition Action]: Camera goes into Power Saving Mode&&&
int main()
camera.initiate();
cout&&camera.getCurState()&&
camera.process_event(EvConfig());
//模拟按Config键
cout&&camera.getCurState()&&
camera.process_event(EvConfig());
//&span style=&font-family: Arial, Helvetica, sans-&&模拟按Config键&/span&
cout&&camera.getCurState()&&
camera.process_event(EvShutterHalf());
//模拟半按快门
cout&&camera.getCurState()&&
cout&&&Press Shutter Full before focused&&&
camera.process_event(EvShutterFull());
//在对焦完成之前,模拟全按快门
cout&&camera.getCurState()&&
camera.process_event(EvInFocus());
//模拟对焦完成事件
cout&&camera.getCurState()&&
camera.process_event(EvShutterRelease()); //模拟释放快门
cout&&camera.getCurState()&&
camera.process_event(EvStored());
//模拟存储完成
cout&&camera.getCurState()&&
src/state/notshoot/notshooting.hpp
#ifndef __NOTSHOOTING_HPP__
#define __NOTSHOOTING_HPP__
#include &boost/statechart/simple_state.hpp&
#include &boost/statechart/custom_reaction.hpp&
#include &../istate.hpp&
#include &../../event/events.hpp&
//Yuanguo: NotShooting is the initial state of Camera, and Camera is the context of NotShooting, so
1. forward declaration of NotShooting is needed when defining Camera, see camera.
2. full definition of Camera is needed when defining NotShooting, thus include camera.
#include &../../camera/camera.hpp&
using namespace boost::
//Yuanguo:
class NotShooting : public simple_state& NotShooting, Camera, Idle &, public IState
typedef custom_reaction& EvShutterHalf &
NotShooting();
~NotShooting();
string getStateName()
result react(const EvShutterHalf & evShutterHalf);
src/state/notshoot/notshooting.cpp
#include &iostream&
#include &notshooting.hpp&
//Yuanguo:
//We need a full definition of Shooting in line
return transit&Shooting&();
//Focusing is the initial state of Shooting, thus we have to include focusing.hpp
//here, otherwise, compiler will complain about error like
incomplete type &#39;boost::statechart::simple_state ...
... MostDerived = NotShooting, Context = Camera ....&#39;
//inclusion of non-initial states of Shoot
#include &../shoot/shooting.hpp&
#include &../shoot/focusing.hpp&
NotShooting::NotShooting()
cout&&&Enter NotShooting&&&
NotShooting::~NotShooting()
cout&&&Exit NotShooting&&&
string NotShooting::getStateName() const
return string(&NotShooting&);
result NotShooting::react(const EvShutterHalf & evShutterHalf)
cout&&&NotShooting::react(const EvShutterHalf & evShutterHalf), &;
if( context& Camera &().isBatteryLow() )
cout&&&Guard: isBatteryLow() is true&&&
//Yuanguo: We cannot react to the event ourselves, so we forward it
//to our outer state (this is also the default if a state
//defines no reaction for a given event).
return forward_event();
cout&&&Guard: isBatteryLow() is false&&&
return transit&Shooting&();
//no transition action
src/state/notshoot/idle.hpp
#ifndef __IDLE_HPP__
#define __IDLE_HPP__
#include &boost/statechart/simple_state.hpp&
#include &boost/statechart/custom_reaction.hpp&
#include &../istate.hpp&
#include &../../event/events.hpp&
//Yuanguo: Idle is the initial state of NotShooting, and NotShooting is the context of Idle, so
1. forward declaration of Idle is needed when defining NotShooting, see notshooting.
2. full definition of NotShooting is needed when defining Idle, thus include notshooting.
#include &notshooting.hpp&
using namespace boost::
class Idle : public simple_state& Idle, NotShooting &, public IState
typedef custom_reaction& EvConfig &
string getStateName()
result react(const EvConfig & evConfig);
src/state/notshoot/idle.cpp
#include &iostream&
#include &configuring.hpp&
#include &idle.hpp&
Idle::Idle()
cout&&&Enter Idle&&&
Idle::~Idle()
cout&&&Exit Idle&&&
string Idle::getStateName() const
return string(&Idle&);
result Idle::react(const EvConfig & evConfig)
cout&&&Idle::react(const EvConfig & evConfig)&&&
return transit&Configuring&(); //no transition action
src/state/notshoot/configuring.hpp
#ifndef __CONFIGURING_HPP__
#define __CONFIGURING_HPP__
#include &boost/statechart/simple_state.hpp&
#include &boost/statechart/transition.hpp&
#include &../../event/events.hpp&
#include &../istate.hpp&
//Yuanguo: NotShooting is the context of Configuring, so
full definition of NotShooting is needed when defining Configuring, thus include notshooting.
#include &notshooting.hpp&
//Yuanguo: full definition of Idle is needed in line
typedef transition& EvConfig, Idle &
#include &idle.hpp&
using namespace boost::
class Configuring : public simple_state& Configuring, NotShooting &, public IState
//Yuanguo: a short way for:
typedef custom_reaction& EvConfig &
result react( const EvConfig & evConfig)
return transit& Idle &(&Camera::powerSavingMode, evConfig);
//transition action is Camera::powerSavingMode()
//return transit& Idle &();
//no transition action
typedef transition& EvConfig, Idle, Camera, &Camera::powerSavingMode &
//transition action is Camera::powerSavingMode()
//typedef transition& EvConfig, Idle &
//no transition action
Configuring();
~Configuring();
string getStateName()
src/state/notshoot/configuring.cpp
#include &iostream&
#include &configuring.hpp&
Configuring::Configuring()
cout&&&Enter Configuring&&&
Configuring::~Configuring()
cout&&&Exit Configuring&&&
string Configuring::getStateName() const
return string(&Configuring&);
src/state/shoot/shooting.hpp
#ifndef __SHOOTING_HPP__
#define __SHOOTING_HPP__
#include &boost/statechart/simple_state.hpp&
#include &boost/statechart/custom_reaction.hpp&
#include &boost/statechart/transition.hpp&
#include &../istate.hpp&
#include &../../event/events.hpp&
//Yuanguo: Camera is the context of Shooting, so
full definition of Camera is needed when defining Shooting, thus include camera.
#include &../../camera/camera.hpp&
using namespace boost::
//Yuanguo: forward declaration
class Shooting : public simple_state& Shooting, Camera, Focusing &, public IState
typedef custom_reaction& EvShutterRelease &
Shooting();
~Shooting();
string getStateName()
result react(const EvShutterRelease & evShutterRelease);
src/state/shoot/shooting.cpp
#include &iostream&
#include &shooting.hpp&
//Yuanguo:
//We need a full definition of NotShooting in line
return transit& NotShooting &();
//Idle is the initial state of NotShooting, thus we have to include idle.hpp
//here, otherwise, compiler will complain about error like
incomplete type &#39;boost::statechart::simple_state ...
... MostDerived = NotShooting, Context = Camera ....&#39;
//inclusion of non-initial states of NotShoot
#include &../notshoot/notshooting.hpp&
#include &../notshoot/idle.hpp&
Shooting::Shooting()
cout&&&Enter Shooting&&&
Shooting::~Shooting()
cout&&&Exit Shooting&&&
string Shooting::getStateName() const
return string(&Shooting&);
result Shooting::react(const EvShutterRelease & evShutterRelease)
cout&&&Shooting::react(const EvShutterRelease & evShutterRelease)&&&
return transit& NotShooting &();
src/state/shoot/focusing.hpp
#ifndef __FOCUSING_HPP__
#define __FOCUSING_HPP__
#include &boost/statechart/simple_state.hpp&
#include &boost/statechart/custom_reaction.hpp&
#include &boost/mpl/list.hpp&
#include &boost/statechart/deferral.hpp&
#include &../../event/events.hpp&
#include &../istate.hpp&
//Yuanguo: Shooting is the context of Focusing, so
full definition of Shooting is needed when defining Focusing, thus include
#include &shooting.hpp&
using namespace boost::
class Focusing : public simple_state& Focusing, Shooting &, public IState
//Yuanguo:
//if the user fully press shutter when the camera is still in focusing (has
//not focused yet), we defer the event until focused.
//that means:
//when an event of EvShutterFull comes:
copy the EvShutterFull event by copy-
put the copy
destruct the EvShutterF
//when camera exits Focusing state (e.g. gets into Focused state):
let Focused state process the
empty the queue and destru
typedef boost::mpl::list&
custom_reaction& EvInFocus &,
deferral& EvShutterFull & &
Focusing();
~Focusing();
string getStateName()
result react(const EvInFocus & evInFocus);
src/state/shoot/focusing.cpp
#include &iostream&
#include &focusing.hpp&
#include &focused.hpp&
#include &../../camera/camera.hpp&
Focusing::Focusing()
cout&&&Enter Focusing&&&
Focusing::~Focusing()
cout&&&Exit Focusing&&&
string Focusing::getStateName() const
return string(&Focusing&);
result Focusing::react(const EvInFocus & evInFocus)
cout&&&Focusing::react(const EvInFocus & evInFocus)&&&
return transit& Focused &(&Camera::displayFocused, evInFocus); //transition action is Camera::displayFocused()
src/state/shoot/focused.hpp
#ifndef __FOCUSED_HPP__
#define __FOCUSED_HPP__
#include &boost/statechart/simple_state.hpp&
#include &boost/statechart/custom_reaction.hpp&
#include &../../event/events.hpp&
#include &../istate.hpp&
//Yuanguo: Shooting is the context of Focused, so
full definition of Shooting is needed when defining Focused, thus include shooting.
#include &shooting.hpp&
class Focused : public simple_state& Focused, Shooting &, public IState
typedef custom_reaction & EvShutterFull &
Focused();
~Focused();
string getStateName()
result react(const EvShutterFull & evShutterFull );
src/state/shoot/focused.cpp
#include &iostream&
#include &focused.hpp&
#include &storing.hpp&
#include &../../camera/camera.hpp&
Focused::Focused()
cout&&&Enter Focused&&&
Focused::~Focused()
cout&&&Exit Focused&&&
string Focused::getStateName() const
return string(&Focused&);
result Focused::react(const EvShutterFull & evShutterFull )
cout&&&Focused::react(const EvShutterFull & evShutterFull ), &;
if( context& Camera &().isMemAvail() )
cout&&&Guard: isMemAvail() is true&&&
return transit&Storing&(&Camera::allocMem, evShutterFull); //transition action is Camera::allocMem()
cout&&&Guard: isMemAvail() is false&&&
//Yuanguo: Indicate that the event can be discarded. So, the
// dispatch algorithm will stop looking for a reaction
// and the machine remains in the Focused state.
return discard_event();
src/state/shoot/storing.hpp
#ifndef __STORING_HPP__
#define __STORING_HPP__
#include &boost/statechart/simple_state.hpp&
#include &boost/statechart/custom_reaction.hpp&
#include &boost/statechart/deferral.hpp&
#include &boost/mpl/list.hpp&
#include &../../event/events.hpp&
#include &../istate.hpp&
//Yuanguo: Shooting is the context of Storing, so
full definition of Shooting is needed when defining Storing, thus include shooting.
#include &shooting.hpp&
using namespace boost::
class Storing : public simple_state& Storing, Shooting &, public IState
//Yuanguo: we have multiple reactions
typedef boost::mpl::list&
custom_reaction & EvStored &,
custom_reaction & EvShutterRelease & &
Storing();
~Storing();
string getStateName()
result react(const EvStored & evStored);
result react(const EvShutterRelease & evShutterRelease);
src/state/shoot/storing.cpp
#include &iostream&
#include &storing.hpp&
//Yuanguo:
//We need a full definition of NotShooting in line
return transit& NotShooting &();
//Idle is the initial state of NotShooting, thus we have to include idle.hpp
//here, otherwise, compiler will complain about error like
incomplete type &#39;boost::statechart::simple_state ...
... MostDerived = NotShooting, Context = Camera ....&#39;
//inclusion of non-initial states of NotShoot
#include &../notshoot/notshooting.hpp&
#include &../notshoot/idle.hpp&
Storing::Storing()
cout&&&Enter Storing&&&
Storing::~Storing()
cout&&&Exit Storing&&&
string Storing::getStateName() const
return string(&Storing&);
result Storing::react(const EvStored & evStored)
cout&&&Storing::react(const EvStored & evStored)&&&
//Yuanguo: goto NotShooting when
return transit& NotShooting &();
result Storing::react(const EvShutterRelease & evShutterRelease)
cout&&&Storing::react(const EvShutterRelease & evShutterRelease)&&&
cout&&&Discard EvShutterRelease&&&
//Yuanguo: Don&#39;t go to NotShooting until storing is finished, even if the user
//releases the shutter
return discard_event();
测试(注意事件和状态对象的生命周期):
[root@localhost camera]# make test
rm -f bin/run
mkdir -p bin
g++ -g -o bin/run
src/camera/camera.cpp src/event/*.cpp src/state/notshoot/idle.cpp src/state/notshoot/notshooting.cpp src/state/notshoot/configuring.cpp src/state/shoot/shooting.cpp src/state/shoot/focusing.cpp src/state/shoot/focused.cpp src/state/shoot/storing.cpp
Construct Camera
Enter NotShooting
Enter Idle
CurrentState ------& Idle
//初始时进入Idle状态
Construct EvConfig
//模拟按Config键,用于Idle到Configuring的切换
Idle::react(const EvConfig & evConfig)
//处理EvConfig事件
Enter Configuring
Destruct EvConfig
//在进入“下一个状态”后,销毁事件
CurrentState ------& Configuring
Construct EvConfig
//再此模拟按Config键,用于Configuring到Idle的切换
Exit Configuring
[Transition Action]: Camera goes into Power Saving Mode
//transition action在退出“前一个状态”之后,进入“后一个状态”之前执行
Enter Idle
Destruct EvConfig
CurrentState ------& Idle
Construct EvShutterHalf
//模拟半按快门
NotShooting::react(const EvShutterHalf & evShutterHalf), Guard: isBatteryLow() is false
//处理EvShutterHalf事件
Exit NotShooting
Enter Shooting
Enter Focusing
Destruct EvShutterHalf
CurrentState ------& Focusing
//进入对焦状态
Press Shutter Full before focused
//模拟提前全按快门
Construct EvShutterFull
//&span style=&font-family: Arial, Helvetica, sans-&&在对焦完成之前按快门,构造EvShutterFull事件&/span&
Copy Construct EvShutterFull
//拷贝EvShutterFull事件并放入queue
Destruct EvShutterFull
//销毁原EvShutterFull对象
CurrentState ------& Focusing
Construct EvInFocus
//模拟对焦完成
Focusing::react(const EvInFocus & evInFocus)
Exit Focusing
[Transition Action]: Camera focused on objects //transition action在退出“前一个状态”之后,进入“后一个状态”之前执行
&/span&Enter Focused
Focused::react(const EvShutterFull & evShutterFull ), Guard: isMemAvail() is true
//进入“后一个状态”时,立即处理被延迟的事件
Exit Focused
//处理被延迟的EvShutterFull事件,导致Camera退出Focused状态
[Transition Action]: Memory allocated for storing the picture
Enter Storing
Destruct EvShutterFull
//销毁EvShutterFull的拷贝
Destruct EvInFocus
CurrentState ------& Storing
Construct EvShutterRelease
//模拟释放快门
Storing::react(const EvShutterRelease & evShutterRelease)
Discard EvShutterRelease
//EvShutterRelease被丢弃,因为storing还未完成
Destruct EvShutterRelease
CurrentState ------& Storing
//仍然处于Storing状态
Construct EvStored
//模拟storing完成事件
Storing::react(const EvStored & evStored)
Exit Storing
//storing完成,退出Storing状态
Exit Shooting
//层层退出外围状态(这里只有一层),直到“前一个状态”和“后一个状态”的“最小公共外围状态”
Enter NotShooting
Enter Idle
//层层进入“后一个状态”(这里只有一层)
Destruct EvStored
CurrentState ------& Idle
Destruct Camera
//销毁状态机时,层层退出所有状态;
Exit NotShooting
[root@localhost camera]#
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:9115次
排名:千里之外
原创:15篇
(1)(4)(1)(5)(7)

我要回帖

更多关于 c语言 事件驱动状态机 的文章

 

随机推荐