Codechange: Remove cargo_suffix C-array from GetIndustryString. (#12472)

The information is pushed onto a vector, so string ownership can be moved there instead of using a pointer into to the CargoSuffix array.
This commit is contained in:
Peter Nelson 2024-04-10 21:32:21 +01:00 committed by GitHub
parent 144bcbbaf1
commit a42aa1a086
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 10 deletions

View File

@ -1559,21 +1559,22 @@ protected:
/* Industry name */
SetDParam(p++, i->index);
static CargoSuffix cargo_suffix[INDUSTRY_NUM_OUTPUTS];
/* Get industry productions (CargoID, production, suffix, transported) */
struct CargoInfo {
CargoID cargo_id;
uint16_t production;
const char *suffix;
uint transported;
CargoID cargo_id; ///< Cargo ID.
uint16_t production; ///< Production last month.
uint transported; ///< Percent transported last month.
std::string suffix; ///< Cargo suffix.
CargoInfo(CargoID cargo_id, uint16_t production, uint transported, std::string &&suffix) : cargo_id(cargo_id), production(production), transported(transported), suffix(std::move(suffix)) {}
};
std::vector<CargoInfo> cargos;
for (auto itp = std::begin(i->produced); itp != std::end(i->produced); ++itp) {
if (!IsValidCargoID(itp->cargo)) continue;
GetCargoSuffix(CARGOSUFFIX_OUT, CST_DIR, i, i->type, indsp, itp->cargo, itp - std::begin(i->produced), cargo_suffix[itp - std::begin(i->produced)]);
cargos.push_back({ itp->cargo, itp->history[LAST_MONTH].production, cargo_suffix[itp - std::begin(i->produced)].text.c_str(), ToPercent8(itp->history[LAST_MONTH].PctTransported()) });
CargoSuffix cargo_suffix;
GetCargoSuffix(CARGOSUFFIX_OUT, CST_DIR, i, i->type, indsp, itp->cargo, itp - std::begin(i->produced), cargo_suffix);
cargos.emplace_back(itp->cargo, itp->history[LAST_MONTH].production, ToPercent8(itp->history[LAST_MONTH].PctTransported()), std::move(cargo_suffix.text));
}
switch (static_cast<IndustryDirectoryWindow::SorterType>(this->industries.SortType())) {
@ -1610,11 +1611,11 @@ protected:
/* Display first 3 cargos */
for (size_t j = 0; j < std::min<size_t>(3, cargos.size()); j++) {
CargoInfo ci = cargos[j];
CargoInfo &ci = cargos[j];
SetDParam(p++, STR_INDUSTRY_DIRECTORY_ITEM_INFO);
SetDParam(p++, ci.cargo_id);
SetDParam(p++, ci.production);
SetDParamStr(p++, ci.suffix);
SetDParamStr(p++, std::move(ci.suffix));
SetDParam(p++, ci.transported);
}