Skip to content

Commit

Permalink
opt(flow):1.7.14, 优化Action,添加对子类重写的onFinished(),onStart()等虚函数是否调用父类对应…
Browse files Browse the repository at this point in the history
…函数的检查
  • Loading branch information
hevake committed Mar 4, 2024
1 parent 2260272 commit 37f7362
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 19 deletions.
61 changes: 53 additions & 8 deletions modules/flow/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ namespace flow {

int Action::_id_alloc_counter_ = 0;

Action::Action(event::Loop &loop, const std::string &type) :
loop_(loop),
id_(++_id_alloc_counter_),
type_(type)
Action::Action(event::Loop &loop, const std::string &type)
: loop_(loop)
, id_(++_id_alloc_counter_)
, type_(type)
{}

Action::~Action() {
Expand Down Expand Up @@ -87,8 +87,13 @@ bool Action::start() {

LogDbg("start action %d:%s[%s]", id_, type_.c_str(), label_.c_str());

is_base_func_invoked_ = false;

onStart();

if (!is_base_func_invoked_)
LogWarn("%d:%s[%s] didn't invoke base func", id_, type_.c_str(), label_.c_str());

if (last_state == state_) {
if (timer_ev_ != nullptr)
timer_ev_->enable();
Expand All @@ -111,8 +116,13 @@ bool Action::pause() {

LogDbg("pause action %d:%s[%s]", id_, type_.c_str(), label_.c_str());

is_base_func_invoked_ = false;

onPause();

if (!is_base_func_invoked_)
LogWarn("%d:%s[%s] didn't invoke base func", id_, type_.c_str(), label_.c_str());

if (last_state == state_) {
if (timer_ev_ != nullptr)
timer_ev_->disable();
Expand All @@ -135,8 +145,13 @@ bool Action::resume() {

LogDbg("resume action %d:%s[%s]", id_, type_.c_str(), label_.c_str());

is_base_func_invoked_ = false;

onResume();

if (!is_base_func_invoked_)
LogWarn("%d:%s[%s] didn't invoke base func", id_, type_.c_str(), label_.c_str());

if (last_state == state_) {
if (timer_ev_ != nullptr)
timer_ev_->enable();
Expand All @@ -155,8 +170,13 @@ bool Action::stop() {

LogDbg("stop action %d:%s[%s]", id_, type_.c_str(), label_.c_str());

is_base_func_invoked_ = false;

onStop();

if (!is_base_func_invoked_)
LogWarn("%d:%s[%s] didn't invoke base func", id_, type_.c_str(), label_.c_str());

if (last_state == state_) {
if (timer_ev_ != nullptr)
timer_ev_->disable();
Expand All @@ -177,8 +197,13 @@ void Action::reset() {
id_, type_.c_str(), label_.c_str(), ToString(state_).c_str());
}

is_base_func_invoked_ = false;

onReset();

if (!is_base_func_invoked_)
LogWarn("%d:%s[%s] didn't invoke base func", id_, type_.c_str(), label_.c_str());

if (timer_ev_ != nullptr)
timer_ev_->disable();

Expand Down Expand Up @@ -232,12 +257,13 @@ bool Action::finish(bool is_succ) {
if (timer_ev_ != nullptr)
timer_ev_->disable();

result_ = is_succ ? Result::kSuccess : Result::kFail;

if (finish_cb_)
finish_cb_run_id_ = loop_.runNext(std::bind(finish_cb_, is_succ), "Action::finish");
is_base_func_invoked_ = false;

onFinished(is_succ);

if (!is_base_func_invoked_)
LogWarn("%d:%s[%s] didn't invoke base func", id_, type_.c_str(), label_.c_str());

return true;

} else {
Expand All @@ -246,6 +272,25 @@ bool Action::finish(bool is_succ) {
}
}

void Action::onStart() { is_base_func_invoked_ = true; }

void Action::onPause() { is_base_func_invoked_ = true; }

void Action::onResume() { is_base_func_invoked_ = true; }

void Action::onStop() { is_base_func_invoked_ = true; }

void Action::onReset() { is_base_func_invoked_ = true; }

void Action::onFinished(bool is_succ) {
result_ = is_succ ? Result::kSuccess : Result::kFail;

if (finish_cb_)
finish_cb_run_id_ = loop_.runNext(std::bind(finish_cb_, is_succ), "Action::finish");

is_base_func_invoked_ = true;
}

std::string ToString(Action::State state) {
const char *tbl[] = { "idle", "running", "pause", "finished", "stoped" };
auto index = static_cast<size_t>(state);
Expand Down
16 changes: 10 additions & 6 deletions modules/flow/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ class Action {
protected:
bool finish(bool is_succ);

virtual void onStart() { } //! WARN: 启动失败是一种异常,要少用
virtual void onPause() { }
virtual void onResume() { }
virtual void onStop() { }
virtual void onReset() { }
virtual void onFinished(bool is_succ) { (void)is_succ; }
virtual void onStart();
virtual void onPause();
virtual void onResume();
virtual void onStop();
virtual void onReset();
virtual void onFinished(bool is_succ);
virtual void onTimeout() { finish(false); }

protected:
Expand All @@ -122,6 +122,10 @@ class Action {

event::TimerEvent *timer_ev_ = nullptr;
event::Loop::RunId finish_cb_run_id_ = 0; //!< runNext()的任务号,用于撤消

bool is_base_func_invoked_ = false; //! 是否已调用基类函数
//! 检查使用者在重写的 onStart(),onPause(),onResume(),onStop(),onFinished() 中是否调用了基类的函数
//! 如果没有调用,则打警告提示
};

//! 枚举转字串
Expand Down
10 changes: 10 additions & 0 deletions modules/flow/actions/composite_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,29 @@ bool CompositeAction::isReady() const {
}

void CompositeAction::onStart() {
Action::onStart();

TBOX_ASSERT(child_ != nullptr);
child_->start();
}

void CompositeAction::onStop() {
TBOX_ASSERT(child_ != nullptr);
child_->stop();

Action::onStop();
}

void CompositeAction::onPause() {
TBOX_ASSERT(child_ != nullptr);
child_->pause();

Action::onPause();
}

void CompositeAction::onResume() {
Action::onResume();

TBOX_ASSERT(child_ != nullptr);
if (child_->state() == State::kFinished) {
finish(child_->result() == Result::kSuccess);
Expand All @@ -83,6 +91,8 @@ void CompositeAction::onResume() {
void CompositeAction::onReset() {
TBOX_ASSERT(child_ != nullptr);
child_->reset();

Action::onReset();
}

void CompositeAction::onChildFinished(bool is_succ) {
Expand Down
8 changes: 7 additions & 1 deletion modules/flow/actions/event_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,33 @@ EventAction::~EventAction() {
}

void EventAction::onStart() {
Action::onStart();
pub_.subscribe(this);
}

void EventAction::onStop() {
pub_.unsubscribe(this);
Action::onStop();
}

void EventAction::onPause() {
pub_.unsubscribe(this);
Action::onPause();
}

void EventAction::onResume() {
Action::onResume();
pub_.subscribe(this);
}

void EventAction::onReset() {
pub_.unsubscribe(this);
Action::onReset();
}

void EventAction::onFinished(bool) {
void EventAction::onFinished(bool is_succ) {
pub_.unsubscribe(this);
Action::onFinished(is_succ);
}

}
Expand Down
1 change: 1 addition & 0 deletions modules/flow/actions/function_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ FunctionAction::FunctionAction(event::Loop &loop, Func &&func)
}

void FunctionAction::onStart() {
Action::onStart();
finish(func_());
}

Expand Down
10 changes: 10 additions & 0 deletions modules/flow/actions/if_else_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ bool IfElseAction::isReady() const {
}

void IfElseAction::onStart() {
Action::onStart();

TBOX_ASSERT(if_action_ != nullptr);
if_action_->start();
}
Expand All @@ -110,6 +112,8 @@ void IfElseAction::onStop() {
} else {
if_action_->stop();
}

Action::onStop();
}

void IfElseAction::onPause() {
Expand All @@ -123,9 +127,13 @@ void IfElseAction::onPause() {
} else {
if_action_->pause();
}

Action::onPause();
}

void IfElseAction::onResume() {
Action::onResume();

TBOX_ASSERT(if_action_ != nullptr);
if (if_action_->state() == Action::State::kFinished) {
if (if_action_->result() == Action::Result::kSuccess) {
Expand Down Expand Up @@ -155,6 +163,8 @@ void IfElseAction::onReset() {

if (fail_action_ != nullptr)
fail_action_->reset();

Action::onReset();
}

void IfElseAction::onCondActionFinished(bool is_succ) {
Expand Down
10 changes: 10 additions & 0 deletions modules/flow/actions/loop_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,38 @@ bool LoopAction::isReady() const {
}

void LoopAction::onStart() {
Action::onStart();

TBOX_ASSERT(child_ != nullptr);
child_->start();
}

void LoopAction::onStop() {
TBOX_ASSERT(child_ != nullptr);
child_->stop();

Action::onStop();
}

void LoopAction::onPause() {
TBOX_ASSERT(child_ != nullptr);
child_->pause();

Action::onPause();
}

void LoopAction::onResume() {
Action::onResume();

TBOX_ASSERT(child_ != nullptr);
child_->resume();
}

void LoopAction::onReset() {
TBOX_ASSERT(child_ != nullptr);
child_->reset();

Action::onReset();
}

void LoopAction::onChildFinished(bool is_succ) {
Expand Down
10 changes: 10 additions & 0 deletions modules/flow/actions/loop_if_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ bool LoopIfAction::isReady() const {
}

void LoopIfAction::onStart() {
Action::onStart();

TBOX_ASSERT(if_action_ != nullptr);
if_action_->start();
}
Expand All @@ -88,15 +90,21 @@ void LoopIfAction::onStop() {
auto curr_action = if_action_->state() == State::kFinished ? exec_action_ : if_action_;
TBOX_ASSERT(curr_action != nullptr);
curr_action->stop();

Action::onStop();
}

void LoopIfAction::onPause() {
auto curr_action = if_action_->state() == State::kFinished ? exec_action_ : if_action_;
TBOX_ASSERT(curr_action != nullptr);
curr_action->pause();

Action::onPause();
}

void LoopIfAction::onResume() {
Action::onResume();

auto curr_action = if_action_->state() == State::kFinished ? exec_action_ : if_action_;
TBOX_ASSERT(curr_action != nullptr);
curr_action->resume();
Expand All @@ -106,6 +114,8 @@ void LoopIfAction::onReset() {
TBOX_ASSERT(if_action_ != nullptr && exec_action_ != nullptr);
if_action_->reset();
exec_action_->reset();

Action::onReset();
}

void LoopIfAction::onIfFinished(bool is_succ) {
Expand Down
Loading

0 comments on commit 37f7362

Please sign in to comment.