Config: use structured bindings

This commit is contained in:
Charles Dang 2024-09-14 16:52:25 -04:00
parent a9e6af20d3
commit 6df2653c61

View file

@ -189,8 +189,8 @@ void config::append_children(config&& cfg)
void config::append_attributes(const config& cfg)
{
for(const attribute& v : cfg.values_) {
values_[v.first] = v.second;
for(const auto& [key, value] : cfg.values_) {
values_[key] = value;
}
}
@ -204,8 +204,8 @@ void config::append_children(const config& cfg, config_key_type key)
void config::append(const config& cfg)
{
append_children(cfg);
for(const attribute& v : cfg.values_) {
values_[v.first] = v.second;
for(const auto& [key, value] : cfg.values_) {
values_[key] = value;
}
}
@ -218,9 +218,9 @@ void config::append(config&& cfg)
values_ = std::move(cfg.values_);
}
else {
for(const attribute& v : cfg.values_) {
for(const auto& [key, value] : cfg.values_) {
//TODO: move the attributes as well?
values_[v.first] = v.second;
values_[key] = value;
}
}
cfg.clear_attributes();
@ -748,18 +748,17 @@ const config::attribute_value& config::get_deprecated_attribute(config_key_type
void config::merge_attributes(const config& cfg)
{
assert(this != &cfg);
for(const attribute& v : cfg.values_) {
std::string key = v.first;
for(const auto& [key, value] : cfg.values_) {
if(key.substr(0, 7) == "add_to_") {
std::string add_to = key.substr(7);
values_[add_to] = values_[add_to].to_double() + v.second.to_double();
values_[add_to] = values_[add_to].to_double() + value.to_double();
} else if(key.substr(0, 10) == "concat_to_") {
std::string concat_to = key.substr(10);
// TODO: Only use t_string if one or both are actually translatable?
// That probably requires using a visitor though.
values_[concat_to] = values_[concat_to].t_str() + v.second.t_str();
values_[concat_to] = values_[concat_to].t_str() + value.t_str();
} else {
values_[v.first] = v.second;
values_[key] = value;
}
}
}
@ -925,35 +924,35 @@ void config::get_diff(const config& c, config& res) const
{
config* inserts = nullptr;
for(const auto& v : values_) {
if(v.second.blank()) {
for(const auto& [key, value] : values_) {
if(value.blank()) {
continue;
}
const attribute_map::const_iterator j = c.values_.find(v.first);
if(j == c.values_.end() || (v.second != j->second && !v.second.blank())) {
const attribute_map::const_iterator j = c.values_.find(key);
if(j == c.values_.end() || (value != j->second && !value.blank())) {
if(inserts == nullptr) {
inserts = &res.add_child("insert");
}
(*inserts)[v.first] = v.second;
(*inserts)[key] = value;
}
}
config* deletes = nullptr;
for(const auto& v : c.values_) {
if(v.second.blank()) {
for(const auto& [key, value] : c.values_) {
if(value.blank()) {
continue;
}
const attribute_map::const_iterator itor = values_.find(v.first);
const attribute_map::const_iterator itor = values_.find(key);
if(itor == values_.end() || itor->second.blank()) {
if(deletes == nullptr) {
deletes = &res.add_child("delete");
}
(*deletes)[v.first] = "x";
(*deletes)[key] = "x";
}
}
@ -1037,8 +1036,8 @@ void config::apply_diff(const config& diff, bool track /* = false */)
}
if(const auto inserts = diff.optional_child("insert")) {
for(const attribute& v : inserts->attribute_range()) {
values_[v.first] = v.second;
for(const auto& [key, value] : inserts->attribute_range()) {
values_[key] = value;
}
}
@ -1158,11 +1157,10 @@ void config::merge_with(const config& c)
}
// Now add any unvisited tags
for(const auto& pair : c.children_) {
const std::string& tag = pair.first;
for(const auto& [tag, list] : c.children_) {
unsigned& visits = visitations[tag];
while(visits < pair.second.size()) {
add_child(tag, *pair.second[visits++]);
while(visits < list.size()) {
add_child(tag, *list[visits++]);
}
}
@ -1192,10 +1190,10 @@ void config::inherit_from(const config& c)
*/
void config::inherit_attributes(const config& cfg)
{
for(const attribute& v : cfg.values_) {
attribute_value& v2 = values_[v.first];
for(const auto& [key, value] : cfg.values_) {
attribute_value& v2 = values_[key];
if(v2.blank()) {
v2 = v.second;
v2 = value;
}
}
}
@ -1203,16 +1201,16 @@ bool config::matches(const config& filter) const
{
bool result = true;
for(const attribute& i : filter.attribute_range()) {
if(i.first.compare(0, 8, "glob_on_") == 0) {
const attribute_value* v = get(i.first.substr(8));
if(!v || !utils::wildcard_string_match(v->str(), i.second.str())) {
for(const auto& [key, value] : filter.attribute_range()) {
if(key.compare(0, 8, "glob_on_") == 0) {
const attribute_value* v = get(key.substr(8));
if(!v || !utils::wildcard_string_match(v->str(), value.str())) {
result = false;
break;
}
} else {
const attribute_value* v = get(i.first);
if(!v || *v != i.second) {
const attribute_value* v = get(key);
if(!v || *v != value) {
result = false;
break;
}
@ -1257,8 +1255,8 @@ std::ostream& operator<<(std::ostream& outstream, const config& cfg)
static int i = 0;
i++;
for(const config::attribute& val : cfg.attribute_range()) {
if(val.second.blank()) {
for(const auto& [key, value] : cfg.attribute_range()) {
if(value.blank()) {
continue;
}
@ -1266,7 +1264,7 @@ std::ostream& operator<<(std::ostream& outstream, const config& cfg)
outstream << '\t';
}
outstream << val.first << " = " << val.second << '\n';
outstream << key << " = " << value << '\n';
}
for(const config::any_child child : cfg.all_children_range()) {
@ -1302,19 +1300,19 @@ std::string config::hash() const
hash_str[hash_length] = 0;
i = 0;
for(const attribute& val : values_) {
if(val.second.blank()) {
for(const auto& [key, value] : values_) {
if(value.blank()) {
continue;
}
for(char c : val.first) {
for(char c : key) {
hash_str[i] ^= c;
if(++i == hash_length) {
i = 0;
}
}
std::string base_str = val.second.t_str().base_str();
std::string base_str = value.t_str().base_str();
for(const char c : base_str) {
hash_str[i] ^= c;
if(++i == hash_length) {