开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
开源项目 > 程序开发 > 常用工具包 &&
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
103 Star 575 Fork 242

cpp-master/cpp-tbox

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
develop
分支 (18)
标签 (32)
master
develop
opt/websocket
feat/http-client
feature/tls-crypto-https
fix/jsonrpc
feat/mqtt-reconnect
tidy/doc
wlabs-develop
fix/terminal-mem-leak
feat/use-config-enable-epoll
feature-Broker
opt-loop-wdog
feature-DBus
feature-epoll
feature-update-readme
feature-Dns
feature-TcpSSL
v1.15.5
v1.15.4
v1.14.4
v1.13.10
v1.13.2
v1.12.28
v1.12.25
v1.12.22
v1.12.20
v1.12.10
v1.12.0
v1.11.14
v1.11.12
v1.11.11
v1.11.5
v1.11.4
v1.11.2
v1.11.0
v1.10.27
v1.10.23
develop
分支 (18)
标签 (32)
master
develop
opt/websocket
feat/http-client
feature/tls-crypto-https
fix/jsonrpc
feat/mqtt-reconnect
tidy/doc
wlabs-develop
fix/terminal-mem-leak
feat/use-config-enable-epoll
feature-Broker
opt-loop-wdog
feature-DBus
feature-epoll
feature-update-readme
feature-Dns
feature-TcpSSL
v1.15.5
v1.15.4
v1.14.4
v1.13.10
v1.13.2
v1.12.28
v1.12.25
v1.12.22
v1.12.20
v1.12.10
v1.12.0
v1.11.14
v1.11.12
v1.11.11
v1.11.5
v1.11.4
v1.11.2
v1.11.0
v1.10.27
v1.10.23
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
develop
分支 (18)
标签 (32)
master
develop
opt/websocket
feat/http-client
feature/tls-crypto-https
fix/jsonrpc
feat/mqtt-reconnect
tidy/doc
wlabs-develop
fix/terminal-mem-leak
feat/use-config-enable-epoll
feature-Broker
opt-loop-wdog
feature-DBus
feature-epoll
feature-update-readme
feature-Dns
feature-TcpSSL
v1.15.5
v1.15.4
v1.14.4
v1.13.10
v1.13.2
v1.12.28
v1.12.25
v1.12.22
v1.12.20
v1.12.10
v1.12.0
v1.11.14
v1.11.12
v1.11.11
v1.11.5
v1.11.4
v1.11.2
v1.11.0
v1.10.27
v1.10.23
cpp-tbox
/
modules
/
flow
/
state_machine.cpp
cpp-tbox
/
modules
/
flow
/
state_machine.cpp
state_machine.cpp 14.17 KB
一键复制 编辑 原始数据 按行查看 历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2023 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#include "state_machine.h"
#include <vector>
#include <map>
#include <stdexcept>
#include <algorithm>
#include <tbox/base/log.h>
#include <tbox/base/assert.h>
#include <tbox/base/json.hpp>
#include <tbox/base/backtrace.h>
namespace tbox {
namespace flow {
using namespace std;
#define NULL_STATE_ID -1 //! 空状态
#define TERM_STATE_ID 0 //! 终止状态
#define ANY_EVENT_ID 0 //! 任务事件
class StateMachine::Impl {
public:
~Impl();
public:
bool newState(StateID state_id,
const ActionFunc &enter_action,
const ActionFunc &exit_action,
const std::string &lable);
bool addRoute(StateID from_state_id,
EventID event_id,
StateID to_state_id,
const GuardFunc &guard,
const ActionFunc &action,
const std::string &label);
bool addEvent(StateID state_id,
EventID event_id,
const EventFunc &action);
void setInitState(StateID init_state_id) { init_state_id_ = init_state_id; }
bool setSubStateMachine(StateID state_id, StateMachine *wp_sub_sm);
void setStateChangedCallback(StateChangedCallback &&cb) {
state_changed_cb_ = std::move(cb);
}
bool start();
void stop();
bool run(Event event);
StateID currentState() const;
StateID lastState() const;
StateID nextState() const;
bool isRunning() const { return is_running_; }
bool isTerminated() const {
return (curr_state_ != nullptr) && (curr_state_->id == TERM_STATE_ID);
}
void toJson(Json &js) const;
void setName(const std::string &name) { name_ = name; }
private:
struct Route {
EventID event_id;
StateID next_state_id;
GuardFunc guard;
ActionFunc action;
std::string label;
};
struct State {
StateID id;
ActionFunc enter_action;
ActionFunc exit_action;
std::string label;
StateMachine::Impl *sub_sm; //! 子状态机
vector<Route> routes; //! 转换路由
map<EventID, EventFunc> events; //! 内部事件
EventFunc default_event;
};
State* findState(StateID state_id) const;
StateID init_state_id_ = NULL_STATE_ID; //! 初始状态
bool is_running_ = false; //! 是否正在运行
State *last_state_ = nullptr; //! 上一个状态指针
State *curr_state_ = nullptr; //! 当前状态指针
State *next_state_ = nullptr; //! 下一个状态指针
map<StateID, State*> states_; //! 状态对象表
StateChangedCallback state_changed_cb_;
static State _term_state_; //! 默认终止状态对象
int cb_level_ = 0;
std::string name_;
};
StateMachine::StateMachine() : impl_(new Impl)
{ }
StateMachine::~StateMachine()
{
delete impl_;
}
bool StateMachine::newState(StateID state_id, const ActionFunc &enter_action, const ActionFunc &exit_action, const std::string &label)
{
return impl_->newState(state_id, enter_action, exit_action, label);
}
bool StateMachine::addRoute(StateID from_state_id, EventID event_id, StateID to_state_id,
const GuardFunc &guard, const ActionFunc &action, const std::string &label)
{
return impl_->addRoute(from_state_id, event_id, to_state_id, guard, action, label);
}
bool StateMachine::addEvent(StateID state_id, EventID event_id, const EventFunc &action)
{
return impl_->addEvent(state_id, event_id, action);
}
void StateMachine::setInitState(StateID init_state_id)
{
impl_->setInitState(init_state_id);
}
bool StateMachine::setSubStateMachine(StateID state_id, StateMachine *wp_sub_sm)
{
return impl_->setSubStateMachine(state_id, wp_sub_sm);
}
void StateMachine::setStateChangedCallback(StateChangedCallback &&cb)
{
impl_->setStateChangedCallback(std::move(cb));
}
bool StateMachine::start()
{
return impl_->start();
}
void StateMachine::stop()
{
impl_->stop();
}
bool StateMachine::restart()
{
impl_->stop();
return impl_->start();
}
bool StateMachine::run(Event event)
{
return impl_->run(event);
}
StateMachine::StateID StateMachine::currentState() const
{
return impl_->currentState();
}
StateMachine::StateID StateMachine::lastState() const
{
return impl_->lastState();
}
StateMachine::StateID StateMachine::nextState() const
{
return impl_->nextState();
}
bool StateMachine::isRunning() const
{
return impl_->isRunning();
}
bool StateMachine::isTerminated() const
{
return impl_->isTerminated();
}
void StateMachine::toJson(Json &js) const
{
return impl_->toJson(js);
}
void StateMachine::setName(const std::string &name)
{
impl_->setName(name);
}
///////////////////////
StateMachine::Impl::State StateMachine::Impl::_term_state_ = { TERM_STATE_ID, nullptr, nullptr, "Term", nullptr, { } };
StateMachine::Impl::~Impl()
{
TBOX_ASSERT(cb_level_ == 0);
for (auto &item : states_)
delete item.second;
states_.clear();
}
bool StateMachine::Impl::newState(StateID state_id,
const ActionFunc &enter_action,
const ActionFunc &exit_action,
const std::string &label)
{
if (is_running_) {
LogWarn("[%s]: it's already started", name_.c_str());
return false;
}
//! 已存在的状态不能再创建了
if (states_.find(state_id) != states_.end()) {
LogWarn("[%s]: state %d exist", name_.c_str(), state_id);
return false;
}
auto new_state = new State { state_id, enter_action, exit_action, label, nullptr, { } };
states_[state_id] = new_state;
if (init_state_id_ == NULL_STATE_ID)
init_state_id_ = state_id;
return true;
}
bool StateMachine::Impl::addRoute(StateID from_state_id,
EventID event_id,
StateID to_state_id,
const GuardFunc &guard,
const ActionFunc &action,
const std::string &label)
{
if (is_running_) {
LogWarn("[%s]: it's already started", name_.c_str());
return false;
}
//! 要求 from_state_id 必须是已存在的状态
auto from_state = findState(from_state_id);
if (from_state == nullptr) {
LogWarn("[%s]: from_state %d not exist", name_.c_str(), from_state_id);
return false;
}
//! 如果 to_state_id 为是终止状态,那么这个状态必须是已存在的
if (to_state_id != TERM_STATE_ID && findState(to_state_id) == nullptr) {
LogWarn("[%s]: to_state %d not exist", name_.c_str(), to_state_id);
return false;
}
from_state->routes.emplace_back(Route{ event_id, to_state_id, guard, action, label });
return true;
}
bool StateMachine::Impl::addEvent(StateID state_id, EventID event_id, const EventFunc &action)
{
if (is_running_) {
LogWarn("[%s]: it's already started", name_.c_str());
return false;
}
if (action == nullptr) {
LogWarn("[%s]: state %d, event:%d, action is nullptr", name_.c_str(), state_id, event_id);
return false;
}
//! 要求 state_id 必须是已存在的状态
auto state = findState(state_id);
if (state == nullptr) {
LogWarn("[%s]: state %d not exist", name_.c_str(), state_id);
return false;
}
if (event_id != ANY_EVENT_ID)
state->events[event_id] = action;
else
state->default_event = action;
return true;
}
bool StateMachine::Impl::setSubStateMachine(StateID state_id, StateMachine *wp_sub_sm)
{
if (is_running_) {
LogWarn("[%s]: it's already started", name_.c_str());
return false;
}
auto state = findState(state_id);
if (state == nullptr) {
LogWarn("[%s]: state:%d not exist", name_.c_str(), state_id);
return false;
}
state->sub_sm = wp_sub_sm->impl_;
return true;
}
bool StateMachine::Impl::start()
{
if (is_running_) {
LogWarn("[%s]: it's already started", name_.c_str());
return false;
}
if (cb_level_ != 0) {
LogWarn("[%s]: recursion invoke, call stack:\n%s", name_.c_str(), DumpBacktrace().c_str());
return false;
}
auto init_state = findState(init_state_id_);
if (init_state == nullptr) {
LogWarn("[%s]: init state %d not exist", name_.c_str(), init_state_id_);
return false;
}
is_running_ = true;
curr_state_ = init_state;
++cb_level_;
if (init_state->enter_action)
init_state->enter_action(Event());
--cb_level_;
//! 如果有子状态机,在启动子状态机
if (curr_state_->sub_sm != nullptr)
curr_state_->sub_sm->start();
return true;
}
void StateMachine::Impl::stop()
{
if (!is_running_)
return;
if (cb_level_ != 0) {
LogWarn("[%s]: recursion invoke, call stack:\n%s", name_.c_str(), DumpBacktrace().c_str());
return;
}
++cb_level_;
if (curr_state_->exit_action)
curr_state_->exit_action(Event());
--cb_level_;
curr_state_ = nullptr;
is_running_ = false;
}
bool StateMachine::Impl::run(Event event)
{
if (!is_running_) {
LogWarn("[%s]: need start first", name_.c_str());
return false;
}
if (cb_level_ != 0) {
LogWarn("[%s]: recursion invoke, call stack:\n%s", name_.c_str(), DumpBacktrace().c_str());
return false;
}
//! 如果有子状态机,则给子状态机处理
if (curr_state_->sub_sm != nullptr) {
bool ret = curr_state_->sub_sm->run(event);
if (!curr_state_->sub_sm->isTerminated())
return ret;
curr_state_->sub_sm->stop();
}
StateID next_state_id = NULL_STATE_ID;
ActionFunc route_action;
//! 检查事件,并执行
++cb_level_;
auto event_iter = curr_state_->events.find(event.id);
if (event_iter != curr_state_->events.end())
next_state_id = event_iter->second(event);
else if (curr_state_->default_event)
next_state_id = curr_state_->default_event(event);
--cb_level_;
if (next_state_id == NULL_STATE_ID) {
//! 找出可行的路径
++cb_level_;
auto route_iter = std::find_if(curr_state_->routes.begin(), curr_state_->routes.end(),
[event] (const Route &item) -> bool {
if (item.event_id != ANY_EVENT_ID && item.event_id != event.id)
return false;
if (item.guard != nullptr && !item.guard(event))
return false;
return true;
}
);
--cb_level_;
//! 如果没有跳转则直接退出
if (route_iter == curr_state_->routes.end())
return false;
//! 以下是有跳转的情况
next_state_id = route_iter->next_state_id;
route_action = route_iter->action;
}
next_state_ = findState(next_state_id);
if (next_state_ == nullptr) {
if (next_state_id == TERM_STATE_ID) {
next_state_ = &_term_state_;
} else {
LogErr("[%s]: Should not happen", name_.c_str());
return false;
}
}
++cb_level_;
if (curr_state_->exit_action)
curr_state_->exit_action(event);
last_state_ = curr_state_;
curr_state_ = nullptr;
if (route_action)
route_action(event);
curr_state_ = next_state_;
next_state_ = nullptr;
if (curr_state_->enter_action)
curr_state_->enter_action(event);
if (state_changed_cb_)
state_changed_cb_(last_state_->id, curr_state_->id, event);
//! 如果新的状态有子状态机,则启动子状态机并将事件交给子状态机处理
if (curr_state_->sub_sm != nullptr) {
curr_state_->sub_sm->start();
curr_state_->sub_sm->run(event);
}
--cb_level_;
return true;
}
StateMachine::StateID StateMachine::Impl::currentState() const
{
if (curr_state_ != nullptr)
return curr_state_->id;
return NULL_STATE_ID;
}
StateMachine::StateID StateMachine::Impl::lastState() const
{
if (last_state_ != nullptr)
return last_state_->id;
return NULL_STATE_ID;
}
StateMachine::StateID StateMachine::Impl::nextState() const
{
if (next_state_ != nullptr)
return next_state_->id;
return NULL_STATE_ID;
}
StateMachine::Impl::State* StateMachine::Impl::findState(StateID state_id) const
{
if (state_id != NULL_STATE_ID) {
auto iter = states_.find(state_id);
if (iter != states_.end())
return iter->second;
}
return nullptr;
}
void StateMachine::Impl::toJson(Json &js) const
{
js["name"] = name_;
js["is_running"] = is_running_;
js["init_state"] = init_state_id_;
js["term_state"] = TERM_STATE_ID;
if (curr_state_ != nullptr)
js["curr_state"] = curr_state_->id;
auto &js_state_array = js["states"];
for (auto &item : states_) {
auto state = item.second;
Json js_state;
js_state["id"] = state->id;
js_state["label"] = state->label;
if (state->sub_sm != nullptr)
state->sub_sm->toJson(js_state["sub_sm"]);
auto &js_route_array = js_state["routes"];
for (auto &route : state->routes) {
Json js_route;
js_route["event_id"] = route.event_id;
js_route["next_state_id"] = route.next_state_id;
js_route["label"] = route.label;
js_route_array.push_back(std::move(js_route));
}
auto &js_event_array = js_state["events"];
for (auto &item : state->events)
js_event_array.push_back(item.first);
js_state_array.push_back(std::move(js_state));
}
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

cpp-tbox是一个Linux C++,MIT开源许可的,基于 Reactor 模式的开发框架与组件库。它主要针对智能硬件、机器人、网络服务等开发领域。 它提供了一套易于开发、稳定可靠的框架,有:通信库(TCP/UDP/串口)、HTTP、线程池、定时器池、协程、日志、命令终端、状态机、行为树等非常实用的开发组件,以及完备实用的主框架,能为广大开发者节省大量开发与调试时间。
取消

发行版

暂无发行版

开源评估指数源自 OSS-Compass 评估体系,评估体系围绕以下三个维度对项目展开评估:

1. 开源生态

  • 生产力:来评估开源项目输出软件制品和开源价值的能力。
  • 创新力:用于评估开源软件及其生态系统的多样化程度。
  • 稳健性:用于评估开源项目面对多变的发展环境,抵御内外干扰并自我恢复的能力。

2. 协作、人、软件

  • 协作:代表了开源开发行为中协作的程度和深度。
  • 人:观察开源项目核心人员在开源项目中的影响力,并通过第三方视角考察用户和开发者对开源项目的评价。
  • 软件:从开源项目对外输出的制品评估其价值最终落脚点。也是开源评估最"古老"的主流方向之一"开源软件" 的具体表现。

3. 评估模型

    基于"开源生态"与"协作、人、软件"的维度,找到与该目标直接或间接相关的可量化指标,对开源项目健康与生态进行量化评估,最终形成开源评估指数。

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cpp-master/cpp-tbox.git
git@gitee.com:cpp-master/cpp-tbox.git
cpp-master
cpp-tbox
cpp-tbox
develop
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /