Config: more structured bindings for attribute_range

This commit is contained in:
Charles Dang 2024-09-27 03:22:35 -04:00
parent 95c9f0c720
commit 1d9f57a668
9 changed files with 58 additions and 62 deletions

View file

@ -333,12 +333,12 @@ variant unit_callable::get_value(const std::string& key) const
needs_flip = true;
}
std::map<variant, variant> res;
for(const auto& p : cfg.attribute_range()) {
int val = p.second;
for(const auto& [key, value] : cfg.attribute_range()) {
int val = value.to_int();
if(needs_flip) {
val = 100 - val;
}
res.emplace(variant(p.first), variant(val));
res.emplace(variant(key), variant(val));
}
return variant(res);
@ -571,8 +571,8 @@ variant config_callable::get_value(const std::string& key) const
return variant(result);
} else if(key == "__attributes") {
std::map<variant,variant> result;
for(const auto& val : cfg_.attribute_range()) {
result[variant(val.first)] = val.second.apply_visitor(fai_variant_visitor());
for(const auto& [key, value] : cfg_.attribute_range()) {
result[variant(key)] = value.apply_visitor(fai_variant_visitor());
}
return variant(result);
@ -587,9 +587,9 @@ void config_callable::get_inputs(formula_input_vector& inputs) const
add_input(inputs, "__children");
add_input(inputs, "__attributes");
for(const auto& val : cfg_.attribute_range()) {
if(val.first.find_first_not_of(formula::id_chars) != std::string::npos) {
add_input(inputs, val.first);
for(const auto& [key, _] : cfg_.attribute_range()) {
if(key.find_first_not_of(formula::id_chars) != std::string::npos) {
add_input(inputs, key);
}
}
}

View file

@ -61,19 +61,17 @@ namespace {
for(const auto& filter : filtertext_)
{
bool found = false;
for(const auto& attribute : cfg.attribute_range())
for(const auto& [_, value] : cfg.attribute_range())
{
std::string val = attribute.second.str();
if(translation::ci_search(val, filter))
if(translation::ci_search(value.str(), filter))
{
found = true;
break;
}
}
for(const config& child : cfg.child_range("translation")) {
for(const auto& attribute : child.attribute_range()) {
std::string val = attribute.second.str();
if(translation::ci_search(val, filter)) {
for(const auto& [_, value] : child.attribute_range()) {
if(translation::ci_search(value.str(), filter)) {
found = true;
break;
}

View file

@ -163,8 +163,8 @@ void editor_edit_unit::pre_show() {
.mandatory_child("units")
.mandatory_child("movetype")
.mandatory_child("defense");
for (const auto& attribute : defense_attr.attribute_range()) {
defense_list_.emplace_back("label", attribute.first);
for (const auto& [key, _] : defense_attr.attribute_range()) {
defense_list_.emplace_back("label", key);
}
menu_button& movement_costs = find_widget<menu_button>("movement_costs_list");
@ -181,8 +181,8 @@ void editor_edit_unit::pre_show() {
.mandatory_child("units")
.mandatory_child("movetype")
.mandatory_child("resistance");
for (const auto& attribute : resistances_attr.attribute_range()) {
resistances_list_.emplace_back("label", attribute.first, "icon", "icons/profiles/" + attribute.first + ".png");
for (const auto& [key, _] : resistances_attr.attribute_range()) {
resistances_list_.emplace_back("label", key, "icon", "icons/profiles/" + key + ".png");
}
if (!resistances_list_.empty()) {
@ -413,8 +413,8 @@ void editor_edit_unit::load_unit_type() {
break;
}
for (const auto& attr : type->get_cfg().mandatory_child("resistance").attribute_range()) {
if (resistances_list_.at(i)["label"] == attr.first) {
for (const auto& [key, _] : type->get_cfg().mandatory_child("resistance").attribute_range()) {
if (resistances_list_.at(i)["label"] == key) {
res_toggles_[i] = 1;
}
}
@ -422,16 +422,16 @@ void editor_edit_unit::load_unit_type() {
for (unsigned i = 0; i < defense_list_.size(); i++) {
if (type->get_cfg().has_child("defense")) {
for (const auto& attr : type->get_cfg().mandatory_child("defense").attribute_range()) {
if (defense_list_.at(i)["label"] == attr.first) {
for (const auto& [key, _] : type->get_cfg().mandatory_child("defense").attribute_range()) {
if (defense_list_.at(i)["label"] == key) {
def_toggles_[i] = 1;
}
}
}
if (type->get_cfg().has_child("movement_costs")) {
for (const auto& attr : type->get_cfg().mandatory_child("movement_costs").attribute_range()) {
if (defense_list_.at(i)["label"] == attr.first) {
for (const auto& [key, _] : type->get_cfg().mandatory_child("movement_costs").attribute_range()) {
if (defense_list_.at(i)["label"] == key) {
move_toggles_[i] = 1;
}
}
@ -530,9 +530,9 @@ void editor_edit_unit::save_unit_type() {
if (res_toggles_.any()) {
config& resistances = utype.add_child("resistance");
int i = 0;
for (const auto& attr : resistances_.attribute_range()) {
for (const auto& [key, _] : resistances_.attribute_range()) {
if (res_toggles_[i]) {
resistances[attr.first] = resistances_[attr.first];
resistances[key] = resistances_[key];
}
i++;
}
@ -541,9 +541,9 @@ void editor_edit_unit::save_unit_type() {
if (def_toggles_.any()) {
config& defenses = utype.add_child("defense");
int i = 0;
for (const auto& attr : defenses_.attribute_range()) {
for (const auto& [key, _] : defenses_.attribute_range()) {
if (def_toggles_[i]) {
defenses[attr.first] = defenses_[attr.first];
defenses[key] = defenses_[key];
}
i++;
}
@ -552,9 +552,9 @@ void editor_edit_unit::save_unit_type() {
if (move_toggles_.any()) {
config& movement_costs = utype.add_child("movement_costs");
int i = 0;
for (const auto& attr : movement_.attribute_range()) {
for (const auto& [key, _] : movement_.attribute_range()) {
if (move_toggles_[i]) {
movement_costs[attr.first] = movement_[attr.first];
movement_costs[key] = movement_[key];
}
i++;
}
@ -878,8 +878,8 @@ void editor_edit_unit::update_wml_view() {
out.open_child("unit_type");
level++;
for (const auto& attr : type_cfg_.mandatory_child("unit_type").attribute_range()) {
::write_key_val(wml_stream, attr.first, attr.second, level, current_textdomain);
for (const auto& [key, value] : type_cfg_.mandatory_child("unit_type").attribute_range()) {
::write_key_val(wml_stream, key, value, level, current_textdomain);
}
// Abilities
@ -898,9 +898,9 @@ void editor_edit_unit::update_wml_view() {
for (const auto& atk : attacks_) {
out.open_child("attack");
level++;
for (const auto& attr : atk.second.attribute_range()) {
if (!attr.second.empty()) {
::write_key_val(wml_stream, attr.first, attr.second, level, current_textdomain);
for (const auto& [key, value] : atk.second.attribute_range()) {
if (!value.empty()) {
::write_key_val(wml_stream, key, value, level, current_textdomain);
}
}
@ -928,9 +928,9 @@ void editor_edit_unit::update_wml_view() {
out.open_child("movement_costs");
level++;
int i = 0;
for (const auto& attr : movement_.attribute_range()) {
for (const auto& [key, value] : movement_.attribute_range()) {
if (move_toggles_[i] == 1) {
::write_key_val(wml_stream, attr.first, attr.second, level, current_textdomain);
::write_key_val(wml_stream, key, value, level, current_textdomain);
}
i++;
}
@ -943,9 +943,9 @@ void editor_edit_unit::update_wml_view() {
out.open_child("defense");
level++;
int i = 0;
for (const auto& attr : defenses_.attribute_range()) {
for (const auto& [key, value] : defenses_.attribute_range()) {
if (def_toggles_[i] == 1) {
::write_key_val(wml_stream, attr.first, attr.second, level, current_textdomain);
::write_key_val(wml_stream, key, value, level, current_textdomain);
}
i++;
}
@ -958,9 +958,9 @@ void editor_edit_unit::update_wml_view() {
out.open_child("resistance");
level++;
int i = 0;
for (const auto& attr : resistances_.attribute_range()) {
for (const auto& [key, value] : resistances_.attribute_range()) {
if (res_toggles_[i] == 1) {
::write_key_val(wml_stream, attr.first, attr.second, level, current_textdomain);
::write_key_val(wml_stream, key, value, level, current_textdomain);
}
i++;
}

View file

@ -325,8 +325,8 @@ config mp_options_helper::get_options_config()
// TODO: enable this as soon as we drop the old mp configure screen.
mod.add_child("options", options_data_[source.id]);
#else
for(const auto& option : options_data_[source.id].attribute_range()) {
mod.add_child("option", config {"id", option.first, "value", option.second});
for(const auto& [key, value] : options_data_[source.id].attribute_range()) {
mod.add_child("option", config {"id", key, "value", value});
}
#endif
}

View file

@ -727,15 +727,15 @@ static std::vector<widget_data> parse_list_data(const config& data, const unsign
for(const auto& c : cols) {
list_data.emplace_back();
for(const auto& i : c.attribute_range()) {
list_data.back()[""][i.first] = i.second;
for(const auto& [key, value] : c.attribute_range()) {
list_data.back()[""][key] = value;
}
for(const auto& w : c.child_range("widget")) {
VALIDATE(w.has_attribute("id"), missing_mandatory_wml_key("[list_data][row][column][widget]", "id"));
for(const auto& i : w.attribute_range()) {
list_data.back()[w["id"]][i.first] = i.second;
for(const auto& [key, value] : w.attribute_range()) {
list_data.back()[w["id"]][key] = value;
}
}
}

View file

@ -208,9 +208,9 @@ builder_multi_page::builder_multi_page(const config& cfg)
for(const auto & column : row.child_range("column"))
{
data.emplace_back();
for(const auto & i : column.attribute_range())
for(const auto& [key, value] : column.attribute_range())
{
data.back()[i.first] = i.second;
data.back()[key] = value;
}
++col;
}

View file

@ -598,10 +598,10 @@ std::pair<config, point> rich_label::get_parsed_text(
DBG_GUI_RL << "span/format: text=" << line;
DBG_GUI_RL << "attributes:";
for (const auto& attr : child.attribute_range()) {
if (attr.first != "text") {
add_attribute(*curr_item, attr.first, start, end, attr.second);
DBG_GUI_RL << attr.first << "=" << attr.second;
for (const auto& [key, value] : child.attribute_range()) {
if (key != "text") {
add_attribute(*curr_item, key, start, end, value);
DBG_GUI_RL << key << "=" << value;
}
}

View file

@ -260,14 +260,14 @@ void prefs::load_preferences()
preferences_.merge_with(synced_prefs);
// check for any unknown preferences
for(const auto& attr : synced_prefs.attribute_range()) {
if(std::find(synced_attributes_.begin(), synced_attributes_.end(), attr.first) == synced_attributes_.end()) {
unknown_synced_attributes_.insert(attr.first);
for(const auto& [key, _] : synced_prefs.attribute_range()) {
if(std::find(synced_attributes_.begin(), synced_attributes_.end(), key) == synced_attributes_.end()) {
unknown_synced_attributes_.insert(key);
}
}
for(const auto& attr : unsynced_prefs.attribute_range()) {
if(std::find(unsynced_attributes_.begin(), unsynced_attributes_.end(), attr.first) == unsynced_attributes_.end()) {
unknown_unsynced_attributes_.insert(attr.first);
for(const auto& [key, _] : unsynced_prefs.attribute_range()) {
if(std::find(unsynced_attributes_.begin(), unsynced_attributes_.end(), key) == unsynced_attributes_.end()) {
unknown_unsynced_attributes_.insert(key);
}
}

View file

@ -66,12 +66,10 @@ std::string format_addon_feedback_url(const std::string& format, const config& p
if(!format.empty() && !params.empty()) {
plain_string_map escaped;
config::const_attr_itors attrs = params.attribute_range();
// Percent-encode parameter values for URL interpolation. This is
// VERY important since otherwise people could e.g. alter query
// strings from the format string.
for(const auto& [key, value] : attrs) {
for(const auto& [key, value] : params.attribute_range()) {
escaped[key] = utils::urlencode(value.str());
}