From 6b87fe6540d1c1a4798ee2169d4606765396adf2 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 18 May 2023 19:56:24 +0100 Subject: [PATCH] Codechange: Use std::array for GRF(File|Config) parameters. This simplifies comparison, copying and assignment operations. --- src/gamelog.cpp | 2 +- src/newgrf.cpp | 11 +++-------- src/newgrf.h | 6 +++--- src/newgrf_config.cpp | 8 ++++---- src/newgrf_config.h | 2 +- src/newgrf_gui.cpp | 4 ++-- src/settings.cpp | 8 ++++---- 7 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/gamelog.cpp b/src/gamelog.cpp index 05ae5f4ff4..461e0ab3a5 100644 --- a/src/gamelog.cpp +++ b/src/gamelog.cpp @@ -659,7 +659,7 @@ void Gamelog::GRFUpdate(const GRFConfig *oldc, const GRFConfig *newc) this->GRFCompatible(&nl[n]->ident); } - if (og->num_params != ng->num_params || memcmp(og->param, ng->param, og->num_params * sizeof(og->param[0])) != 0) { + if (og->num_params != ng->num_params || og->param == ng->param) { this->GRFParameters(ol[o]->ident.grfid); } diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 49af8ccf41..ba49f32c92 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -8090,7 +8090,7 @@ static bool ChangeGRFNumUsedParams(size_t len, ByteReader *buf) GrfMsg(2, "StaticGRFInfo: expected only 1 byte for 'INFO'->'NPAR' but got {}, ignoring this field", len); buf->Skip(len); } else { - _cur.grfconfig->num_valid_params = std::min(buf->ReadByte(), lengthof(_cur.grfconfig->param)); + _cur.grfconfig->num_valid_params = std::min(buf->ReadByte(), ClampTo(_cur.grfconfig->param.size())); } return true; } @@ -8239,7 +8239,7 @@ static bool ChangeGRFParamMask(size_t len, ByteReader *buf) buf->Skip(len); } else { byte param_nr = buf->ReadByte(); - if (param_nr >= lengthof(_cur.grfconfig->param)) { + if (param_nr >= _cur.grfconfig->param.size()) { GrfMsg(2, "StaticGRFInfo: invalid parameter number in 'INFO'->'PARA'->'MASK', param {}, ignoring this field", param_nr); buf->Skip(len - 1); } else { @@ -8926,13 +8926,8 @@ GRFFile::GRFFile(const GRFConfig *config) /* Copy the initial parameter list * 'Uninitialised' parameters are zeroed as that is their default value when dynamically creating them. */ - static_assert(lengthof(this->param) == lengthof(config->param) && lengthof(this->param) == 0x80); - - assert(config->num_params <= lengthof(config->param)); + this->param = config->param; this->param_end = config->num_params; - if (this->param_end > 0) { - MemCpyT(this->param, config->param, this->param_end); - } } GRFFile::~GRFFile() diff --git a/src/newgrf.h b/src/newgrf.h index af7af801d0..50f2897d5b 100644 --- a/src/newgrf.h +++ b/src/newgrf.h @@ -121,7 +121,7 @@ struct GRFFile : ZeroedMemoryAllocator { std::vector> airtspec; std::vector> roadstops; - uint32 param[0x80]; + std::array param; uint param_end; ///< one more than the highest set parameter std::vector labels; ///< List of labels @@ -154,9 +154,9 @@ struct GRFFile : ZeroedMemoryAllocator { /** Get GRF Parameter with range checking */ uint32 GetParam(uint number) const { - /* Note: We implicitly test for number < lengthof(this->param) and return 0 for invalid parameters. + /* Note: We implicitly test for number < this->param.size() and return 0 for invalid parameters. * In fact this is the more important test, as param is zeroed anyway. */ - assert(this->param_end <= lengthof(this->param)); + assert(this->param_end <= this->param.size()); return (number < this->param_end) ? this->param[number] : 0; } }; diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 17b0ab6464..f0e6d7a482 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -35,7 +35,7 @@ * @param filename Set the filename of this GRFConfig to filename. */ GRFConfig::GRFConfig(const std::string &filename) : - filename(filename), num_valid_params(lengthof(param)) + filename(filename), num_valid_params(ClampTo(GRFConfig::param.size())) { } @@ -56,13 +56,13 @@ GRFConfig::GRFConfig(const GRFConfig &config) : flags(config.flags & ~(1 << GCF_COPY)), status(config.status), grf_bugs(config.grf_bugs), + param(config.param), num_params(config.num_params), num_valid_params(config.num_valid_params), palette(config.palette), param_info(config.param_info), has_param_defaults(config.has_param_defaults) { - MemCpyT(this->param, config.param, lengthof(this->param)); if (config.error != nullptr) this->error = std::make_unique(*config.error); } @@ -74,7 +74,7 @@ void GRFConfig::CopyParams(const GRFConfig &src) { this->num_params = src.num_params; this->num_valid_params = src.num_valid_params; - MemCpyT(this->param, src.param, lengthof(this->param)); + this->param = src.param; } /** @@ -110,7 +110,7 @@ const char *GRFConfig::GetURL() const void GRFConfig::SetParameterDefaults() { this->num_params = 0; - MemSetT(this->param, 0, lengthof(this->param)); + this->param = {}; if (!this->has_param_defaults) return; diff --git a/src/newgrf_config.h b/src/newgrf_config.h index ef71e7e8d1..3273d001bf 100644 --- a/src/newgrf_config.h +++ b/src/newgrf_config.h @@ -164,7 +164,7 @@ struct GRFConfig : ZeroedMemoryAllocator { uint8 flags; ///< NOSAVE: GCF_Flags, bitset GRFStatus status; ///< NOSAVE: GRFStatus, enum uint32 grf_bugs; ///< NOSAVE: bugs in this GRF in this run, @see enum GRFBugs - uint32 param[0x80]; ///< GRF parameters + std::array param; ///< GRF parameters uint8 num_params; ///< Number of used parameters uint8 num_valid_params; ///< NOSAVE: Number of valid parameters (action 0x14) uint8 palette; ///< GRFPalette, bitset diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index ba6ffa307d..e73dbd90e6 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -169,7 +169,7 @@ struct NewGRFParametersWindow : public Window { clicked_row(UINT_MAX), editable(editable) { - this->action14present = (c->num_valid_params != lengthof(c->param) || c->param_info.size() != 0); + this->action14present = (c->num_valid_params != c->param.size() || c->param_info.size() != 0); this->CreateNestedTree(); this->vscroll = this->GetScrollbar(WID_NP_SCROLLBAR); @@ -225,7 +225,7 @@ struct NewGRFParametersWindow : public Window { } case WID_NP_NUMPAR: { - SetDParamMaxValue(0, lengthof(this->grf_config->param)); + SetDParamMaxValue(0, this->grf_config->param.size()); Dimension d = GetStringBoundingBox(this->GetWidget(widget)->widget_data); d.width += padding.width; d.height += padding.height; diff --git a/src/settings.cpp b/src/settings.cpp index 7843714889..5e5cfe1178 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -227,9 +227,9 @@ static size_t LookupManyOfMany(const std::vector &many, const char * @return returns the number of items found, or -1 on an error */ template -static int ParseIntList(const char *p, T *items, int maxitems) +static int ParseIntList(const char *p, T *items, size_t maxitems) { - int n = 0; // number of items read so far + size_t n = 0; // number of items read so far bool comma = false; // do we accept comma? while (*p != '\0') { @@ -262,7 +262,7 @@ static int ParseIntList(const char *p, T *items, int maxitems) * We have read comma when (n != 0) and comma is not allowed */ if (n != 0 && !comma) return -1; - return n; + return ClampTo(n); } /** @@ -996,7 +996,7 @@ static GRFConfig *GRFLoadConfig(IniFile &ini, const char *grpname, bool is_stati /* Parse parameters */ if (item->value.has_value() && !item->value->empty()) { - int count = ParseIntList(item->value->c_str(), c->param, lengthof(c->param)); + int count = ParseIntList(item->value->c_str(), c->param.data(), c->param.size()); if (count < 0) { SetDParamStr(0, filename); ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY, WL_CRITICAL);