Deployed config::optional_child in a bunch of places

This commit is contained in:
Charles Dang 2021-07-02 14:55:23 -04:00
parent f5de3cca18
commit f6327849f3
12 changed files with 90 additions and 87 deletions

View File

@ -675,10 +675,10 @@ void formula_ai::on_create(){
}
vars_ = map_formula_callable();
if (const config &ai_vars = cfg_.child("vars"))
if (const auto ai_vars = cfg_.optional_child("vars"))
{
variant var;
for(const config::attribute &i : ai_vars.attribute_range()) {
for(const config::attribute &i : ai_vars->attribute_range()) {
var.serialize_from_string(i.second);
vars_.add(i.first, var);
}

View File

@ -1105,14 +1105,14 @@ void config::apply_diff(const config& diff, bool track /* = false */)
values_[diff_track_attribute] = "modified";
}
if(const config& inserts = diff.child("insert")) {
for(const attribute& v : inserts.attribute_range()) {
if(const auto inserts = diff.optional_child("insert")) {
for(const attribute& v : inserts->attribute_range()) {
values_[v.first] = v.second;
}
}
if(const config& deletes = diff.child("delete")) {
for(const attribute& v : deletes.attribute_range()) {
if(const auto deletes = diff.optional_child("delete")) {
for(const attribute& v : deletes->attribute_range()) {
values_.erase(v.first);
}
}

View File

@ -685,12 +685,12 @@ void context_manager::init_map_generators(const game_config_view& game_config)
continue;
}
const config& generator_cfg = i.child("generator");
if(!generator_cfg) {
// TODO: we should probably use `child` with a try/catch block once that function throws
if(const auto generator_cfg = i.optional_child("generator")) {
map_generators_.emplace_back(create_map_generator(i["map_generation"].empty() ? i["scenario_generation"] : i["map_generation"], generator_cfg.value()));
} else {
ERR_ED << "Scenario \"" << i["name"] << "\" with id " << i["id"]
<< " has map_generation= but no [generator] tag" << std::endl;
} else {
map_generators_.emplace_back(create_map_generator(i["map_generation"].empty() ? i["scenario_generation"] : i["map_generation"], generator_cfg));
}
}
}

View File

@ -62,8 +62,8 @@ const config& game_config_view::find_child(config_key_type key, const std::strin
const config& game_config_view::child(config_key_type key) const
{
for(const config& cfg : cfgs_) {
if(const config& res = cfg.child(key)) {
return res;
if(const auto res = cfg.optional_child(key)) {
return res.value();
}
}
const config cfg;
@ -74,8 +74,8 @@ const config& game_config_view::child(config_key_type key) const
const config& game_config_view::child_or_empty(config_key_type key) const
{
for(const config& cfg : cfgs_) {
if(const config& res = cfg.child(key)) {
return res;
if(const auto res = cfg.optional_child(key)) {
return res.value();
}
}
static const config cfg;

View File

@ -103,29 +103,29 @@ builder_widget_ptr create_widget_builder(const config& cfg)
config::const_all_children_itors children = cfg.all_children_range();
VALIDATE(children.size() == 1, "Grid cell does not have exactly 1 child.");
if(const config& grid = cfg.child("grid")) {
return std::make_shared<builder_grid>(grid);
if(const auto grid = cfg.optional_child("grid")) {
return std::make_shared<builder_grid>(grid.value());
}
if(const config& instance = cfg.child("instance")) {
return std::make_shared<implementation::builder_instance>(instance);
if(const auto instance = cfg.optional_child("instance")) {
return std::make_shared<implementation::builder_instance>(instance.value());
}
if(const config& pane = cfg.child("pane")) {
return std::make_shared<implementation::builder_pane>(pane);
if(const auto pane = cfg.optional_child("pane")) {
return std::make_shared<implementation::builder_pane>(pane.value());
}
if(const config& viewport = cfg.child("viewport")) {
return std::make_shared<implementation::builder_viewport>(viewport);
if(const auto viewport = cfg.optional_child("viewport")) {
return std::make_shared<implementation::builder_viewport>(viewport.value());
}
for(const auto& item : widget_builder_lookup()) {
if(item.first == "window" || item.first == "tooltip") {
for(const auto& [type, builder] : widget_builder_lookup()) {
if(type == "window" || type == "tooltip") {
continue;
}
if(const config& c = cfg.child(item.first)) {
return item.second(c);
if(const auto c = cfg.optional_child(type)) {
return builder(c.value());
}
}

View File

@ -135,10 +135,10 @@ turn_info::PROCESS_DATA_RESULT turn_info::process_network_data(const config& cfg
ERR_NW << "processing network data while still having data on the replay." << std::endl;
}
if (const config &message = cfg.child("message"))
if (const auto message = cfg.optional_child("message"))
{
game_display::get_singleton()->get_chat_manager().add_chat_message(std::time(nullptr), message["sender"], message["side"],
message["message"], events::chat_handler::MESSAGE_PUBLIC,
game_display::get_singleton()->get_chat_manager().add_chat_message(std::time(nullptr), message.value()["sender"], message.value()["side"],
message.value()["message"], events::chat_handler::MESSAGE_PUBLIC,
preferences::message_bell());
}
else if (const config &whisper = cfg.child("whisper") /*&& is_observer()*/)

View File

@ -454,16 +454,16 @@ config& replay::get_last_real_command()
*/
static bool fix_rename_command(const config& c, config& async_child)
{
if (const config &child = c.child("move"))
if (const auto child = c.optional_child("move"))
{
// A unit's move is being undone.
// Repair unsynced cmds whose locations depend on that unit's location.
std::vector<map_location> steps;
try {
read_locations(child,steps);
read_locations(child.value(), steps);
} catch(const bad_lexical_cast &) {
WRN_REPLAY << "Warning: Path data contained something which could not be parsed to a sequence of locations:" << "\n config = " << child.debug() << std::endl;
WRN_REPLAY << "Warning: Path data contained something which could not be parsed to a sequence of locations:" << "\n config = " << child->debug() << std::endl;
}
if (steps.empty()) {
@ -478,12 +478,15 @@ static bool fix_rename_command(const config& c, config& async_child)
}
else
{
const config *chld = &c.child("recruit");
if (!*chld) chld = &c.child("recall");
if (*chld) {
auto loc = c.optional_child("recruit");
if(!loc) {
loc = c.optional_child("recall");
}
if(loc) {
// A unit is being un-recruited or un-recalled.
// Remove unsynced commands that would act on that unit.
map_location src(*chld);
map_location src(loc.value());
map_location aloc(async_child);
if (src == aloc) {
return true;
@ -538,9 +541,9 @@ void replay::undo_cut(config& dst)
}
else if(cc["async"].to_bool(false))
{
if(config& rename = c.child("rename"))
if(auto rename = c.optional_child("rename"))
{
if(fix_rename_command(command(cmd_index), rename))
if(fix_rename_command(command(cmd_index), rename.value()))
{
//remove the command from the replay if fix_rename_command requested it.
remove_command(i);

View File

@ -91,10 +91,10 @@ replay_controller::~replay_controller()
void replay_controller::add_replay_theme()
{
const config &theme_cfg = controller_.get_theme(game_config_manager::get()->game_config(), controller_.theme());
if (const config &res = theme_cfg.child("resolution"))
if (const auto res = theme_cfg.optional_child("resolution"))
{
if (const config &replay_theme_cfg = res.child("replay")) {
controller_.get_display().get_theme().modify(replay_theme_cfg);
if (const auto replay_theme_cfg = res->optional_child("replay")) {
controller_.get_display().get_theme().modify(replay_theme_cfg.value());
}
//Make sure we get notified if the theme is redrawn completely. That way we have
//a chance to restore the replay controls of the theme as well.

View File

@ -95,9 +95,9 @@ config& replay_recorder_base::insert_command(int index)
void replay_recorder_base::append_config(const config& data)
{
if(const config& upload_log = data.child("upload_log"))
if(const auto upload_log = data.optional_child("upload_log"))
{
upload_log_ = upload_log;
upload_log_ = upload_log.value();
}
for(const config& command : data.child_range("command"))
{
@ -107,9 +107,9 @@ void replay_recorder_base::append_config(const config& data)
void replay_recorder_base::append_config(config& data)
{
if(config& upload_log = data.child("upload_log"))
if(auto upload_log = data.optional_child("upload_log"))
{
upload_log_.swap(upload_log);
upload_log_.swap(upload_log.value());
}
for(config& command : data.child_range("command"))
{

View File

@ -451,45 +451,45 @@ void stats::write(config_writer &out) const
void stats::read(const config& cfg)
{
if (const config &c = cfg.child("recruits")) {
recruits = read_str_int_map(c);
if (const auto c = cfg.optional_child("recruits")) {
recruits = read_str_int_map(c.value());
}
if (const config &c = cfg.child("recalls")) {
recalls = read_str_int_map(c);
if (const auto c = cfg.optional_child("recalls")) {
recalls = read_str_int_map(c.value());
}
if (const config &c = cfg.child("advances")) {
advanced_to = read_str_int_map(c);
if (const auto c = cfg.optional_child("advances")) {
advanced_to = read_str_int_map(c.value());
}
if (const config &c = cfg.child("deaths")) {
deaths = read_str_int_map(c);
if (const auto c = cfg.optional_child("deaths")) {
deaths = read_str_int_map(c.value());
}
if (const config &c = cfg.child("killed")) {
killed = read_str_int_map(c);
if (const auto c = cfg.optional_child("killed")) {
killed = read_str_int_map(c.value());
}
if (const config &c = cfg.child("recalls")) {
recalls = read_str_int_map(c);
if (const auto c = cfg.optional_child("recalls")) {
recalls = read_str_int_map(c.value());
}
if (const config &c = cfg.child("attacks")) {
attacks_inflicted = read_battle_result_map(c);
if (const auto c = cfg.optional_child("attacks")) {
attacks_inflicted = read_battle_result_map(c.value());
}
if (const config &c = cfg.child("defends")) {
defends_inflicted = read_battle_result_map(c);
if (const auto c = cfg.optional_child("defends")) {
defends_inflicted = read_battle_result_map(c.value());
}
if (const config &c = cfg.child("attacks_taken")) {
attacks_taken = read_battle_result_map(c);
if (const auto c = cfg.optional_child("attacks_taken")) {
attacks_taken = read_battle_result_map(c.value());
}
if (const config &c = cfg.child("defends_taken")) {
defends_taken = read_battle_result_map(c);
if (const auto c = cfg.optional_child("defends_taken")) {
defends_taken = read_battle_result_map(c.value());
}
by_cth_inflicted = read_by_cth_map_from_battle_result_maps(attacks_inflicted, defends_inflicted);
// by_cth_taken will be an empty map in old (pre-#4070) savefiles that don't have
// [attacks_taken]/[defends_taken] tags in their [statistics] tags
by_cth_taken = read_by_cth_map_from_battle_result_maps(attacks_taken, defends_taken);
if (const config &c = cfg.child("turn_by_cth_inflicted")) {
turn_by_cth_inflicted = read_by_cth_map(c);
if (const auto c = cfg.optional_child("turn_by_cth_inflicted")) {
turn_by_cth_inflicted = read_by_cth_map(c.value());
}
if (const config &c = cfg.child("turn_by_cth_taken")) {
turn_by_cth_taken = read_by_cth_map(c);
if (const auto c = cfg.optional_child("turn_by_cth_taken")) {
turn_by_cth_taken = read_by_cth_map(c.value());
}
recruit_cost = cfg["recruit_cost"].to_int();

View File

@ -149,8 +149,8 @@ SYNCED_COMMAND_HANDLER_FUNCTION(recall, child, use_undo, show, error_handler)
SYNCED_COMMAND_HANDLER_FUNCTION(attack, child, /*use_undo*/, show, error_handler)
{
const config &destination = child.child("destination");
const config &source = child.child("source");
const auto destination = child.optional_child("destination");
const auto source = child.optional_child("source");
//check_checksums(*cfg);
if (!destination) {
@ -165,8 +165,8 @@ SYNCED_COMMAND_HANDLER_FUNCTION(attack, child, /*use_undo*/, show, error_handler
//we must get locations by value instead of by references, because the iterators
//may become invalidated later
const map_location src(source, resources::gamedata);
const map_location dst(destination, resources::gamedata);
const map_location src(source.value(), resources::gamedata);
const map_location dst(destination.value(), resources::gamedata);
int weapon_num = child["weapon"];
// having defender_weapon in the replay fixes a bug (OOS) where one player (or observer) chooses a different defensive weapon.
@ -322,14 +322,14 @@ SYNCED_COMMAND_HANDLER_FUNCTION(fire_event, child, use_undo, /*show*/, /*error_
{
bool undoable = true;
if(const config &last_select = child.child("last_select"))
if(const auto last_select = child.optional_child("last_select"))
{
//the select event cannot clear the undo stack.
resources::game_events->pump().fire("select", map_location(last_select, resources::gamedata));
resources::game_events->pump().fire("select", map_location(last_select.value(), resources::gamedata));
}
const std::string &event_name = child["raise"];
if (const config &source = child.child("source")) {
undoable = undoable & !std::get<0>(resources::game_events->pump().fire(event_name, map_location(source, resources::gamedata)));
if (const auto source = child.optional_child("source")) {
undoable = undoable & !std::get<0>(resources::game_events->pump().fire(event_name, map_location(source.value(), resources::gamedata)));
} else {
undoable = undoable & !std::get<0>(resources::game_events->pump().fire(event_name));
}

View File

@ -675,24 +675,24 @@ bool theme::set_resolution(const SDL_Rect& screen)
void theme::add_object(std::size_t sw, std::size_t sh, const config& cfg)
{
if(const config& c = cfg.child("main_map")) {
main_map_ = object(sw, sh, c);
if(const auto c = cfg.optional_child("main_map")) {
main_map_ = object(sw, sh, c.value());
}
if(const config& c = cfg.child("mini_map")) {
mini_map_ = object(sw, sh, c);
if(const auto c = cfg.optional_child("mini_map")) {
mini_map_ = object(sw, sh, c.value());
}
if(const config& c = cfg.child("palette")) {
palette_ = object(sw, sh, c);
if(const auto c = cfg.optional_child("palette")) {
palette_ = object(sw, sh, c.value());
}
if(const config& status_cfg = cfg.child("status")) {
for(const config::any_child i : status_cfg.all_children_range()) {
if(const auto status_cfg = cfg.optional_child("status")) {
for(const config::any_child i : status_cfg->all_children_range()) {
status_[i.key].reset(new status_item(sw, sh, i.cfg));
}
if(const config& unit_image_cfg = status_cfg.child("unit_image")) {
unit_image_ = object(sw, sh, unit_image_cfg);
if(const auto unit_image_cfg = status_cfg->optional_child("unit_image")) {
unit_image_ = object(sw, sh, unit_image_cfg.value());
} else {
unit_image_ = object();
}