Skip to content

Commit

Permalink
Convert a bunch of C-style arrays to std::array
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Jun 25, 2018
1 parent fbcdec7 commit b40c666
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 45 deletions.
7 changes: 4 additions & 3 deletions src/addon/validation.cpp
Expand Up @@ -18,17 +18,18 @@
#include "serialization/unicode_cast.hpp"

#include <algorithm>
#include <array>
#include <boost/algorithm/string.hpp>
#include <set>

const unsigned short default_campaignd_port = 15014;

namespace {
const std::string addon_type_strings[] {
const std::array<std::string, ADDON_TYPES_COUNT> addon_type_strings {{
"unknown", "core", "campaign", "scenario", "campaign_sp_mp", "campaign_mp",
"scenario_mp", "map_pack", "era", "faction", "mod_mp", /*"gui", */ "media",
"other", ""
};
"other"
}};

// Reserved DOS device names on Windows XP and later.
const std::set<std::string> dos_device_names = {
Expand Down
4 changes: 3 additions & 1 deletion src/commandline_options.cpp
Expand Up @@ -28,6 +28,8 @@
#include <boost/program_options/positional_options.hpp>
#include <boost/program_options/value_semantic.hpp> // for value, etc
#include <boost/program_options/variables_map.hpp> // for variables_map, etc

#include <array>
#include <iostream> // for operator<<, basic_ostream, etc

namespace po = boost::program_options;
Expand Down Expand Up @@ -502,7 +504,7 @@ void commandline_options::parse_log_domains_(const std::string &domains_string,
}

void commandline_options::parse_log_strictness (const std::string & severity ) {
static lg::logger const *loggers[] { &lg::err(), &lg::warn(), &lg::info(), &lg::debug() };
static const std::array<lg::logger const*, 4> loggers {{&lg::err(), &lg::warn(), &lg::info(), &lg::debug()}};
for (const lg::logger * l : loggers ) {
if (severity == l->get_name()) {
lg::set_strict_severity(*l);
Expand Down
9 changes: 5 additions & 4 deletions src/game_initialization/connect_engine.cpp
Expand Up @@ -26,6 +26,7 @@
#include "team.hpp"
#include "wesnothd_connection.hpp"

#include <array>
#include <cstdlib>
#include <ctime>

Expand All @@ -42,15 +43,15 @@ static lg::log_domain log_mp_connect_engine("mp/connect/engine");
static lg::log_domain log_network("network");
#define LOG_NW LOG_STREAM(info, log_network)

static const std::string controller_names[] {
static const std::array<std::string, 5> controller_names {{
"human",
"human",
"ai",
"null",
"reserved"
};
}};

static const std::string attributes_to_trim[] {
static const std::array<std::string, 15> attributes_to_trim {{
"side",
"type",
"gender",
Expand All @@ -66,7 +67,7 @@ static const std::string attributes_to_trim[] {
"income",
"allow_changes",
"faction"
};
}};

namespace ng {

Expand Down
5 changes: 3 additions & 2 deletions src/generators/lua_map_generator.cpp
Expand Up @@ -18,6 +18,7 @@
#include "game_errors.hpp"
#include "scripting/mapgen_lua_kernel.hpp"

#include <array>
#include <string>

lua_map_generator::lua_map_generator(const config & cfg)
Expand All @@ -30,8 +31,8 @@ lua_map_generator::lua_map_generator(const config & cfg)
, generator_data_(cfg)
{
lk_.load_core();
const char* required[] {"id", "config_name", "create_map"};
for (std::string req : required) {
const std::array<std::string, 3> required {{"id", "config_name", "create_map"}};
for(const std::string& req : required) {
if (!cfg.has_attribute(req)) {
if(req == "create_map" && cfg.has_attribute("create_scenario")) {
// One of these is required, but not both
Expand Down
13 changes: 7 additions & 6 deletions src/replay.cpp
Expand Up @@ -39,6 +39,7 @@
#include "whiteboard/manager.hpp"
#include "replay_recorder_base.hpp"

#include <array>
#include <set>
#include <map>

Expand Down Expand Up @@ -104,12 +105,12 @@ static void verify(const unit_map& units, const config& cfg) {
u->write(u_cfg);

bool is_ok = true;
static const std::string fields[] {"type","hitpoints","experience","side",""};
for(const std::string* str = fields; str->empty() == false; ++str) {
if (u_cfg[*str] != un[*str]) {
errbuf << "ERROR IN FIELD '" << *str << "' for unit at "
<< loc << " data source: '" << un[*str]
<< "' local: '" << u_cfg[*str] << "'\n";
static const std::array<std::string, 4> fields {{"type","hitpoints","experience","side"}};
for(const std::string& field : fields) {
if (u_cfg[field] != un[field]) {
errbuf << "ERROR IN FIELD '" << field << "' for unit at "
<< loc << " data source: '" << un[field]
<< "' local: '" << u_cfg[field] << "'\n";
is_ok = false;
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/units/types.cpp
Expand Up @@ -34,6 +34,7 @@

#include <boost/regex.hpp>

#include <array>
#include <locale>

static lg::log_domain log_config("config");
Expand Down Expand Up @@ -805,7 +806,7 @@ const config& unit_type::build_unit_cfg() const

// Remove "pure" unit_type attributes (attributes that do not get directly
// copied to units; some do get copied, but under different keys).
static char const* unit_type_attrs[] {
static std::array<std::string, 25> unit_type_attrs {{
"attacks",
"base_ids",
"die_sound",
Expand All @@ -816,7 +817,8 @@ const config& unit_type::build_unit_cfg() const
"hitpoints",
"id",
"ignore_race_traits",
"inherit", "movement",
"inherit",
"movement",
"movement_type",
"name",
"num_traits",
Expand All @@ -830,9 +832,9 @@ const config& unit_type::build_unit_cfg() const
"alignment",
"advances_to",
"do_not_list"
};
}};

for(const char* attr : unit_type_attrs) {
for(const std::string& attr : unit_type_attrs) {
unit_cfg_.remove_attribute(attr);
}

Expand Down Expand Up @@ -1162,7 +1164,7 @@ void unit_type_data::set_config(config& cfg)
const std::string& ter_type = terrain["id"];
config temp_cfg;

static const std::string terrain_info_tags[] {"movement", "vision", "jamming", "defense"};
static const std::array<std::string, 4> terrain_info_tags {{"movement", "vision", "jamming", "defense"}};

for(const std::string& tag : terrain_info_tags) {
if(!terrain.has_child(tag)) {
Expand Down
46 changes: 22 additions & 24 deletions src/units/unit.cpp
Expand Up @@ -64,6 +64,7 @@
#pragma warning (pop)
#endif

#include <array>
#include <cassert> // for assert
#include <cstdlib> // for rand
#include <exception> // for exception
Expand Down Expand Up @@ -92,7 +93,7 @@ namespace
static std::vector<const unit*> units_with_cache;

static const std::string leader_crown_path = "misc/leader-crown.png";
static std::string internalized_attrs[] {
static std::array<std::string, 60> internalized_attrs {{
"type",
"id",
"name",
Expand Down Expand Up @@ -154,7 +155,7 @@ namespace
"language_name",
"image",
"image_icon"
};
}};

struct internalized_attrs_sorter
{
Expand All @@ -172,8 +173,8 @@ namespace
config::const_attribute_iterator cur = cfg.begin();
config::const_attribute_iterator end = cfg.end();

const std::string* cur_known = std::begin(internalized_attrs);
const std::string* end_known = std::end(internalized_attrs);
auto cur_known = internalized_attrs.begin();
auto end_known = internalized_attrs.end();

while(cur_known != end_known) {
if(cur == end) {
Expand Down Expand Up @@ -959,7 +960,7 @@ void unit::advance_to(const unit_type& u_type, bool use_traits)
max_attacks_ = new_type.max_attacks();

flag_rgb_ = new_type.flag_rgb();

upkeep_ = upkeep_full();
parse_upkeep(new_type.get_cfg()["upkeep"]);

Expand Down Expand Up @@ -2553,7 +2554,7 @@ std::string get_checksum(const unit& u)
config wcfg;
u.write(unit_config);

const std::string main_keys[] {
const std::array<std::string, 22> main_keys {{
"advances_to",
"alignment",
"cost",
Expand All @@ -2575,28 +2576,26 @@ std::string get_checksum(const unit& u)
"resting",
"undead_variation",
"upkeep",
"zoc",
""
};
"zoc"
}};

for(int i = 0; !main_keys[i].empty(); ++i) {
wcfg[main_keys[i]] = unit_config[main_keys[i]];
for(const std::string& main_key : main_keys) {
wcfg[main_key] = unit_config[main_key];
}

const std::string attack_keys[] {
const std::array<std::string, 5> attack_keys {{
"name",
"type",
"range",
"damage",
"number",
""
};
"number"
}};

for(const config& att : unit_config.child_range("attack")) {
config& child = wcfg.add_child("attack");

for(int i = 0; !attack_keys[i].empty(); ++i) {
child[attack_keys[i]] = att[attack_keys[i]];
for(const std::string& attack_key : attack_keys) {
child[attack_key] = att[attack_key];
}

for(const config& spec : att.child_range("specials")) {
Expand Down Expand Up @@ -2624,19 +2623,18 @@ std::string get_checksum(const unit& u)
child.recursive_clear_value("name");
}

const std::string child_keys[] {
const std::array<std::string, 6> child_keys {{
"advance_from",
"defense",
"movement_costs",
"vision_costs",
"jamming_costs",
"resistance",
""
};
"resistance"
}};

for(int i = 0; !child_keys[i].empty(); ++i) {
for(const config& c : unit_config.child_range(child_keys[i])) {
wcfg.add_child(child_keys[i], c);
for(const std::string& child_key : child_keys) {
for(const config& c : unit_config.child_range(child_key)) {
wcfg.add_child(child_key, c);
}
}

Expand Down

0 comments on commit b40c666

Please sign in to comment.