Skip to content

Commit 8ac7718

Browse files
committed
feat(main): 给Module添加变量,并提供vars()接口
1 parent cda73b4 commit 8ac7718

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

modules/main/module.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
* of the source tree.
1919
*/
2020
#include "module.h"
21+
2122
#include <algorithm>
23+
2224
#include <tbox/base/log.h>
2325
#include <tbox/base/json.hpp>
2426

@@ -40,11 +42,15 @@ Module::~Module()
4042

4143
bool Module::add(Module *child, bool required)
4244
{
43-
if (state_ != State::kNone)
45+
if (state_ != State::kNone) {
46+
LogWarn("module %s's state is not State::kNone", name_.c_str());
4447
return false;
48+
}
4549

46-
if (child == nullptr)
50+
if (child == nullptr) {
51+
LogWarn("child == nullptr");
4752
return false;
53+
}
4854

4955
auto iter = std::find_if(children_.begin(), children_.end(),
5056
[child] (const ModuleItem &item) {
@@ -55,6 +61,8 @@ bool Module::add(Module *child, bool required)
5561
return false;
5662

5763
children_.emplace_back(ModuleItem{ child, required });
64+
child->vars_.setParent(&vars_);
65+
5866
return true;
5967
}
6068

modules/main/module.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
#define TBOX_MAIN_MODULE_H_20220326
2222

2323
#include <vector>
24+
2425
#include <tbox/base/json_fwd.h>
26+
#include <tbox/util/variables.h>
27+
2528
#include "context.h"
2629

2730
namespace tbox {
@@ -105,6 +108,7 @@ class Module {
105108
inline std::string name() const { return name_; }
106109
inline Context& ctx() const { return ctx_; }
107110
inline State state() const { return state_; }
111+
inline util::Variables& vars() { return vars_; }
108112

109113
protected:
110114
//! 下面的5个虚函数,可由使用者根据需要重写。如果没有操作,就不用重写
@@ -130,6 +134,8 @@ class Module {
130134
};
131135
std::vector<ModuleItem> children_;
132136
State state_ = State::kNone;
137+
138+
util::Variables vars_;
133139
};
134140

135141
}

0 commit comments

Comments
 (0)