Skip to content

Commit 4abc2e1

Browse files
committed
feat(flow):1.8.16, 修改StateMachine的默认状态指定方式,改成首个newState()的状态
1 parent 07071e7 commit 4abc2e1

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

modules/flow/state_machine.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class StateMachine::Impl {
107107

108108
State* findState(StateID state_id) const;
109109

110-
StateID init_state_id_ = 1; //! 初始状态
110+
StateID init_state_id_ = NULL_STATE_ID; //! 初始状态
111111

112112
bool is_running_ = false; //! 是否正在运行
113113

@@ -253,6 +253,9 @@ bool StateMachine::Impl::newState(StateID state_id,
253253
auto new_state = new State { state_id, enter_action, exit_action, label, nullptr, { } };
254254
states_[state_id] = new_state;
255255

256+
if (init_state_id_ == NULL_STATE_ID)
257+
init_state_id_ = state_id;
258+
256259
return true;
257260
}
258261

@@ -498,9 +501,11 @@ StateMachine::StateID StateMachine::Impl::nextState() const
498501

499502
StateMachine::Impl::State* StateMachine::Impl::findState(StateID state_id) const
500503
{
501-
auto iter = states_.find(state_id);
502-
if (iter != states_.end())
503-
return iter->second;
504+
if (state_id != NULL_STATE_ID) {
505+
auto iter = states_.find(state_id);
506+
if (iter != states_.end())
507+
return iter->second;
508+
}
504509
return nullptr;
505510
}
506511

modules/flow/state_machine.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ namespace flow {
3232
//! HFSM,多层级有限状态机
3333
class StateMachine {
3434
public:
35-
using StateID = int; //! StateID 为 0 与 1 的两个状态为特定状态
36-
//! StateID = 0 的状态为终止状态,用户可以不用定义
37-
//! StateID = 1 的状态为默认的初始状态。也可以通过 setInitState() 重新指定
38-
//! StateID < 0 表示无效状态
35+
using StateID = int; //! StateID = 0 的状态为终止状态,用户可以不用定义
36+
//! StateID = -1 的表示无效状态
37+
3938
using EventID = int; //! EventID = 0 表示任意事件,仅在 addRoute(), addEvent() 时使用
4039

4140
//! 动作执行函数
@@ -55,11 +54,13 @@ class StateMachine {
5554
/**
5655
* \brief 创建一个状态
5756
*
58-
* \param state_id 状态ID号,StateID = 1 的状态默认为初始状态
57+
* \param state_id 状态ID号
5958
* \param enter_action 进入该状态时的动作,nullptr表示无动作
6059
* \param exit_action 退出该状态时的动作,nullptr表示无动作
6160
*
6261
* \return bool 成功与否,重复创建会失败
62+
*
63+
* \note newState() 的第一个状态,默认为起始状态
6364
*/
6465
bool newState(StateID state_id,
6566
const ActionFunc &enter_action,
@@ -132,6 +133,8 @@ class StateMachine {
132133
* \brief 设置起始与终止状态
133134
*
134135
* \param init_state_id 起始状态
136+
*
137+
* \note 如果不指定,那么第一个newState()的状态为起始状态
135138
*/
136139
void setInitState(StateID init_state_id);
137140

version.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
2323
TBOX_VERSION_MINOR := 8
24-
TBOX_VERSION_REVISION := 15
24+
TBOX_VERSION_REVISION := 16

0 commit comments

Comments
 (0)