@@ -54,18 +54,35 @@ bool Module::add(Module *child, bool required)
5454
5555 auto iter = std::find_if (children_.begin (), children_.end (),
5656 [child] (const ModuleItem &item) {
57- return item.module_ptr == child;
57+ return item.module_ptr == child ||
58+ item.module_ptr ->name () == child->name ();
5859 }
5960 );
60- if (iter != children_.end ())
61+ if (iter != children_.end ()) {
62+ LogWarn (" module %s is already exist or name duplicated." , child->name ().c_str ());
6163 return false ;
64+ }
6265
6366 children_.emplace_back (ModuleItem{ child, required });
6467 child->vars_ .setParent (&vars_);
6568
6669 return true ;
6770}
6871
72+ bool Module::addAs (Module *child, const std::string &name, bool required)
73+ {
74+ auto tmp = child->name_ ;
75+ child->name_ = name;
76+
77+ if (!add (child, required)) {
78+ // ! 如果失败了,还原之前的名称
79+ child->name_ = tmp;
80+ return false ;
81+ }
82+
83+ return true ;
84+ }
85+
6986void Module::fillDefaultConfig (Json &js_parent)
7087{
7188 Json &js_this = name_.empty () ? js_parent : js_parent[name_];
@@ -157,5 +174,20 @@ void Module::cleanup()
157174 state_ = State::kNone ;
158175}
159176
177+ void Module::toJson (Json &js) const
178+ {
179+ if (!vars_.empty ())
180+ vars_.toJson (js[" vars" ]);
181+
182+ if (!children_.empty ()) {
183+ Json &js_children = js[" children" ];
184+ for (auto &item : children_) {
185+ Json &js_child = js_children[item.module_ptr ->name ()];
186+ js_child[" required" ] = item.required ;
187+ item.module_ptr ->toJson (js_child);
188+ }
189+ }
190+ }
191+
160192}
161193}
0 commit comments