C++实现状态机
原理
状态机(State Machine)是一种用来描述系统行为的模型,它由一组有限的状态(State)和在这些状态之间的转换(Transition)组成。状态机可以根据输入事件(Event)或者条件(Predicate)来改变当前的状态,从而实现不同的功能。
状态机有两种常见的类型:有限状态机(Finite State Machine,FSM)和层次状态机(Hierarchical State Machine,HSM)。FSM是最基本的状态机模型,它只有一个当前状态,当输入事件发生时,它会根据预定义的转换规则来切换到另一个状态。HSM是在FSM的基础上增加了层次结构,它允许一个状态拥有子状态,并且可以继承父状态的行为。HSM可以更好地组织复杂的系统逻辑,避免状态爆炸的问题。
应用场景
状态机在编程中有很多应用场景,例如:
- 游戏开发中,可以用状态机来描述角色的行为和动画,如站立、跑动、跳跃、攻击等。
- 网络编程中,可以用状态机来描述通信协议的交互过程,如TCP/IP协议中的三次握手、四次挥手等。
- 嵌入式开发中,可以用状态机来描述硬件设备的工作模式和控制逻辑,如电梯、自动售货机、红绿灯等。
- 编译器开发中,可以用状态机来描述词法分析和语法分析的过程,如正则表达式、BNF文法等。
代码实现
C++语言本身并没有提供直接支持状态机的语法或库,但是可以利用C++的面向对象特性和函数对象特性来实现状态机的功能。下面给出两个参考链接,分别介绍了如何用C++11实现一个通用的HSM框架和一个简单的FSM类。
- C++状态机框架实现_三体问题的博客-CSDN博客:这篇博客介绍了如何用C++11实现一个通用的HSM框架,它支持创建多个状态和父子关系,支持同步和异步发送事件,支持绑定函数对象、类成员函数、lambda表达式等作为事件处理回调函数。它还给出了一个使用该框架实现一个简单游戏逻辑的示例代码。
- github经典C++状态机(fsm)源代码剖析 - CSDN博客:这篇博客介绍了如何用C++11实现一个简单的FSM类,它是一个单头文件的轻量级库,支持绑定函数对象、类成员函数、lambda表达式等作为事件处理回调函数。它还给出了一个使用该类实现一个小游戏逻辑的示例代码。
- 下面将使用C++11实现一个简单的FSM类,然后用它来实现一个简单的状态机示例。请看以下代码:
// fsm.hpp: a simple FSM class
#include <functional>
#include <map>
#include <vector>
class FSM {
public:
// define the type of the event handler function
using EventHandler = std::function<void(const std::vector<std::string>&)>;
// define the type of the transition map
using TransitionMap = std::map<std::pair<int, std::string>, int>;
// constructor: initialize the current state and the transition map
FSM(int initial_state, const TransitionMap& transitions) : current_state(initial_state), transitions(transitions) {}
// register an event handler for a given state and event
void on(int state, const std::string& event, EventHandler handler) {
handlers[{state, event}] = handler;
}
// trigger an event with optional arguments
void trigger(const std::string& event, const std::vector<std::string>& args = {}) {
// find the next state according to the current state and the event
auto iter = transitions.find({current_state, event});
if (iter != transitions.end()) {
int next_state = iter->second;
// find the event handler for the current state and the event
auto iter2 = handlers.find({current_state, event});
if (iter2 != handlers.end()) {
EventHandler handler = iter2->second;
// call the event handler with the arguments
handler(args);
}
// update the current state
current_state = next_state;
}
}
private:
int current_state; // the current state of the FSM
TransitionMap transitions; // the map of transitions between states and events
std::map<std::pair<int, std::string>, EventHandler> handlers; // the map of event handlers for each state and event
};
// main.cpp: a simple example of using the FSM class
#include <iostream>
#include "fsm.hpp"
// define some states and events for the example
enum State { OFF, ON };
enum Event { SWITCH };
int main() {
// create an FSM object with the initial state and the transition map
FSM fsm(OFF, {
{{OFF, SWITCH}, ON},
{{ON, SWITCH}, OFF}
});
// register some event handlers for each state and event
fsm.on(OFF, SWITCH, [](const std::vector<std::string>& args) {
std::cout << "Turning on" << std::endl;
});
fsm.on(ON, SWITCH, [](const std::vector<std::string>& args) {
std::cout << "Turning off" << std::endl;
});
// trigger some events to test the FSM logic
fsm.trigger(SWITCH); // output: Turning on
fsm.trigger(SWITCH); // output: Turning off
fsm.trigger(SWITCH); // output: Turning on
return 0;
}
这是一个简单的状态机示例,它模拟了一个开关的行为。你可以运行这个代码来看看效果。如果你想修改或扩展这个示例,你可以根据你的需要添加更多的状态、事件和事件处理函数。
希望这些信息对你有帮助。