From 2a6ca6cd9e8620b433e9b7ecd8892ca52c926814 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Fri, 28 Apr 2017 17:15:13 +1100 Subject: [PATCH] Deployed more emplace_back (how do I keep missing these...) Covers more cases of: * push_back(std::pair * push_back(std::make_pair * push_back(std::make_tuple * push_back(T) where T is an empty object of type T Small thing I noticed: this does mean the numbers in font::subset_descriptor are no longer 'cast' to size_t before being added to the vector, but that shouldn't matter (hopefully). --- src/ai/configuration.cpp | 6 ++--- src/ai/default/aspect_attacks.cpp | 2 +- src/ai/default/ca.cpp | 23 +++++++++-------- src/ai/default/recruitment.cpp | 4 +-- src/commandline_options.cpp | 4 +-- src/configr_assign.hpp | 2 +- src/font/font_description.hpp | 4 +-- src/game_initialization/connect_engine.cpp | 2 +- src/help/help_impl.cpp | 2 +- src/help/help_topic_generators.cpp | 26 ++++++++------------ src/reports.cpp | 5 ++-- src/scripting/application_lua_kernel.cpp | 2 +- src/serialization/preprocessor.cpp | 2 +- src/terrain/builder.cpp | 2 +- src/units/abilities.cpp | 13 +++++----- src/units/types.cpp | 4 +-- src/utils/context_free_grammar_generator.cpp | 4 +-- src/video.cpp | 2 +- src/wesnoth.cpp | 2 +- 19 files changed, 51 insertions(+), 60 deletions(-) diff --git a/src/ai/configuration.cpp b/src/ai/configuration.cpp index 040d84e4d44..eac6a814b4e 100644 --- a/src/ai/configuration.cpp +++ b/src/ai/configuration.cpp @@ -282,7 +282,7 @@ void configuration::expand_simplified_aspects(side_number side, config &cfg) { facet_config["turns"] = turns; facet_config["time_of_day"] = time_of_day; facet_config["value"] = attr.second; - facet_configs.push_back(std::make_pair(attr.first, facet_config)); + facet_configs.emplace_back(attr.first, facet_config); } for (const config::any_child &child : aiparam.all_children_range()) { if (just_copy_tags.count(child.key)) { @@ -310,7 +310,7 @@ void configuration::expand_simplified_aspects(side_number side, config &cfg) { // then it can be copied verbatim as a [facet] tag. // Otherwise, it needs to be placed as a [value] within a [facet] tag. if (child.key == "attacks" || child.cfg.has_attribute("value") || child.cfg.has_child("value")) { - facet_configs.push_back(std::make_pair(child.key, child.cfg)); + facet_configs.emplace_back(child.key, child.cfg); } else { config facet_config; facet_config["engine"] = engine; @@ -325,7 +325,7 @@ void configuration::expand_simplified_aspects(side_number side, config &cfg) { facet_config["id"] = child.cfg["id"]; } } - facet_configs.push_back(std::make_pair(child.key, facet_config)); + facet_configs.emplace_back(child.key, facet_config); } } std::map aspect_configs; diff --git a/src/ai/default/aspect_attacks.cpp b/src/ai/default/aspect_attacks.cpp index 232a799db0d..126ec8c99fe 100644 --- a/src/ai/default/aspect_attacks.cpp +++ b/src/ai/default/aspect_attacks.cpp @@ -322,7 +322,7 @@ void aspect_attacks_base::do_attack_analysis( if(cur_position != -1) { units.erase(units.begin() + i); - cur_analysis.movements.push_back(std::pair(current_unit,tiles[cur_position])); + cur_analysis.movements.emplace_back(current_unit,tiles[cur_position]); cur_analysis.vulnerability += best_vulnerability; diff --git a/src/ai/default/ca.cpp b/src/ai/default/ca.cpp index d5068634ee7..2e6a9c26a44 100644 --- a/src/ai/default/ca.cpp +++ b/src/ai/default/ca.cpp @@ -752,7 +752,7 @@ void get_villages_phase::find_villages( move_result_ptr move_check_res = check_move_action(j->second,j->first,true); if (move_check_res->is_ok()) { DBG_AI_TESTING_AI_DEFAULT << "Dispatched unit at " << j->second << " to village " << j->first << '\n'; - moves.push_back(std::make_pair(j->first, j->second)); + moves.emplace_back(j->first, j->second); } reachmap.erase(j->second); dispatched_units.push_back(j->second); @@ -837,7 +837,7 @@ bool get_villages_phase::dispatch_unit_simple(treachmap& reachmap, tmoves& moves result = true; DBG_AI_TESTING_AI_DEFAULT << "Dispatched unit at " << itor->first << " to village " << village << '\n'; - moves.push_back(std::make_pair(village, itor->first)); + moves.emplace_back(village, itor->first); reachmap.erase(itor++); if(remove_village(reachmap, moves, village)) { @@ -860,8 +860,7 @@ bool get_villages_phase::dispatch_unit_simple(treachmap& reachmap, tmoves& moves DBG_AI_TESTING_AI_DEFAULT << "Dispatched _last_ unit at " << reachmap.begin()->first << " to village " << reachmap.begin()->second[0] << '\n'; - moves.push_back(std::make_pair( - reachmap.begin()->second[0], reachmap.begin()->first)); + moves.emplace_back(reachmap.begin()->second[0], reachmap.begin()->first); reachmap.clear(); // We're done. @@ -907,7 +906,7 @@ bool get_villages_phase::dispatch_village_simple( result = true; DBG_AI_TESTING_AI_DEFAULT << "Dispatched unit at " << itor->second[0] << " to village " << itor->first << '\n'; - moves.push_back(std::make_pair(itor->first, itor->second[0])); + moves.emplace_back(itor->first, itor->second[0]); reachmap.erase(itor->second[0]); remove_village(reachmap, moves, village); @@ -949,7 +948,7 @@ get_villages_phase::treachmap::iterator get_villages_phase::remove_unit( DBG_AI_TESTING_AI_DEFAULT << "Dispatch leader at " << leader_loc_ << " closer to the keep at " << best_leader_loc_ << '\n'; - moves.push_back(std::make_pair(best_leader_loc_, leader_loc_)); + moves.emplace_back(best_leader_loc_, leader_loc_); } reachmap.erase(unit++); @@ -1092,11 +1091,11 @@ void get_villages_phase::dispatch_complex( // Dispatch DBG_AI_TESTING_AI_DEFAULT << "Found a square.\nDispatched unit at " << units[src_itor->second] << " to village " << village1 << '\n'; - moves.push_back(std::make_pair(village1, units[src_itor->second])); + moves.emplace_back(village1, units[src_itor->second]); DBG_AI_TESTING_AI_DEFAULT << "Dispatched unit at " << units[dst_itor->second] << " to village " << village2 << '\n'; - moves.push_back(std::make_pair(village2, units[dst_itor->second])); + moves.emplace_back(village2, units[dst_itor->second]); // Remove the units reachmap.erase(units[src_itor->second]); @@ -1157,7 +1156,7 @@ void get_villages_phase::dispatch_complex( std::vector > result; for(size_t u = 0; u < max_options; ++u) { if(matrix[u][perm[u]]) { - result.push_back(std::make_pair(villages[perm[u]], units[u])); + result.emplace_back(villages[perm[u]], units[u]); } } @@ -1195,7 +1194,7 @@ void get_villages_phase::dispatch_complex( std::vector > result; for(size_t u = 0; u < unit_count; ++u) { if(matrix[u][perm[u]]) { - result.push_back(std::make_pair(villages[perm[u]], units[u])); + result.emplace_back(villages[perm[u]], units[u]); } } @@ -1237,7 +1236,7 @@ void get_villages_phase::dispatch_complex( std::vector > result; for(size_t v = 0; v < village_count; ++v) { if(matrix[perm[v]][v]) { - result.push_back(std::make_pair(villages[v], units[perm[v]])); + result.emplace_back(villages[v], units[perm[v]]); } } @@ -1274,7 +1273,7 @@ void get_villages_phase::full_dispatch(treachmap& reachmap, tmoves& moves) for(size_t i = 0; i < reachmap.size(); ++i, ++itor) { DBG_AI_TESTING_AI_DEFAULT << "Dispatched unit at " << itor->first << " to village " << itor->second[i] << '\n'; - moves.push_back(std::make_pair(itor->second[i], itor->first)); + moves.emplace_back(itor->second[i], itor->first); } } diff --git a/src/ai/default/recruitment.cpp b/src/ai/default/recruitment.cpp index 98a45d9d817..db4bfbc239f 100644 --- a/src/ai/default/recruitment.cpp +++ b/src/ai/default/recruitment.cpp @@ -928,7 +928,7 @@ void recruitment::do_combat_analysis(std::vector* leader_data) { if (!current_team().is_enemy(unit.side()) || unit.incapacitated()) { continue; } - enemy_units.push_back(std::make_pair(unit.type_id(), unit.hitpoints())); + enemy_units.emplace_back(unit.type_id(), unit.hitpoints()); } if (enemy_units.size() < UNIT_THRESHOLD) { // Use also enemies recruitment lists and insert units into enemy_units. @@ -949,7 +949,7 @@ void recruitment::do_combat_analysis(std::vector* leader_data) { const unit_type* recruit_type = unit_types.find(possible_recruit); if (recruit_type) { int hp = recruit_type->hitpoints(); - enemy_units.push_back(std::make_pair(possible_recruit, hp)); + enemy_units.emplace_back(possible_recruit, hp); } } } diff --git a/src/commandline_options.cpp b/src/commandline_options.cpp index 84528467f93..9e16864be35 100644 --- a/src/commandline_options.cpp +++ b/src/commandline_options.cpp @@ -479,7 +479,7 @@ void commandline_options::parse_log_domains_(const std::string &domains_string, { if (!log) log = std::vector >(); - log->push_back(std::make_pair(severity, domain)); + log->emplace_back(severity, domain); } } @@ -534,7 +534,7 @@ std::vector > commandline_options::parse_to_ throw bad_commandline_tuple(s, expected_format); } - vec.push_back(std::make_pair(temp, tokens[1])); + vec.emplace_back(temp, tokens[1]); } return vec; } diff --git a/src/configr_assign.hpp b/src/configr_assign.hpp index f550735322f..273a51106d1 100644 --- a/src/configr_assign.hpp +++ b/src/configr_assign.hpp @@ -39,7 +39,7 @@ struct configr_of configr_of& operator()(const std::string& tagname, const configr_of& child) { - subtags_.push_back(std::make_pair(&tagname, &child)); + subtags_.emplace_back(&tagname, &child); return *this; } std::vector> subtags_; diff --git a/src/font/font_description.hpp b/src/font/font_description.hpp index 7ac04c5ae66..c96774d5323 100644 --- a/src/font/font_description.hpp +++ b/src/font/font_description.hpp @@ -60,12 +60,12 @@ struct subset_descriptor std::vector r = utils::split(i, '-'); if(r.size() == 1) { size_t r1 = lexical_cast_default(r[0], 0); - present_codepoints.push_back(std::pair(r1, r1)); + present_codepoints.emplace_back(r1, r1); } else if(r.size() == 2) { size_t r1 = lexical_cast_default(r[0], 0); size_t r2 = lexical_cast_default(r[1], 0); - present_codepoints.push_back(std::pair(r1, r2)); + present_codepoints.emplace_back(r1, r2); } } } diff --git a/src/game_initialization/connect_engine.cpp b/src/game_initialization/connect_engine.cpp index a8f05c14fd9..a2a2b94cb7f 100644 --- a/src/game_initialization/connect_engine.cpp +++ b/src/game_initialization/connect_engine.cpp @@ -1376,7 +1376,7 @@ void side_engine::add_controller_option(ng::controller controller, return; } - controller_options_.push_back(std::make_pair(controller, name)); + controller_options_.emplace_back(controller, name); } } // end namespace ng diff --git a/src/help/help_impl.cpp b/src/help/help_impl.cpp index 1af6f48e5bf..6bf1071c1bd 100644 --- a/src/help/help_impl.cpp +++ b/src/help/help_impl.cpp @@ -1488,7 +1488,7 @@ unsigned image_width(const std::string &filename) void push_tab_pair(std::vector > &v, const std::string &s) { - v.push_back(std::make_pair(s, font::line_width(s, normal_font_size))); + v.emplace_back(s, font::line_width(s, normal_font_size)); } std::string generate_table(const table_spec &tab, const unsigned int spacing) diff --git a/src/help/help_topic_generators.cpp b/src/help/help_topic_generators.cpp index 0e0a297ba2c..ba8db63c862 100644 --- a/src/help/help_topic_generators.cpp +++ b/src/help/help_topic_generators.cpp @@ -401,7 +401,7 @@ std::string unit_topic_generator::operator()() const { continue; } const std::string ref_id = "traits_"+trait["id"].str(); - ((trait["availability"].str() == "musthave") ? must_have_traits : random_traits).push_back(std::make_pair(lang_trait_name, ref_id)); + ((trait["availability"].str() == "musthave") ? must_have_traits : random_traits).emplace_back(lang_trait_name, ref_id); } bool line1 = !must_have_traits.empty(); @@ -515,7 +515,7 @@ std::string unit_topic_generator::operator()() const { std::vector row; std::stringstream attack_ss; attack_ss << "src='" << attack.icon() << "'"; - row.push_back(std::make_pair(attack_ss.str(),image_width(attack.icon()))); + row.emplace_back(attack_ss.str(),image_width(attack.icon())); push_tab_pair(row, lang_weapon); push_tab_pair(row, lang_type); attack_ss.str(clear_stringstream); @@ -544,8 +544,7 @@ std::string unit_topic_generator::operator()() const { attack_ss << ", "; //comma placed before next special } } - row.push_back(std::make_pair(attack_ss.str(), - font::line_width(lang_special, normal_font_size))); + row.emplace_back(attack_ss.str(), font::line_width(lang_special, normal_font_size)); } table.push_back(row); } @@ -596,8 +595,7 @@ std::string unit_topic_generator::operator()() const { const std::string markup = str.str(); str.str(clear_stringstream); str << resist; - row.push_back(std::make_pair(markup, - font::line_width(str.str(), normal_font_size))); + row.emplace_back(markup, font::line_width(str.str(), normal_font_size)); resistance_table.push_back(row); } ss << generate_table(resistance_table); @@ -652,9 +650,9 @@ std::string unit_topic_generator::operator()() const { const std::string final_image = tc_base + "~RC(magenta>" + id + ")~BLIT(" + terrain_image + ")"; - row.push_back(std::make_pair( "src='" + final_image + "' " + + row.emplace_back("src='" + final_image + "' " + make_link(name, "..terrain_" + id), - font::line_width(name, normal_font_size) + (high_res ? 32 : 16) )); + font::line_width(name, normal_font_size) + (high_res ? 32 : 16) ); //defense - range: +10 % .. +70 % const int defense = 100 - movement_type.defense_modifier(terrain); @@ -674,8 +672,7 @@ std::string unit_topic_generator::operator()() const { std::string markup = str.str(); str.str(clear_stringstream); str << defense << "%"; - row.push_back(std::make_pair(markup, - font::line_width(str.str(), normal_font_size))); + row.emplace_back(markup, font::line_width(str.str(), normal_font_size)); //movement - range: 1 .. 5, movetype::UNREACHABLE=impassable str.str(clear_stringstream); @@ -699,8 +696,7 @@ std::string unit_topic_generator::operator()() const { markup = str.str(); str.str(clear_stringstream); str << moves; - row.push_back(std::make_pair(markup, - font::line_width(str.str(), normal_font_size))); + row.emplace_back(markup, font::line_width(str.str(), normal_font_size)); //defense cap if (has_terrain_defense_caps) { @@ -718,8 +714,7 @@ std::string unit_topic_generator::operator()() const { } else { str << font::unicode_figure_dash; } - row.push_back(std::make_pair(markup, - font::line_width(str.str(), normal_font_size))); + row.emplace_back(markup, font::line_width(str.str(), normal_font_size)); } //vision @@ -747,8 +742,7 @@ std::string unit_topic_generator::operator()() const { markup = str.str(); str.str(clear_stringstream); str << views; - row.push_back(std::make_pair(markup, - font::line_width(str.str(), normal_font_size))); + row.emplace_back(markup, font::line_width(str.str(), normal_font_size)); } //jamming diff --git a/src/reports.cpp b/src/reports.cpp index 56acd0f0150..2323c7c1515 100644 --- a/src/reports.cpp +++ b/src/reports.cpp @@ -919,7 +919,7 @@ static config unit_weapons(reports::context & rc, const unit *attacker, const ma // We keep only values above 0.1%. if(prob > 0.001) - prob_hp_vector.push_back(std::pair(prob, i)); + prob_hp_vector.emplace_back(prob, i); } std::sort(prob_hp_vector.begin(), prob_hp_vector.end()); @@ -933,8 +933,7 @@ static config unit_weapons(reports::context & rc, const unit *attacker, const ma for(i = prob_hp_vector.size() - nb_elem; i < static_cast(prob_hp_vector.size()); i++) { - hp_prob_vector.push_back(std::pair - (prob_hp_vector[i].second, prob_hp_vector[i].first)); + hp_prob_vector.emplace_back(prob_hp_vector[i].second, prob_hp_vector[i].first); } // Then, we sort the hitpoint values in ascending order. diff --git a/src/scripting/application_lua_kernel.cpp b/src/scripting/application_lua_kernel.cpp index 7420c09740f..23192e91537 100644 --- a/src/scripting/application_lua_kernel.cpp +++ b/src/scripting/application_lua_kernel.cpp @@ -335,7 +335,7 @@ application_lua_kernel::request_list application_lua_kernel::thread::run_script( for (const plugins_manager::event & req : this_context_backend->requests) { results.push_back(std::bind(ctxt.callbacks_.find(req.name)->second, req.data)); - //results.push_back(std::make_pair(ctxt.callbacks_.find(req.name)->second, req.data)); + //results.emplace_back(ctxt.callbacks_.find(req.name)->second, req.data); } return results; } diff --git a/src/serialization/preprocessor.cpp b/src/serialization/preprocessor.cpp index 2d86b1d3108..b441a61f027 100644 --- a/src/serialization/preprocessor.cpp +++ b/src/serialization/preprocessor.cpp @@ -692,7 +692,7 @@ void preprocessor_data::pop_token() prepare for it. */ if (inner_type == token_desc::MACRO_SPACE || inner_type == token_desc::MACRO_CHUNK) { strings_.erase(strings_.begin() + stack_pos, strings_.end()); - strings_.push_back(std::string()); + strings_.emplace_back(); } assert(stack_pos + 1 == strings_.size()); outer_type = token_desc::MACRO_CHUNK; diff --git a/src/terrain/builder.cpp b/src/terrain/builder.cpp index de6769d0060..0fd73ad0790 100644 --- a/src/terrain/builder.cpp +++ b/src/terrain/builder.cpp @@ -170,7 +170,7 @@ void terrain_builder::tile::rebuild_cache(const std::string& tod, logs* log) } if(log) { - log->push_back(std::make_pair(&ri, &variant)); + log->emplace_back(&ri, &variant); } break; // found a matching variant diff --git a/src/units/abilities.cpp b/src/units/abilities.cpp index a006f9cc016..485907fe893 100644 --- a/src/units/abilities.cpp +++ b/src/units/abilities.cpp @@ -265,10 +265,10 @@ std::vector > unit::ability_tooltips(bo gender_value(ab.cfg, gender_, "name", "female_name", "name").t_str(); if (!name.empty()) { - res.push_back(std::make_tuple( + res.emplace_back( ab.cfg["name"].t_str(), name, - ab.cfg["description"].t_str() )); + ab.cfg["description"].t_str() ); if ( active_list ) active_list->push_back(true); } @@ -283,10 +283,10 @@ std::vector > unit::ability_tooltips(bo gender_value(ab.cfg, gender_, "name", "female_name", "name").t_str(); if (!name.empty()) { - res.push_back(std::make_tuple( + res.emplace_back( default_value(ab.cfg, "name_inactive", "name").t_str(), name, - default_value(ab.cfg, "description_inactive", "description").t_str() )); + default_value(ab.cfg, "description_inactive", "description").t_str() ); active_list->push_back(false); } } @@ -615,15 +615,14 @@ std::vector > attack_type::special_tooltips( if ( !active_list || special_active(sp.cfg, AFFECT_EITHER) ) { const t_string &name = sp.cfg["name"]; if (!name.empty()) { - res.push_back(std::make_pair(name, sp.cfg["description"].t_str() )); + res.emplace_back(name, sp.cfg["description"].t_str() ); if ( active_list ) active_list->push_back(true); } } else { t_string const &name = default_value(sp.cfg, "name_inactive", "name").t_str(); if (!name.empty()) { - res.push_back(std::make_pair( - name, default_value(sp.cfg, "description_inactive", "description").t_str() )); + res.emplace_back(name, default_value(sp.cfg, "description_inactive", "description").t_str() ); active_list->push_back(false); } } diff --git a/src/units/types.cpp b/src/units/types.cpp index ca6a5022bae..e3479b7f571 100644 --- a/src/units/types.cpp +++ b/src/units/types.cpp @@ -1218,8 +1218,8 @@ void unit_type_data::read_hide_help(const config& cfg) if (!cfg) return; - hide_help_race_.push_back(std::set()); - hide_help_type_.push_back(std::set()); + hide_help_race_.emplace_back(); + hide_help_type_.emplace_back(); std::vector races = utils::split(cfg["race"]); hide_help_race_.back().insert(races.begin(), races.end()); diff --git a/src/utils/context_free_grammar_generator.cpp b/src/utils/context_free_grammar_generator.cpp index b6d26069da8..7fd3a8a195d 100644 --- a/src/utils/context_free_grammar_generator.cpp +++ b/src/utils/context_free_grammar_generator.cpp @@ -47,7 +47,7 @@ context_free_grammar_generator::context_free_grammar_generator(const std::string throw name_generator_invalid_exception("[context_free_grammar_generator] Parsing error: nonterminals (, ! and ) may not be overridden"); } current = &nonterminals_[key]; - current->possibilities_.push_back(std::vector()); + current->possibilities_.emplace_back(); filled = ¤t->possibilities_.back(); buf.clear(); } else if (*reading == '\n') { @@ -61,7 +61,7 @@ context_free_grammar_generator::context_free_grammar_generator(const std::string throw name_generator_invalid_exception("[context_free_grammar_generator] Parsing error: misplaced | symbol"); } filled->push_back(buf); - current->possibilities_.push_back(std::vector()); + current->possibilities_.emplace_back(); filled = ¤t->possibilities_.back(); buf.clear(); } else if (*reading == '\\' && reading[1] == 'n') { diff --git a/src/video.cpp b/src/video.cpp index 4fb7b14365d..c32979d5b49 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -353,7 +353,7 @@ std::vector> CVideo::get_available_resolutions(const bool in for (int i = 0; i < modes; ++i) { if(SDL_GetDisplayMode(0, i, &mode) == 0) { if(mode.w >= min_res.first && mode.h >= min_res.second) - result.push_back(std::make_pair(mode.w, mode.h)); + result.emplace_back(mode.w, mode.h); } } diff --git a/src/wesnoth.cpp b/src/wesnoth.cpp index c1ca1d1f14a..801e81a6ce7 100644 --- a/src/wesnoth.cpp +++ b/src/wesnoth.cpp @@ -876,7 +876,7 @@ static std::vector parse_commandline_arguments(std::string input) while(parse_commandline_argument(start, end, buffer)) { - res.push_back(std::string()); + res.emplace_back(); res.back().swap(buffer); } return res;