Skip to content

Commit

Permalink
Variadic config methods to combine remove_attribute or clear_children…
Browse files Browse the repository at this point in the history
… calls
  • Loading branch information
CelticMinstrel committed Apr 4, 2016
1 parent 38010fd commit ff3e1bf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/ai/default/recruitment.cpp
Expand Up @@ -1786,12 +1786,11 @@ recruitment_aspect::recruitment_aspect(readonly_context &context, const config &
parsed_cfg["pattern"] = true;
parsed_cfg.add_child("recruit", pattern);
}
parsed_cfg.clear_children("pattern");
for (config total : parsed_cfg.child_range("total")) {
parsed_cfg["total"] = true;
parsed_cfg.add_child("recruit", total);
}
parsed_cfg.clear_children("total");
parsed_cfg.clear_children("pattern", "total");
// Then, if there's no [recruit], add one.
if (!parsed_cfg.has_child("recruit")) {
parsed_cfg.add_child("recruit", config_of("importance", 0));
Expand Down
6 changes: 1 addition & 5 deletions src/carryover.cpp
Expand Up @@ -34,11 +34,7 @@ carryover::carryover(const config& side)
for(const config& u : side.child_range("unit")) {
recall_list_.push_back(u);
config& u_back = recall_list_.back();
u_back.remove_attribute("side");
u_back.remove_attribute("goto_x");
u_back.remove_attribute("goto_y");
u_back.remove_attribute("x");
u_back.remove_attribute("y");
u_back.remove_attributes("side", "goto_x", "goto_y", "x", "y");
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/config.hpp
Expand Up @@ -450,7 +450,9 @@ class config
template<int N>
config &child(const char(&key)[N], int n = 0)
{ return child_impl(key, N - 1, n); }
private:
config &child_impl(const char* key, int len, int n = 0);
public:
#endif
/**
* Returns the nth child with the given @a key, or
Expand Down Expand Up @@ -564,6 +566,12 @@ class config

void remove_attribute(const std::string &key);
void merge_attributes(const config &);
template<typename... T>
void remove_attributes(T... keys) {
for(const std::string& key : {keys...}) {
remove_attribute(key);
}
}

const_attr_itors attribute_range() const;

Expand All @@ -579,6 +587,12 @@ class config
{ return const_cast<config *>(this)->find_child(key, name, value); }

void clear_children(const std::string& key);
template<typename... T>
void clear_children(T... keys) {
for(std::string key : {keys...}) {
clear_children(key);
}
}

/**
* Moves all the children with tag @a key from @a src to this.
Expand Down Expand Up @@ -660,7 +674,7 @@ class config
* A function to get the differences between this object,
* and 'c', as another config object.
* I.e. calling cfg2.apply_diff(cfg1.get_diff(cfg2))
* will make cfg1 identical to cfg2.
* will make cfg2 identical to cfg1.
*/
config get_diff(const config& c) const;
void get_diff(const config& c, config& res) const;
Expand Down
6 changes: 1 addition & 5 deletions src/game_initialization/connect_engine.cpp
Expand Up @@ -988,11 +988,7 @@ config side_engine::new_config() const
// Merge the faction data to res.
config faction = flg_.current_faction();
res["faction_name"] = faction["name"];
faction.remove_attribute("id");
faction.remove_attribute("name");
faction.remove_attribute("image");
faction.remove_attribute("gender");
faction.remove_attribute("type");
faction.remove_attributes("id", "name", "image", "gender", "type");
res.append(faction);
}

Expand Down
3 changes: 1 addition & 2 deletions src/gui/dialogs/gamestate_inspector.cpp
Expand Up @@ -563,8 +563,7 @@ class team_mode_controller : public single_mode_controller
config c = resources::teams
? resources::teams->at(side_ - 1).to_config()
: config();
c.clear_children("ai");
c.clear_children("village");
c.clear_children("ai", "village");
model_.set_inspect_window_text(config_to_string(c));
return;
}
Expand Down

0 comments on commit ff3e1bf

Please sign in to comment.