开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 242

xiongying/cpp-tbox

forked from cpp-master/cpp-tbox
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (12)
标签 (3)
master
develop
fix-ActionFinishIssue
feature-DBus
feature-epoll
feature-update-readme
feature-cmake
feature-Dns
refactory-Flow
feature-Broker
feature-autotools
feature-TcpSSL
2.2
v2.1
v2.0
master
分支 (12)
标签 (3)
master
develop
fix-ActionFinishIssue
feature-DBus
feature-epoll
feature-update-readme
feature-cmake
feature-Dns
refactory-Flow
feature-Broker
feature-autotools
feature-TcpSSL
2.2
v2.1
v2.0
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (12)
标签 (3)
master
develop
fix-ActionFinishIssue
feature-DBus
feature-epoll
feature-update-readme
feature-cmake
feature-Dns
refactory-Flow
feature-Broker
feature-autotools
feature-TcpSSL
2.2
v2.1
v2.0
cpp-tbox
/
modules
/
flow
/
state_machine.cpp
cpp-tbox
/
modules
/
flow
/
state_machine.cpp
state_machine.cpp 12.68 KB
一键复制 编辑 原始数据 按行查看 历史
海卫哥 提交于 2023年08月07日 23:54 +08:00 . feat(flow,dbus):1.5.8
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
/*
* .============.
* // 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>
namespace tbox {
namespace flow {
using namespace std;
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(const StateChangedCallback &cb);
bool start();
void stop();
bool run(Event event);
StateID currentState() const;
StateID lastState() const;
StateID nextState() const;
bool isTerminated() const;
void toJson(Json &js) const;
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_ = 1; //! 初始状态
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;
};
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(const StateChangedCallback &cb)
{
impl_->setStateChangedCallback(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::isTerminated() const
{
return impl_->isTerminated();
}
void StateMachine::toJson(Json &js) const
{
return impl_->toJson(js);
}
///////////////////////
StateMachine::Impl::State StateMachine::Impl::_term_state_ = { 0, 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 (states_.find(state_id) != states_.end()) {
LogWarn("state %u exist", state_id);
return false;
}
auto new_state = new State { state_id, enter_action, exit_action, label, nullptr, { } };
states_[state_id] = new_state;
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)
{
//! 要求 from_state_id 必须是已存在的状态
auto from_state = findState(from_state_id);
if (from_state == nullptr) {
LogWarn("from_state %d not exist", from_state_id);
return false;
}
//! 如果 to_state_id 为是终止状态,那么这个状态必须是已存在的
if (to_state_id != 0 && findState(to_state_id) == nullptr) {
LogWarn("to_state %d not exist", 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 (action == nullptr) {
LogWarn("action is nullptr", state_id);
return false;
}
//! 要求 state_id 必须是已存在的状态
auto state = findState(state_id);
if (state == nullptr) {
LogWarn("state %d not exist", state_id);
return false;
}
if (event_id != 0)
state->events[event_id] = action;
else
state->default_event = action;
return true;
}
bool StateMachine::Impl::setSubStateMachine(StateID state_id, StateMachine *wp_sub_sm)
{
auto state = findState(state_id);
if (state == nullptr) {
LogWarn("state:%u not exist", state_id);
return false;
}
state->sub_sm = wp_sub_sm->impl_;
return true;
}
void StateMachine::Impl::setStateChangedCallback(const StateChangedCallback &cb)
{
state_changed_cb_ = cb;
}
bool StateMachine::Impl::start()
{
if (curr_state_ != nullptr) {
LogWarn("it's already started");
return false;
}
if (cb_level_ != 0) {
LogWarn("recursion invoke");
return false;
}
auto init_state = findState(init_state_id_);
if (init_state == nullptr) {
LogWarn("init state %u not exist", init_state_id_);
return false;
}
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 (curr_state_ == nullptr)
return;
if (cb_level_ != 0) {
LogWarn("recursion invoke");
return;
}
++cb_level_;
if (curr_state_->exit_action)
curr_state_->exit_action(Event());
--cb_level_;
curr_state_ = nullptr;
}
bool StateMachine::Impl::run(Event event)
{
if (curr_state_ == nullptr) {
LogWarn("need start first");
return false;
}
if (cb_level_ != 0) {
LogWarn("recursion invoke");
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 = -1;
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 < 0) {
//! 找出可行的路径
++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 != 0 && 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 == 0) {
next_state_ = &_term_state_;
} else {
LogErr("Should not happen");
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 -1;
}
StateMachine::StateID StateMachine::Impl::lastState() const
{
if (last_state_ != nullptr)
return last_state_->id;
return -1;
}
StateMachine::StateID StateMachine::Impl::nextState() const
{
if (next_state_ != nullptr)
return next_state_->id;
return -1;
}
bool StateMachine::Impl::isTerminated() const
{
if (curr_state_ == nullptr) {
LogWarn("need start first");
return false;
}
return curr_state_->id == 0;
}
StateMachine::Impl::State* StateMachine::Impl::findState(StateID state_id) const
{
try {
return states_.at(state_id);
} catch (const out_of_range &e) {
return nullptr;
}
}
void StateMachine::Impl::toJson(Json &js) const
{
js["init_state"] = init_state_id_;
js["term_state"] = 0;
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个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

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

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

取消
提交

简介

C++的百宝箱,是一个完备的Linux应用l软件开发工具库与运行框架。 它有通信库(TCP/UDP/串口)、HTTP、线程池、定时器池、协程、日志、命令终端、状态机、行为树等非常实用的开发组件,它还有完备实用的启动框架。 它可以让应用开发者从实现细节中解放出来,专注于功能逻辑。
暂无标签
MIT
使用 MIT 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

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

搜索帮助

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

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