Codechange: use std::array instead of C-style array for produced/accepts cargo

This commit is contained in:
Rubidium 2024-04-08 20:30:32 +02:00 committed by rubidium42
parent 40fa45a76a
commit e441033d68
4 changed files with 18 additions and 18 deletions

View File

@ -1788,7 +1788,7 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type,
i->type = type;
Industry::IncIndustryTypeCount(type);
for (size_t index = 0; index < lengthof(indspec->produced_cargo); ++index) {
for (size_t index = 0; index < std::size(indspec->produced_cargo); ++index) {
if (!IsValidCargoID(indspec->produced_cargo[index])) break;
Industry::ProducedCargo &p = i->produced.emplace_back();
@ -1796,7 +1796,7 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type,
p.rate = indspec->production_rate[index];
}
for (size_t index = 0; index < lengthof(indspec->accepts_cargo); ++index) {
for (size_t index = 0; index < std::size(indspec->accepts_cargo); ++index) {
if (!IsValidCargoID(indspec->accepts_cargo[index])) break;
Industry::AcceptedCargo &a = i->accepted.emplace_back();

View File

@ -155,7 +155,7 @@ enum CargoSuffixInOut {
template <typename TC, typename TS>
static inline void GetAllCargoSuffixes(CargoSuffixInOut use_input, CargoSuffixType cst, const Industry *ind, IndustryType ind_type, const IndustrySpec *indspec, const TC &cargoes, TS &suffixes)
{
static_assert(lengthof(cargoes) <= lengthof(suffixes));
static_assert(std::tuple_size_v<std::remove_reference_t<decltype(cargoes)>> <= lengthof(suffixes));
if (indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED) {
/* Reworked behaviour with new many-in-many-out scheme */
@ -452,7 +452,7 @@ public:
Dimension d = {0, 0};
for (const auto &indtype : this->list) {
const IndustrySpec *indsp = GetIndustrySpec(indtype);
CargoSuffix cargo_suffix[lengthof(indsp->accepts_cargo)];
CargoSuffix cargo_suffix[std::tuple_size_v<decltype(indsp->accepts_cargo)>];
/* Measure the accepted cargoes, if any. */
GetAllCargoSuffixes(CARGOSUFFIX_IN, CST_FUND, nullptr, indtype, indsp, indsp->accepts_cargo, cargo_suffix);
@ -568,7 +568,7 @@ public:
ir.top += GetCharacterHeight(FS_NORMAL);
}
CargoSuffix cargo_suffix[lengthof(indsp->accepts_cargo)];
CargoSuffix cargo_suffix[std::tuple_size_v<decltype(indsp->accepts_cargo)>];
/* Draw the accepted cargoes, if any. Otherwise, will print "Nothing". */
GetAllCargoSuffixes(CARGOSUFFIX_IN, CST_FUND, nullptr, this->selected_type, indsp, indsp->accepts_cargo, cargo_suffix);
@ -2422,7 +2422,7 @@ struct CargoesRow {
int other_count = 0;
const IndustrySpec *indsp = GetIndustrySpec(ind_fld->u.industry.ind_type);
assert(CargoesField::max_cargoes <= lengthof(indsp->produced_cargo));
assert(CargoesField::max_cargoes <= std::size(indsp->produced_cargo));
for (uint i = 0; i < CargoesField::max_cargoes; i++) {
int col = cargo_fld->ConnectCargo(indsp->produced_cargo[i], true);
if (col < 0) others[other_count++] = indsp->produced_cargo[i];
@ -2481,7 +2481,7 @@ struct CargoesRow {
int other_count = 0;
const IndustrySpec *indsp = GetIndustrySpec(ind_fld->u.industry.ind_type);
assert(CargoesField::max_cargoes <= lengthof(indsp->accepts_cargo));
assert(CargoesField::max_cargoes <= std::size(indsp->accepts_cargo));
for (uint i = 0; i < CargoesField::max_cargoes; i++) {
int col = cargo_fld->ConnectCargo(indsp->accepts_cargo[i], false);
if (col < 0) others[other_count++] = indsp->accepts_cargo[i];

View File

@ -109,7 +109,7 @@ struct IndustrySpec {
uint32_t prospecting_chance; ///< Chance prospecting succeeds
IndustryType conflicting[3]; ///< Industries this industry cannot be close to
uint8_t check_proc; ///< Index to a procedure to check for conflicting circumstances
CargoID produced_cargo[INDUSTRY_NUM_OUTPUTS];
std::array<CargoID, INDUSTRY_NUM_OUTPUTS> produced_cargo;
std::variant<CargoLabel, MixedCargoType> produced_cargo_label[INDUSTRY_NUM_OUTPUTS];
uint8_t production_rate[INDUSTRY_NUM_OUTPUTS];
/**
@ -117,7 +117,7 @@ struct IndustrySpec {
* If the waiting cargo is less than this number, no cargo is moved to it.
*/
uint8_t minimal_cargo;
CargoID accepts_cargo[INDUSTRY_NUM_INPUTS]; ///< 16 accepted cargoes.
std::array<CargoID, INDUSTRY_NUM_INPUTS> accepts_cargo; ///< 16 accepted cargoes.
std::variant<CargoLabel, MixedCargoType> accepts_cargo_label[INDUSTRY_NUM_INPUTS];
uint16_t input_cargo_multiplier[INDUSTRY_NUM_INPUTS][INDUSTRY_NUM_OUTPUTS]; ///< Input cargo multipliers (multiply amount of incoming cargo for the produced cargoes)
IndustryLifeType life_type; ///< This is also known as Industry production flag, in newgrf specs

View File

@ -3787,12 +3787,12 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop,
case 0x25: { // variable length produced cargoes
uint8_t num_cargoes = buf->ReadByte();
if (num_cargoes > lengthof(indsp->produced_cargo)) {
if (num_cargoes > std::size(indsp->produced_cargo)) {
GRFError *error = DisableGrf(STR_NEWGRF_ERROR_LIST_PROPERTY_TOO_LONG);
error->param_value[1] = prop;
return CIR_DISABLED;
}
for (uint i = 0; i < lengthof(indsp->produced_cargo); i++) {
for (size_t i = 0; i < std::size(indsp->produced_cargo); i++) {
if (i < num_cargoes) {
CargoID cargo = GetCargoTranslation(buf->ReadByte(), _cur.grffile);
indsp->produced_cargo[i] = cargo;
@ -3806,12 +3806,12 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop,
case 0x26: { // variable length accepted cargoes
uint8_t num_cargoes = buf->ReadByte();
if (num_cargoes > lengthof(indsp->accepts_cargo)) {
if (num_cargoes > std::size(indsp->accepts_cargo)) {
GRFError *error = DisableGrf(STR_NEWGRF_ERROR_LIST_PROPERTY_TOO_LONG);
error->param_value[1] = prop;
return CIR_DISABLED;
}
for (uint i = 0; i < lengthof(indsp->accepts_cargo); i++) {
for (size_t i = 0; i < std::size(indsp->accepts_cargo); i++) {
if (i < num_cargoes) {
CargoID cargo = GetCargoTranslation(buf->ReadByte(), _cur.grffile);
indsp->accepts_cargo[i] = cargo;
@ -3843,13 +3843,13 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop,
case 0x28: { // variable size input/output production multiplier table
uint8_t num_inputs = buf->ReadByte();
uint8_t num_outputs = buf->ReadByte();
if (num_inputs > lengthof(indsp->accepts_cargo) || num_outputs > lengthof(indsp->produced_cargo)) {
if (num_inputs > std::size(indsp->accepts_cargo) || num_outputs > std::size(indsp->produced_cargo)) {
GRFError *error = DisableGrf(STR_NEWGRF_ERROR_LIST_PROPERTY_TOO_LONG);
error->param_value[1] = prop;
return CIR_DISABLED;
}
for (uint i = 0; i < lengthof(indsp->accepts_cargo); i++) {
for (uint j = 0; j < lengthof(indsp->produced_cargo); j++) {
for (size_t i = 0; i < std::size(indsp->accepts_cargo); i++) {
for (size_t j = 0; j < std::size(indsp->produced_cargo); j++) {
uint16_t mult = 0;
if (i < num_inputs && j < num_outputs) mult = buf->ReadWord();
indsp->input_cargo_multiplier[i][j] = mult;
@ -9516,10 +9516,10 @@ static void FinaliseIndustriesArray()
}
/* Apply default cargo translation map for unset cargo slots */
for (uint i = 0; i < lengthof(indsp.produced_cargo); ++i) {
for (size_t i = 0; i < std::size(indsp.produced_cargo); ++i) {
if (!IsValidCargoID(indsp.produced_cargo[i])) indsp.produced_cargo[i] = GetCargoIDByLabel(GetActiveCargoLabel(indsp.produced_cargo_label[i]));
}
for (uint i = 0; i < lengthof(indsp.accepts_cargo); ++i) {
for (size_t i = 0; i < std::size(indsp.accepts_cargo); ++i) {
if (!IsValidCargoID(indsp.accepts_cargo[i])) indsp.accepts_cargo[i] = GetCargoIDByLabel(GetActiveCargoLabel(indsp.accepts_cargo_label[i]));
}
}