c++状态机实现示例

普通状态机的实现

#include <iostream>
#include <string>
#include <unordered_map>
#include <functional>

using namespace std;

// 状态机状态的枚举
enum class State {
    Idle,
    Active,
    Suspended,
    Downloading,
    Uploading,
    Error
};

// 事件的枚举
enum class Event {
    Start,
    Stop,
    Suspend,
    Resume,
    DownloadStart,
    DownloadComplete,
    UploadStart,
    UploadComplete,
    ErrorEncountered
};

// 状态机类
class StateMachine {
public:
    StateMachine() : currentState(State::Idle) {}

    // 处理事件的方法
    void handleEvent(Event event) {
        auto it = stateTransitionTable.find(make_pair(currentState, event));
        if (it != stateTransitionTable.end()) {
            // 执行状态转移和相应的操作
            auto transition = it->second;
            currentState = transition.first;
            transition.second(); // 执行与状态转移相关的操作
            cout << "Transition: " << stateToString(currentState) << endl;
        } else {
            cout << "Invalid transition from " << stateToString(currentState) << " on event " << eventToString(event) << endl;
        }
    }

private:
    State currentState;

    // 状态转移表,使用unordered_map存储当前状态和事件到下一状态及其对应的处理函数
    unordered_map<pair<State, Event>, pair<State, function<void()>>> stateTransitionTable {
        { {State::Idle, Event::Start}, {State::Active, [](){ cout << "Machine started." << endl; }} },
        { {State::Idle, Event::ErrorEncountered}, {State::Error, [](){ cout << "Error encountered. Transition to Error state." << endl; }} },
        { {State::Active, Event::Stop}, {State::Idle, [](){ cout << "Machine stopped." << endl; }} },
        { {State::Active, Event::Suspend}, {State::Suspended, [](){ cout << "Machine suspended." << endl; }} },
        { {State::Suspended, Event::Resume}, {State::Active, [](){ cout << "Machine resumed." << endl; }} },
        { {State::Active, Event::DownloadStart}, {State::Downloading, [](){ cout << "Downloading started." << endl; }} },
        { {State::Downloading, Event::DownloadComplete}, {State::Active, [](){ cout << "Downloading completed." << endl; }} },
        { {State::Active, Event::UploadStart}, {State::Uploading, [](){ cout << "Uploading started." << endl; }} },
        { {State::Uploading, Event::UploadComplete}, {State::Active, [](){ cout << "Uploading completed." << endl; }} }
        // 可根据需求继续添加更多状态和事件的转移规则
    };

    // 辅助方法,将状态和事件转换为字符串以便输出
    string stateToString(State state) {
        switch (state) {
            case State::Idle: return "Idle";
            case State::Active: return "Active";
            case State::Suspended: return "Suspended";
            case State::Downloading: return "Downloading";
            case State::Uploading: return "Uploading";
            case State::Error: return "Error";
            default: return "Unknown";
        }
    }

    string eventToString(Event event) {
        switch (event) {
            case Event::Start: return "Start";
            case Event::Stop: return "Stop";
            case Event::Suspend: return "Suspend";
            case Event::Resume: return "Resume";
            case Event::DownloadStart: return "DownloadStart";
            case Event::DownloadComplete: return "DownloadComplete";
            case Event::UploadStart: return "UploadStart";
            case Event::UploadComplete: return "UploadComplete";
            case Event::ErrorEncountered: return "ErrorEncountered";
            default: return "Unknown";
        }
    }
};

int main() {
    StateMachine machine;

    // 通过不同的事件触发状态机的状态变化和相关操作
    machine.handleEvent(Event::Start);
    machine.handleEvent(Event::DownloadStart);
    machine.handleEvent(Event::DownloadComplete);
    machine.handleEvent(Event::UploadStart);
    machine.handleEvent(Event::UploadComplete);
    machine.handleEvent(Event::Suspend);
    machine.handleEvent(Event::Resume);
    machine.handleEvent(Event::Stop);
    machine.handleEvent(Event::ErrorEncountered);

    return 0;
}

状态模式的状态机的实现

个人认为非常不实用,开销大,不简洁,个人理解也就是易扩展这个优点,并没有感知到其他的优势。
除了那种真的有大几十种上百种的状态,也许会考虑下这种方式
理论性大于实用性

相关推荐

  1. c++状态实现示例

    2024-07-11 21:44:03       24 阅读
  2. 状态实现单词统计

    2024-07-11 21:44:03       35 阅读
  3. 状态模式-C++实现

    2024-07-11 21:44:03       50 阅读
  4. lua 实现一个 StateMachine状态

    2024-07-11 21:44:03       20 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 21:44:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 21:44:03       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 21:44:03       58 阅读
  4. Python语言-面向对象

    2024-07-11 21:44:03       69 阅读

热门阅读

  1. Mac上pyenv的安装及使用

    2024-07-11 21:44:03       18 阅读
  2. 【C++】智能指针

    2024-07-11 21:44:03       24 阅读
  3. 【TS】Typescript 的类

    2024-07-11 21:44:03       23 阅读
  4. 说一下浏览器中的强缓存和协商缓存的区别

    2024-07-11 21:44:03       24 阅读
  5. 【Redis 如何实现分级缓存】

    2024-07-11 21:44:03       20 阅读
  6. Rust开发环境搭建

    2024-07-11 21:44:03       24 阅读
  7. E10.【C语言】练习:编写一个猜数字游戏

    2024-07-11 21:44:03       19 阅读
  8. k8s 容器环境下的镜像如何转换为docker 使用

    2024-07-11 21:44:03       26 阅读