Added a sized_array alias to string enums

This commit is contained in:
Charles Dang 2022-03-22 10:07:47 -04:00
parent 2e869ce204
commit 3435cf7ab9
4 changed files with 17 additions and 13 deletions

View File

@ -82,9 +82,13 @@ struct enum_base : public T
}
};
#define ENUM_AND_ARRAY(...) \
static constexpr std::size_t count = std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value; \
enum class type { __VA_ARGS__ }; \
static constexpr std::array<const char*, count> values{ __VA_ARGS__ };
#define ENUM_AND_ARRAY(...) \
enum class type { __VA_ARGS__ }; \
\
/** Provide a variable template for an array of matching size. */ \
template<typename T> \
using sized_array = std::array<T, std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value>; \
\
static constexpr sized_array<const char*> values{__VA_ARGS__};
} // namespace string_enums

View File

@ -67,7 +67,7 @@ unit_const_ptr game_stats::get_leader(const int side)
static std::string controller_name(const team& t)
{
static const std::array<t_string, side_controller::size()> names {{_("controller^Idle"), _("controller^Human"), _("controller^AI"), _("controller^Reserved")}};
static const side_controller::sized_array<t_string> names {_("controller^Idle"), _("controller^Human"), _("controller^AI"), _("controller^Reserved")};
return "<span color='#808080'><small>" + names[static_cast<int>(t.controller())] + "</small></span>";
}

View File

@ -253,12 +253,12 @@ void mp_create_game::pre_show(window& win)
//
// Set up random faction mode menu_button
//
static const std::array<t_string, random_faction_mode::size()> names {{_("Independent"), _("No Mirror"), _("No Ally Mirror")}};
static const std::array<t_string, random_faction_mode::size()> tooltips {{
static const random_faction_mode::sized_array<t_string> names {_("Independent"), _("No Mirror"), _("No Ally Mirror")};
static const random_faction_mode::sized_array<t_string> tooltips {
_("Independent: Random factions assigned independently"),
_("No Mirror: No two players will get the same faction"),
_("No Ally Mirror: No two allied players will get the same faction")
}};
};
std::vector<config> rfm_options;
for(std::size_t i = 0; i < random_faction_mode::size(); i++) {
rfm_options.emplace_back("label", names[i]);

View File

@ -839,8 +839,8 @@ bool unit_type::resistance_filter_matches(
std::string unit_type::alignment_description(unit_alignments::type align, unit_race::GENDER gender)
{
static const std::array<t_string, unit_alignments::size()> male_names {{_("lawful"), _("neutral"), _("chaotic"), _("liminal")}};
static const std::array<t_string, unit_alignments::size()> female_names {{_("female^lawful"), _("female^neutral"), _("female^chaotic"), _("female^liminal")}};
static const unit_alignments::sized_array<t_string> male_names {_("lawful"), _("neutral"), _("chaotic"), _("liminal")};
static const unit_alignments::sized_array<t_string> female_names {_("female^lawful"), _("female^neutral"), _("female^chaotic"), _("female^liminal")};
if(gender == unit_race::FEMALE) {
return female_names[static_cast<int>(align)];
@ -926,7 +926,7 @@ void patch_movetype(movetype& mt,
// These three need to follow movetype's fallback system, where values for
// movement costs are used for vision too.
const auto fallback_children = std::array<std::string, 3>{{"movement_costs", "vision_costs", "jamming_costs"}};
const std::array fallback_children {"movement_costs", "vision_costs", "jamming_costs"};
config cumulative_values;
for(const auto& x : fallback_children) {
if(mt_cfg.has_child(x)) {
@ -943,7 +943,7 @@ void patch_movetype(movetype& mt,
}
// These don't need the fallback system
const auto child_names = std::array<std::string, 2>{{"defense", "resistance"}};
const std::array child_names {"defense", "resistance"};
for(const auto& x : child_names) {
if(mt_cfg.has_child(x)) {
const auto& subtag = mt_cfg.child(x);
@ -1143,7 +1143,7 @@ void unit_type_data::set_config(const game_config_view& cfg)
std::string alias;
int default_val;
};
const std::array<ter_defs_to_movetype, 4> terrain_info_tags{
const std::array terrain_info_tags{
ter_defs_to_movetype{{"movement_costs"}, {"movement"}, movetype::UNREACHABLE},
ter_defs_to_movetype{{"vision_costs"}, {"vision"}, movetype::UNREACHABLE},
ter_defs_to_movetype{{"jamming_costs"}, {"jamming"}, movetype::UNREACHABLE},