多实例tinyfsm

不支持多实例的tinyfsm

tinyfsm (https://github.com/digint/tinyfsm)是一款基于c++模板实现的状态机,因为模板所以效率很高,但又因为是模板,所以无法支持多实例,据说可以通过静态模板支持多实例,但静态模板需要在代码阶段就定义出来,那这多实例也太鸡肋了。因此基于tinyfsm,魔改了一款支持多实例的fsm。

支持多实例的mi_tinyfsm

mi是multi instance的缩写,多实例tinyfsm摒弃了模板(高效率),并且使用了STL,但真正支持多实例,相当于在功能和效率之间的一个取舍吧。

举个栗子

参考tinyfsm经典例子switch实现的多实例switch,输入0或者1对switch0或者switch1进行开关状态切换。完整源码见以下链接。

https://gitee.com/cyy1205/mi_tinyfsmm

关键代码

/*
 * MI_TinyFSM - Multi Instansce tinyfsm
 *
 * Salute to tinyfsm https://github.com/digint/tinyfsm
 *
 * Copyright (c) 2023-2024 yuping.li
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef MI_TINYFSM_H
#define MI_TINYFSM_H

#include <functional>
#include <map>
#include <string>


class MiTinyfsm {
public:
    struct State {
        std::string name;
        std::function<void()> entry;
        std::function<void()> exit;
        std::map<std::string, std::function<void()>> events;
    };

    explicit MiTinyfsm(State &initial_state) : current_state_ptr(&initial_state) { }

    virtual void enter_initial_state() const {
        if (current_state_ptr->entry) {
            current_state_ptr->entry();
        }
    }

    virtual void start() {
        enter_initial_state();
    }

    virtual void dispatch(const std::string& event) {
        if (current_state_ptr->events.count(event)) {
            current_state_ptr->events[event]();
        }
    }

protected:
    virtual void transit(State& state) {
        if (current_state_ptr->exit) {
            current_state_ptr->exit();
        }
        current_state_ptr = &state;
        if (current_state_ptr->entry) {
            current_state_ptr->entry();
        }
    }

    virtual void transit(State& state, const std::function<void()>& action) {
        if (current_state_ptr->exit) {
            current_state_ptr->exit();
        }
        action();
        current_state_ptr = &state;
        if (current_state_ptr->entry) {
            current_state_ptr->entry();
        }
    }

    virtual void transit(State& state, const std::function<void()>& action, const std::function<bool()>& condition) {
        if (condition()) {
            transit(state, action);
        }
    }

    State* current_state_ptr{nullptr};
};

#endif //MI_TINYFSM_H

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Oracle多实例是指在一个物理服务器上运行多个独立的Oracle数据库实例。每个实例都有自己的系统进程和内存结构,相互之间独立运行。 多实例的优点之一是提供了资源隔离。不同的实例可以使用不同的硬件资源和操作系统进程,避免了单一实例过载导致的性能下降或系统崩溃的风险。每个实例独立运行,一个实例的问题不会影响其他实例的正常运行。 另一个优势是提高了数据库的可用性。一旦一个实例发生故障或需要维护,其他实例仍然可以继续提供服务,确保了系统的连续运行。同时,多实例也提供了负载均衡的功能,可以根据负载情况自动分配请求到不同的实例上,有效提高了数据库的性能和可扩展性。 此外,多实例还可以实现业务数据的隔离。每个实例可以独立管理不同的数据库,适用于多个业务系统或租户共享一个物理服务器的场景。通过配置实例参数和资源分配,可以灵活地进行数据库资源管理和权限控制。 然而,多实例也存在一些挑战和限制。首先,每个实例都需要占用一定的系统资源,包括内存、CPU和磁盘空间。因此,在资源有限的情况下,多实例可能会增加系统负担。其次,多实例的配置和管理相对复杂,需要合理规划和分配各个实例的资源。此外,多实例的通信和同步也需要一定的网络带宽和延迟,可能影响系统的响应速度。 综上所述,Oracle多实例是一种灵活高效的数据库架构,可以提供资源隔离、可用性和负载均衡的功能,适用于高可用性、高并发和多租户的场景。然而,在实施多实例架构时需要根据具体情况进行仔细设计和管理,避免资源浪费和性能问题的发生。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值