Use structured bindings for config attribute ranges in most places

This commit is contained in:
Charles Dang 2024-09-14 23:03:28 -04:00
parent 857799da37
commit 78676a27ad
16 changed files with 62 additions and 66 deletions

View File

@ -299,8 +299,8 @@ void configuration::expand_simplified_aspects(side_number side, config &cfg) {
}
}
std::deque<std::pair<std::string, config>> facet_configs;
for (const config::attribute &attr : aiparam.attribute_range()) {
if (non_aspect_attributes.count(attr.first)) {
for(const auto& [key, value] : aiparam.attribute_range()) {
if (non_aspect_attributes.count(key)) {
continue;
}
config facet_config;
@ -308,8 +308,8 @@ void configuration::expand_simplified_aspects(side_number side, config &cfg) {
facet_config["name"] = "standard_aspect";
facet_config["turns"] = turns;
facet_config["time_of_day"] = time_of_day;
facet_config["value"] = attr.second;
facet_configs.emplace_back(attr.first, facet_config);
facet_config["value"] = value;
facet_configs.emplace_back(key, facet_config);
}
for (const config::any_child child : aiparam.all_children_range()) {
if (just_copy_tags.count(child.key)) {

View File

@ -647,9 +647,9 @@ void formula_ai::on_create(){
if (const auto ai_vars = cfg_.optional_child("vars"))
{
variant var;
for(const config::attribute &i : ai_vars->attribute_range()) {
var.serialize_from_string(i.second);
vars_.add(i.first, var);
for(const auto& [key, value] : ai_vars->attribute_range()) {
var.serialize_from_string(value);
vars_.add(key, var);
}
}

View File

@ -65,12 +65,12 @@ candidate_action_with_filters::candidate_action_with_filters(
auto filter_params = cfg.optional_child("filter");
if( filter_params ) {
for(const config::attribute& filter_param : filter_params->attribute_range())
for(const auto& [key, value] : filter_params->attribute_range())
{
const_formula_ptr filter_formula(
new formula(filter_param.second, function_table));
new formula(value, function_table));
filter_map_[filter_param.first]=filter_formula;
filter_map_[key]=filter_formula;
}
}
}

View File

@ -486,9 +486,9 @@ void add_color_info(const game_config_view& v, bool build_defaults)
}
for(const config &cp : v.child_range("color_palette")) {
for(const config::attribute& rgb : cp.attribute_range()) {
for(const auto& [key, value] : cp.attribute_range()) {
std::vector<color_t> temp;
for(const auto& s : utils::split(rgb.second)) {
for(const auto& s : utils::split(value)) {
try {
temp.push_back(color_t::from_hex_string(s));
} catch(const std::invalid_argument& e) {
@ -496,8 +496,8 @@ void add_color_info(const game_config_view& v, bool build_defaults)
}
}
team_rgb_colors.emplace(rgb.first, temp);
LOG_NG << "registered color palette: " << rgb.first;
team_rgb_colors.emplace(key, temp);
LOG_NG << "registered color palette: " << key;
}
}
}

View File

@ -317,9 +317,9 @@ bool load_strings(bool complain)
}
for (const config &lang : languages_) {
DBG_G << "[language]";
for (const config::attribute &j : lang.attribute_range()) {
DBG_G << j.first << "=\"" << j.second << "\"";
strings_[j.first] = j.second;
for(const auto& [key, value] : lang.attribute_range()) {
DBG_G << key << "=\"" << value << "\"";
strings_[key] = value;
}
DBG_G << "[/language]";
}

View File

@ -1310,8 +1310,8 @@ protected:
register_alias("whiteboard_options", "wbo");
if(auto alias_list = prefs::get().get_alias()) {
for(const config::attribute& a : alias_list->attribute_range()) {
register_alias(a.second, a.first);
for(const auto& [key, value] : alias_list->attribute_range()) {
register_alias(value, key);
}
}
}

View File

@ -169,13 +169,13 @@ bool movetype::terrain_info::data::config_has_changes(const config & new_values,
bool overwrite) const
{
if ( overwrite ) {
for (const config::attribute & a : new_values.attribute_range())
if ( a.second != cfg_[a.first] )
for (const auto& [key, value] : new_values.attribute_range())
if ( value != cfg_[key] )
return true;
}
else {
for (const config::attribute & a : new_values.attribute_range())
if ( a.second.to_int() != 0 )
for(const auto& [_, value] : new_values.attribute_range())
if ( value.to_int() != 0 )
return true;
}
@ -202,15 +202,15 @@ void movetype::terrain_info::data::merge(const config & new_values, bool overwri
// change "merge_attributes" to "merge_with".)
cfg_.merge_attributes(new_values);
else {
for (const config::attribute & a : new_values.attribute_range()) {
config::attribute_value & dest = cfg_[a.first];
for(const auto& [new_key, new_value] : new_values.attribute_range()) {
config::attribute_value & dest = cfg_[new_key];
int old = dest.to_int(params_.max_value);
// The new value is the absolute value of the old plus the
// provided value, capped between minimum and maximum, then
// given the sign of the old value.
// (Think defenses for why we might have negative values.)
int value = std::abs(old) + a.second.to_int(0);
int value = std::abs(old) + new_value.to_int(0);
value = std::max(params_.min_value, std::min(value, params_.max_value));
if ( old < 0 )
value = -value;
@ -727,8 +727,8 @@ utils::string_map_res movetype::resistances::damage_table() const
{
utils::string_map_res result;
for (const config::attribute & attrb : cfg_.attribute_range()) {
result[attrb.first] = attrb.second;
for(const auto& [key, value] : cfg_.attribute_range()) {
result[key] = value;
}
return result;
@ -755,9 +755,9 @@ void movetype::resistances::merge(const config & new_data, bool overwrite)
// change "merge_attributes" to "merge_with".)
cfg_.merge_attributes(new_data);
else
for (const config::attribute & a : new_data.attribute_range()) {
config::attribute_value & dest = cfg_[a.first];
dest = std::max(0, dest.to_int(100) + a.second.to_int(0));
for(const auto& [key, value] : new_data.attribute_range()) {
config::attribute_value & dest = cfg_[key];
dest = std::max(0, dest.to_int(100) + value.to_int(0));
}
}

View File

@ -195,9 +195,9 @@ void prefs::migrate_preferences(const std::string& migrate_prefs_file)
// when both files have the same attribute, use the one from whichever was most recently modified
bool current_prefs_are_older = filesystem::file_modified_time(filesystem::get_synced_prefs_file()) < filesystem::file_modified_time(migrate_prefs_file);
for(const config::attribute& val : old_cfg.attribute_range()) {
if(current_prefs_are_older || !current_cfg.has_attribute(val.first)) {
preferences_[val.first] = val.second;
for(const auto& [key, value] : old_cfg.attribute_range()) {
if(current_prefs_are_older || !current_cfg.has_attribute(key)) {
preferences_[key] = value;
}
}

View File

@ -235,12 +235,12 @@ static int impl_vconfig_get(lua_State *L)
if (shallow_literal || strcmp(m, "__shallow_parsed") == 0)
{
lua_newtable(L);
for (const config::attribute &a : v->get_config().attribute_range()) {
for(const auto& [key, value] : v->get_config().attribute_range()) {
if (shallow_literal)
luaW_pushscalar(L, a.second);
luaW_pushscalar(L, value);
else
luaW_pushscalar(L, v->expand(a.first));
lua_setfield(L, -2, a.first.c_str());
luaW_pushscalar(L, v->expand(key));
lua_setfield(L, -2, key.c_str());
}
vconfig::all_children_iterator i = v->ordered_begin(),
i_end = v->ordered_end();
@ -667,10 +667,10 @@ void luaW_filltable(lua_State *L, const config& cfg)
lua_rawseti(L, -2, 2);
lua_rawseti(L, -2, k++);
}
for (const config::attribute &attr : cfg.attribute_range())
for(const auto& [key, value] : cfg.attribute_range())
{
luaW_pushscalar(L, attr.second);
lua_setfield(L, -2, attr.first.c_str());
luaW_pushscalar(L, value);
lua_setfield(L, -2, key.c_str());
}
}

View File

@ -712,13 +712,13 @@ static void write_internal(const config& cfg, std::ostream& out, std::string& te
throw config::error("Too many recursion levels in config write");
}
for(const config::attribute& i : cfg.attribute_range()) {
if(!config::valid_attribute(i.first)) {
ERR_CF << "Config contains invalid attribute name '" << i.first << "', skipping...";
for(const auto& [key, value] : cfg.attribute_range()) {
if(!config::valid_attribute(key)) {
ERR_CF << "Config contains invalid attribute name '" << key << "', skipping...";
continue;
}
write_key_val(out, i.first, i.second, tab, textdomain);
write_key_val(out, key, value, tab, textdomain);
}
for(const config::any_child item : cfg.all_children_range()) {

View File

@ -71,8 +71,8 @@ std::string format_addon_feedback_url(const std::string& format, const config& p
// 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 config::attribute& a : attrs) {
escaped[a.first] = utils::urlencode(a.second.str());
for(const auto& [key, value] : attrs) {
escaped[key] = utils::urlencode(value.str());
}
// FIXME: We cannot use utils::interpolate_variables_into_string

View File

@ -64,10 +64,10 @@ static void write_str_int_map(config_writer& out, const stats_t::str_int_map& m)
static stats_t::str_int_map read_str_int_map(const config& cfg)
{
stats_t::str_int_map m;
for(const config::attribute& i : cfg.attribute_range()) {
for(const auto& [key, value] : cfg.attribute_range()) {
try {
for(const std::string& val : utils::split(i.second)) {
m[val] = std::stoi(i.first);
for(const std::string& val : utils::split(value)) {
m[val] = std::stoi(key);
}
} catch(const std::invalid_argument&) {
ERR_NG << "Invalid statistics entry; skipping";

View File

@ -35,9 +35,9 @@ void unit_formula_manager::read(const config & ai)
formula_vars_ = std::make_shared<wfl::map_formula_callable>();
wfl::variant var;
for (const config::attribute &i : ai_vars->attribute_range()) {
var.serialize_from_string(i.second);
formula_vars_->add(i.first, var);
for(const auto& [key, value] : ai_vars->attribute_range()) {
var.serialize_from_string(value);
formula_vars_->add(key, var);
}
} else {
formula_vars_ = wfl::map_formula_callable_ptr();

View File

@ -1123,15 +1123,13 @@ void unit_type_data::set_config(const game_config_view& cfg)
for(const config& r : cfg.child_range("resistance_defaults")) {
const std::string& dmg_type = r["id"];
for(const config::attribute& attr : r.attribute_range()) {
const std::string& mt = attr.first;
for(const auto& [mt, value] : r.attribute_range()) {
if(mt == "id" || mt == "default" || movement_types_.find(mt) == movement_types_.end()) {
continue;
}
DBG_CF << "Patching specific movetype " << mt;
patch_movetype(movement_types_[mt], "resistance", dmg_type, attr.second, 100, true);
patch_movetype(movement_types_[mt], "resistance", dmg_type, value, 100, true);
}
if(r.has_attribute("default")) {
@ -1185,15 +1183,13 @@ void unit_type_data::set_config(const game_config_view& cfg)
const config& info = terrain.mandatory_child(*src_tag);
for(const config::attribute& attr : info.attribute_range()) {
const std::string& mt = attr.first;
for(const auto& [mt, value] : info.attribute_range()) {
if(mt == "default" || movement_types_.find(mt) == movement_types_.end()) {
continue;
}
patch_movetype(
movement_types_[mt], cost_type.subtag, ter_type, attr.second, cost_type.default_val, true);
movement_types_[mt], cost_type.subtag, ter_type, value, cost_type.default_val, true);
}
if(info.has_attribute("default")) {

View File

@ -641,9 +641,9 @@ void unit::init(const config& cfg, bool use_traits, const vconfig* vcfg)
movement_type_.merge(cfg);
if(auto status_flags = cfg.optional_child("status")) {
for(const config::attribute &st : status_flags->attribute_range()) {
if(st.second.to_bool()) {
set_state(st.first, true);
for(const auto& [key, value] : status_flags->attribute_range()) {
if(value.to_bool()) {
set_state(key, true);
}
}
}

View File

@ -180,8 +180,8 @@ config vconfig::get_parsed_config() const
config res;
for (const config::attribute &i : cfg_->attribute_range()) {
res[i.first] = expand(i.first);
for(const auto& [key, _] : cfg_->attribute_range()) {
res[key] = expand(key);
}
for (const config::any_child child : cfg_->all_children_range())