boost 状态机--中级篇以Camera为例,讲解了Boost状态机模型的使用方法;而本文的主要:
- 对原文中的Camera模型略加修改与完善;
- 写出了完整代码;可编译运行;
- 演示各个对象(事件对象和状态对象)的生命周期;
- 更清晰的模块化各个组件(事件、状态); 故可以当作一个简单的代码框架;
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完成的。
完整的状态图
工程目录结构
.
├── bin
├── Makefile
└── src
├── camera
│ ├── camera.cpp
│ └── camera.hpp
├── event
│ ├── events.cpp
│ └── events.hpp
└── state
├── istate.hpp
├── notshooting
│ ├── configuring.cpp
│ ├── configuring.hpp
│ ├── idle.cpp
│ ├── idle.hpp
│ ├── notshooting.cpp
│ └── notshooting.hpp
└── shooting
├── focused.cpp
├── focused.hpp
├── focusing.cpp
├── focusing.hpp
├── shooting.cpp
├── shooting.hpp
├── storing.cpp
└── storing.hpp
输出结果
penghuser@pch:~/linux/study_src/statchart/camera$ ./bin/run
Construct Camera
Enter NotShooting
Enter Idle.
CurrentState -------->Idle
Construct EvConfig
Idle::react( const EvConfig & evConfig )
Exit Idle
Enter Configuring
Destruct EvConfig
CurrentState -------->Configuring
Construct EvConfig
Exit Configuring
[Transition Action] : Camera goes into Power Saving Mode
Enter Idle.
Destruct EvConfig
CurrentState -------->Idle
Construct EvShutterHalf
NotShooting::react(const EvShutterHalf & evShutterHalf)
Guard: isBatteryLow() is true
Exit Idle
Exit NotShooting
Enter Shooting
Enter Focusing
Destruct EvShutterHalf
CurrentState -------->Focusing
Press Shutter Full before focused
Construct EvShutterFull
Destruct EvShutterFull
CurrentState -------->Focusing
Construct EvInFocus
Focusing::react(const EvInFocus & evInFocus)
Exit Focusing
[Transition Action] : Camera focused on objects
Enter Focused
Focused::react(const EvShutterFull & evShutterFull)
Guard: isMemAvail() is true
Exit Focused
[Transition Action] : Memory allocated for storing the picture.
Enter Storing
Destruct EvShutterFull
Destruct EvInFocus
CurrentState -------->Storing
Construct EvShutterRelease
Storing::react(const EvShutterRelease & EvShutterRelease)
Discard EvShutterRelease
Destruct EvShutterRelease
CurrentState -------->Storing
Construct EvStored
Storing::react(const EvStored & evStored)
Exit Storing
Exit Shooting
Enter NotShooting
Enter Idle.
Destruct EvStored
CurrentState -------->Idle
Destruct Camera
Exit Idle
Exit NotShooting
源码
github地址:https://github.com/penghuster/study_src/tree/master/statchart/camera