Skip to content

Commit d490e7e

Browse files
committed
Merge branch 'develop'
2 parents 072af9a + 988c58e commit d490e7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2813
-489
lines changed

examples/main/03_nc_client_and_echo_server/main.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void RegisterApps(Module &apps, Context &ctx)
2929
{
3030
apps.add(new echo_server::App(ctx));
3131
apps.add(new nc_client::App(ctx));
32+
apps.addAs(new nc_client::App(ctx), "nc_client_2");
3233
}
3334

3435
std::string GetAppDescribe()
@@ -46,7 +47,7 @@ void GetAppVersion(int &major, int &minor, int &rev, int &build)
4647
major = 1;
4748
minor = 0;
4849
rev = 0;
49-
build = 0;
50+
build = 1;
5051
}
5152

5253
}

modules/base/defines.h

+28-1
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,31 @@
4343
class_name(class_name &&) = delete; \
4444
class_name& operator = (class_name &&) = delete
4545

46+
#define DECLARE_COPY_FUNC(class_name) \
47+
class_name(const class_name& other); \
48+
class_name& operator = (const class_name& other)
49+
50+
#define DECLARE_MOVE_RESET_FUNC(class_name) \
51+
class_name(class_name&& other); \
52+
class_name& operator = (class_name&& other); \
53+
void reset()
54+
55+
//! 基于copy(),实现复构造与赋值函数
56+
#define IMPL_COPY_FUNC(class_name) \
57+
class_name::class_name(const class_name& other) \
58+
{ \
59+
copy(other); \
60+
} \
61+
class_name& class_name::operator = (const class_name& other) \
62+
{ \
63+
if (this != &other) { \
64+
copy(other); \
65+
} \
66+
return *this; \
67+
}
68+
4669
//! 基于无参构造、析构、swap,实现reset与移动函数
47-
#define IMP_MOVE_RESET_FUNC_BASE_ON_SWAP(class_name) \
70+
#define IMPL_MOVE_RESET_FUNC(class_name) \
4871
class_name::class_name(class_name&& other) \
4972
{ \
5073
swap(other); \
@@ -77,4 +100,8 @@
77100
#define UNUSED_VAR(x) (void)(x)
78101
#endif
79102

103+
#ifndef DEPRECATED
104+
#define DEPRECATED __attribute__((deprecated))
105+
#endif
106+
80107
#endif //TBOX_BASE_DEFINES_H_20171030

modules/flow/CMakeLists.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ set(TBOX_FLOW_HEADERS
4444
actions/sequence_action.h
4545
actions/parallel_action.h
4646
actions/if_else_action.h
47+
actions/if_then_action.h
4748
actions/loop_action.h
4849
actions/loop_if_action.h
4950
actions/event_action.h
@@ -52,6 +53,7 @@ set(TBOX_FLOW_HEADERS
5253
actions/wrapper_action.h
5354
actions/succ_fail_action.h
5455
actions/dummy_action.h
56+
actions/switch_action.h
5557
to_graphviz.h)
5658

5759
set(TBOX_FLOW_SOURCES
@@ -65,12 +67,15 @@ set(TBOX_FLOW_SOURCES
6567
actions/sequence_action.cpp
6668
actions/parallel_action.cpp
6769
actions/if_else_action.cpp
70+
actions/if_then_action.cpp
6871
actions/loop_action.cpp
6972
actions/loop_if_action.cpp
7073
actions/event_action.cpp
7174
actions/repeat_action.cpp
7275
actions/composite_action.cpp
7376
actions/wrapper_action.cpp
77+
actions/dummy_action.cpp
78+
actions/switch_action.cpp
7479
to_graphviz.cpp)
7580

7681
set(TBOX_FLOW_TEST_SOURCES
@@ -84,12 +89,14 @@ set(TBOX_FLOW_TEST_SOURCES
8489
actions/sequence_action_test.cpp
8590
actions/parallel_action_test.cpp
8691
actions/if_else_action_test.cpp
92+
actions/if_then_action_test.cpp
8793
actions/loop_action_test.cpp
8894
actions/loop_if_action_test.cpp
8995
actions/repeat_action_test.cpp
9096
actions/composite_action_test.cpp
9197
actions/wrapper_action_test.cpp
9298
actions/succ_fail_action_test.cpp
99+
actions/switch_action_test.cpp
93100
to_graphviz_test.cpp)
94101

95102
add_library(${TBOX_LIBRARY_NAME} ${TBOX_BUILD_LIB_TYPE} ${TBOX_FLOW_SOURCES})
@@ -103,7 +110,7 @@ set_target_properties(
103110

104111
if(${TBOX_ENABLE_TEST})
105112
add_executable(${TBOX_LIBRARY_NAME}_test ${TBOX_FLOW_TEST_SOURCES})
106-
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)
107114
add_test(NAME ${TBOX_LIBRARY_NAME}_test COMMAND ${TBOX_LIBRARY_NAME}_test)
108115
endif()
109116

modules/flow/Makefile

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ HEAD_FILES = \
3939
actions/sequence_action.h \
4040
actions/parallel_action.h \
4141
actions/if_else_action.h \
42+
actions/if_then_action.h \
4243
actions/loop_action.h \
4344
actions/loop_if_action.h \
4445
actions/event_action.h \
@@ -47,6 +48,7 @@ HEAD_FILES = \
4748
actions/wrapper_action.h \
4849
actions/succ_fail_action.h \
4950
actions/dummy_action.h \
51+
actions/switch_action.h \
5052
to_graphviz.h \
5153

5254
CPP_SRC_FILES = \
@@ -60,12 +62,15 @@ CPP_SRC_FILES = \
6062
actions/sequence_action.cpp \
6163
actions/parallel_action.cpp \
6264
actions/if_else_action.cpp \
65+
actions/if_then_action.cpp \
6366
actions/loop_action.cpp \
6467
actions/loop_if_action.cpp \
6568
actions/event_action.cpp \
6669
actions/repeat_action.cpp \
6770
actions/composite_action.cpp \
6871
actions/wrapper_action.cpp \
72+
actions/dummy_action.cpp \
73+
actions/switch_action.cpp \
6974
to_graphviz.cpp \
7075

7176
CXXFLAGS := -DMODULE_ID='"tbox.flow"' $(CXXFLAGS)
@@ -82,15 +87,17 @@ TEST_CPP_SRC_FILES = \
8287
actions/sequence_action_test.cpp \
8388
actions/parallel_action_test.cpp \
8489
actions/if_else_action_test.cpp \
90+
actions/if_then_action_test.cpp \
8591
actions/loop_action_test.cpp \
8692
actions/loop_if_action_test.cpp \
8793
actions/repeat_action_test.cpp \
8894
actions/composite_action_test.cpp \
8995
actions/wrapper_action_test.cpp \
9096
actions/succ_fail_action_test.cpp \
97+
actions/switch_action_test.cpp \
9198
to_graphviz_test.cpp \
9299

93-
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
94101
ENABLE_SHARED_LIB = no
95102

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

modules/flow/action.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ void Action::toJson(Json &js) const {
5656
js["label"] = label_;
5757
js["state"] = ToString(state_);
5858
js["result"] = ToString(result_);
59+
60+
if (!vars_.empty())
61+
vars_.toJson(js["vars"]);
5962
}
6063

6164
bool Action::start() {
@@ -324,6 +327,21 @@ void Action::cancelDispatchedCallback() {
324327
}
325328
}
326329

330+
bool Action::setParent(Action *parent) {
331+
//! 如果之前设置过了,就不能再设置
332+
if (parent != nullptr && parent_ != nullptr) {
333+
LogWarn("%d:%s[%s] can't set %d:%s[%s] as parent. its origin parent is %d:%s[%s]",
334+
id_, type_.c_str(), label_.c_str(),
335+
parent->id_, parent->type_.c_str(), parent->label_.c_str(),
336+
parent_->id_, parent_->type_.c_str(), parent_->label_.c_str());
337+
return false;
338+
}
339+
340+
vars_.setParent(&(parent->vars_));
341+
parent_ = parent;
342+
return true;
343+
}
344+
327345
Action::Reason::Reason(const Reason &other)
328346
: code(other.code)
329347
, message(other.message)

modules/flow/action.h

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <tbox/base/defines.h>
2929
#include <tbox/base/json_fwd.h>
3030
#include <tbox/event/loop.h>
31+
#include <tbox/util/variables.h>
3132

3233
#include "action_reason.h"
3334

@@ -123,6 +124,10 @@ class Action {
123124
bool stop(); //!< 停止
124125
void reset(); //!< 重置,将所有的状态恢复到刚构建状态
125126

127+
bool setParent(Action *parent);
128+
129+
util::Variables& vars() { return vars_; }
130+
126131
protected:
127132
//! 主动暂停动作
128133
bool block(const Reason &why = Reason(), //!< 阻塞原因
@@ -169,6 +174,9 @@ class Action {
169174
bool is_base_func_invoked_ = false; //! 是否已调用基类函数
170175
//! 检查使用者在重写的 onStart(),onPause(),onResume(),onStop(),onFinished() 中是否调用了基类的函数
171176
//! 如果没有调用,则打警告提示
177+
178+
Action *parent_ = nullptr;
179+
util::Variables vars_;
172180
};
173181

174182
//! 枚举转字串

modules/flow/action_executor.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ActionExecutor::ActionId ActionExecutor::append(Action *action, int prio) {
4747
};
4848
action_deque_array_.at(prio).push_back(item);
4949
action->setFinishCallback([this](bool, const Action::Reason &, const Action::Trace &) { schedule(); });
50+
action->vars().setParent(&vars_);
5051
schedule();
5152
return item.id;
5253
}

modules/flow/action_executor.h

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <array>
2525
#include <tbox/base/defines.h>
2626
#include <tbox/event/loop.h>
27+
#include <tbox/util/variables.h>
2728

2829
namespace tbox {
2930
namespace flow {
@@ -81,6 +82,8 @@ class ActionExecutor {
8182
void setActionFinishedCallback(const ActionCallback &cb) { action_finished_cb_ = cb; }
8283
void setAllFinishedCallback(const Callback &cb) { all_finished_cb_ = cb; }
8384

85+
inline util::Variables& vars() { return vars_; }
86+
8487
private:
8588
ActionId allocActionId();
8689
void schedule();
@@ -100,6 +103,8 @@ class ActionExecutor {
100103
Callback all_finished_cb_;
101104

102105
int cb_level_ = 0;
106+
107+
util::Variables vars_;
103108
};
104109

105110
}

modules/flow/action_reason.h

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#define ACTION_REASON_FAIL_ACTION 5 //!< "FailAction"
3030
#define ACTION_REASON_START_CHILD_FAIL 6 //!< "StartChildFail"
3131
#define ACTION_REASON_REPEAT_NO_TIMES 7 //!< "RepeatNoTimes"
32+
#define ACTION_REASON_SWITCH_FAIL 8 //!< "SwitchFail"
33+
#define ACTION_REASON_SWITCH_SKIP 9 //!< "SwitchSkip"
34+
#define ACTION_REASON_IF_THEN_SKIP 10 //!< "IfThenSkip"
3235

3336
//! 保存 1000 以内供 Action 自用,使用者自定义的 Reason 需 >= 1000
3437

modules/flow/actions/assemble_action.cpp

+100
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ int AssembleAction::addChild(Action *)
3030
return -1;
3131
}
3232

33+
int AssembleAction::addChildAs(Action *, const std::string &)
34+
{
35+
LogWarn("%d:%s[%s] not implement this function", id(), type().c_str(), label().c_str());
36+
return -1;
37+
}
38+
3339
bool AssembleAction::setChild(Action *)
3440
{
3541
LogWarn("%d:%s[%s] not implement this function", id(), type().c_str(), label().c_str());
@@ -48,5 +54,99 @@ void AssembleAction::onFinal()
4854
final_cb_();
4955
}
5056

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

0 commit comments

Comments
 (0)