Skip to content

Commit 4edd2ec

Browse files
committed
feat(flow):1.11.11, 使用SerialAssembleAction对所有组装对象进行改进
1 parent 231a34d commit 4edd2ec

32 files changed

+769
-539
lines changed

modules/flow/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ set_target_properties(
110110

111111
if(${TBOX_ENABLE_TEST})
112112
add_executable(${TBOX_LIBRARY_NAME}_test ${TBOX_FLOW_TEST_SOURCES})
113-
target_link_libraries(${TBOX_LIBRARY_NAME}_test gmock_main gmock gtest pthread ${TBOX_LIBRARY_NAME} tbox_base tbox_event tbox_util rt dl)
113+
target_link_libraries(${TBOX_LIBRARY_NAME}_test gmock_main gmock gtest pthread ${TBOX_LIBRARY_NAME} tbox_eventx tbox_event tbox_util tbox_base rt dl)
114114
add_test(NAME ${TBOX_LIBRARY_NAME}_test COMMAND ${TBOX_LIBRARY_NAME}_test)
115115
endif()
116116

modules/flow/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ TEST_CPP_SRC_FILES = \
9797
actions/switch_action_test.cpp \
9898
to_graphviz_test.cpp \
9999

100-
TEST_LDFLAGS := $(LDFLAGS) -ltbox_flow -ltbox_event -ltbox_util -ltbox_base -ldl
100+
TEST_LDFLAGS := $(LDFLAGS) -ltbox_flow -ltbox_eventx -ltbox_event -ltbox_util -ltbox_base -ldl
101101
ENABLE_SHARED_LIB = no
102102

103103
include $(TOP_DIR)/mk/lib_tbox_common.mk

modules/flow/actions/assemble_action.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,93 @@ void AssembleAction::onFinal()
5454
final_cb_();
5555
}
5656

57+
bool SerialAssembleAction::startThisAction(Action *action)
58+
{
59+
if (action->start()) {
60+
curr_action_ = action;
61+
return true;
62+
}
63+
64+
return false;
65+
}
66+
67+
void SerialAssembleAction::stopCurrAction()
68+
{
69+
if (curr_action_ != nullptr) {
70+
curr_action_->stop();
71+
curr_action_ = nullptr;
72+
}
73+
}
74+
75+
bool SerialAssembleAction::handleChildFinishEvent(ChildFinishFunc &&child_finish_func)
76+
{
77+
curr_action_ = nullptr;
78+
79+
//! 如果处于运行状态,则由派生类自行处理
80+
if (state() == State::kRunning)
81+
return false;
82+
83+
//! 如果处于暂停状态,则暂存结果
84+
if (state() == State::kPause)
85+
child_finish_func_ = std::move(child_finish_func);
86+
87+
//! 其它状态,如已结束或停止,则不处理
88+
return true;
89+
}
90+
91+
void SerialAssembleAction::onLastChildFinished(bool is_succ, const Reason &reason, const Trace &trace)
92+
{
93+
curr_action_ = nullptr;
94+
95+
//! 如果处于运行状态,则正常退出
96+
if (state() == State::kRunning) {
97+
finish(is_succ, reason, trace);
98+
return;
99+
}
100+
101+
//! 如果处于暂停状态,则暂存结果
102+
if (state() == State::kPause)
103+
child_finish_func_ = [this, is_succ, reason, trace] { finish(is_succ, reason, trace); };
104+
105+
//! 其它状态,如已结束或停止,则不处理
106+
}
107+
108+
void SerialAssembleAction::onPause()
109+
{
110+
if (curr_action_ != nullptr)
111+
curr_action_->pause();
112+
113+
AssembleAction::onPause();
114+
}
115+
116+
void SerialAssembleAction::onResume()
117+
{
118+
AssembleAction::onResume();
119+
120+
if (curr_action_ != nullptr) {
121+
curr_action_->resume();
122+
123+
} else if (child_finish_func_) {
124+
loop_.runNext(std::move(child_finish_func_));
125+
126+
} else {
127+
LogWarn("%d:%s[%s] can't resume", id(), type().c_str(), label().c_str());
128+
}
129+
}
130+
131+
void SerialAssembleAction::onStop()
132+
{
133+
if (curr_action_ != nullptr)
134+
curr_action_->stop();
135+
136+
AssembleAction::onStop();
137+
}
138+
139+
void SerialAssembleAction::onReset()
140+
{
141+
curr_action_ = nullptr;
142+
child_finish_func_ = nullptr;
143+
}
144+
57145
}
58146
}

modules/flow/actions/assemble_action.h

+27
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,33 @@ class AssembleAction : public Action {
4848
FinalCallback final_cb_;
4949
};
5050

51+
//! 串行执行的组装动作
52+
class SerialAssembleAction : public AssembleAction {
53+
public:
54+
using AssembleAction::AssembleAction;
55+
56+
protected:
57+
virtual void onPause() override;
58+
virtual void onResume() override;
59+
virtual void onStop() override;
60+
virtual void onReset() override;
61+
62+
protected:
63+
using ChildFinishFunc = std::function<void()>;
64+
65+
bool startThisAction(Action *action);
66+
void stopCurrAction();
67+
68+
//! 子动作结束事件处理
69+
bool handleChildFinishEvent(ChildFinishFunc &&child_finish_func);
70+
//! 最后一个动作结束的缺省处理
71+
void onLastChildFinished(bool is_succ, const Reason &reason, const Trace &trace);
72+
73+
private:
74+
Action *curr_action_ = nullptr; //! 当前正在执行的动作
75+
ChildFinishFunc child_finish_func_; //! 上一个动用缓存的finish事件
76+
};
77+
5178
}
5279
}
5380

modules/flow/actions/composite_action.cpp

+17-46
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,28 @@ CompositeAction::~CompositeAction() {
3232
}
3333

3434
bool CompositeAction::setChild(Action *child) {
35-
if (child == nullptr)
35+
if (child == nullptr) {
36+
LogWarn("%d:%s[%s], add child %d:%s[%s] fail, child == nullptr",
37+
id(), type().c_str(), label().c_str());
38+
return false;
39+
}
40+
41+
if (!child->setParent(this))
3642
return false;
3743

44+
child->setFinishCallback(std::bind(&CompositeAction::onLastChildFinished, this, _1, _2, _3));
45+
child->setBlockCallback(std::bind(&CompositeAction::block, this, _1, _2));
46+
3847
CHECK_DELETE_RESET_OBJ(child_);
3948
child_ = child;
40-
child_->setFinishCallback(std::bind(&CompositeAction::onChildFinished, this, _1, _2, _3));
41-
child_->setBlockCallback(std::bind(&CompositeAction::onChildBlocked, this, _1, _2));
42-
child_->setParent(this);
4349

4450
return true;
4551
}
4652

4753
void CompositeAction::toJson(Json &js) const {
54+
SerialAssembleAction::toJson(js);
55+
4856
TBOX_ASSERT(child_ != nullptr);
49-
AssembleAction::toJson(js);
5057
child_->toJson(js["child"]);
5158
}
5259

@@ -59,60 +66,24 @@ bool CompositeAction::isReady() const {
5966
}
6067

6168
void CompositeAction::onStart() {
62-
AssembleAction::onStart();
63-
64-
TBOX_ASSERT(child_ != nullptr);
65-
child_->start();
66-
}
69+
SerialAssembleAction::onStart();
6770

68-
void CompositeAction::onStop() {
6971
TBOX_ASSERT(child_ != nullptr);
70-
child_->stop();
71-
72-
AssembleAction::onStop();
73-
}
74-
75-
void CompositeAction::onPause() {
76-
TBOX_ASSERT(child_ != nullptr);
77-
child_->pause();
78-
79-
AssembleAction::onPause();
80-
}
81-
82-
void CompositeAction::onResume() {
83-
AssembleAction::onResume();
84-
85-
TBOX_ASSERT(child_ != nullptr);
86-
if (child_->state() == State::kFinished) {
87-
finish(child_->result() == Result::kSuccess);
88-
} else {
89-
child_->resume();
90-
}
72+
startThisAction(child_);
9173
}
9274

9375
void CompositeAction::onReset() {
9476
TBOX_ASSERT(child_ != nullptr);
9577
child_->reset();
9678

97-
AssembleAction::onReset();
79+
SerialAssembleAction::onReset();
9880
}
9981

10082
void CompositeAction::onFinished(bool is_succ, const Reason &why, const Trace &trace) {
10183
//! 有可能不是child_自然结束产生的finish
102-
TBOX_ASSERT(child_ != nullptr);
103-
child_->stop();
104-
105-
AssembleAction::onFinished(is_succ, why, trace);
106-
}
107-
108-
void CompositeAction::onChildFinished(bool is_succ, const Reason &why, const Trace &trace) {
109-
if (state() == State::kRunning)
110-
finish(is_succ, why, trace);
111-
}
84+
stopCurrAction();
11285

113-
void CompositeAction::onChildBlocked(const Reason &why, const Trace &trace) {
114-
if (state() == State::kRunning)
115-
block(why, trace);
86+
SerialAssembleAction::onFinished(is_succ, why, trace);
11687
}
11788

11889
}

modules/flow/actions/composite_action.h

+2-8
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ namespace flow {
4646
*
4747
* 不用重写`Action`中其它虚函数。
4848
*/
49-
class CompositeAction : public AssembleAction {
49+
class CompositeAction : public SerialAssembleAction {
5050
public:
51-
using AssembleAction::AssembleAction;
51+
using SerialAssembleAction::SerialAssembleAction;
5252
virtual ~CompositeAction();
5353

5454
public:
@@ -59,15 +59,9 @@ class CompositeAction : public AssembleAction {
5959

6060
protected:
6161
virtual void onStart() override;
62-
virtual void onStop() override;
63-
virtual void onPause() override;
64-
virtual void onResume() override;
6562
virtual void onReset() override;
6663
virtual void onFinished(bool is_succ, const Reason &why, const Trace &trace) override;
6764

68-
void onChildFinished(bool is_succ, const Reason &why, const Trace &trace);
69-
void onChildBlocked(const Reason &why, const Trace &trace);
70-
7165
private:
7266
Action *child_ = nullptr;
7367
};

0 commit comments

Comments
 (0)