From bf8de188ec36cf29fdd6cf08436a71d288d3f18a Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 26 Apr 2024 22:58:54 +0100 Subject: [PATCH] Codechange: Use member initialization of GRFFilePropsBase. (#12581) Don't blame compilers for our sloppy initialisation. Removes memset, and lengthof. --- src/newgrf_commons.h | 18 ++++-------------- src/newgrf_engine.cpp | 4 ++-- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/newgrf_commons.h b/src/newgrf_commons.h index bdfea825e6136..a45fcdf5924db 100644 --- a/src/newgrf_commons.h +++ b/src/newgrf_commons.h @@ -308,25 +308,15 @@ bool Convert8bitBooleanCallback(const struct GRFFile *grffile, uint16_t cbid, ui */ template struct GRFFilePropsBase { - GRFFilePropsBase() : local_id(0), grffile(nullptr) - { - /* The lack of some compilers to provide default constructors complying to the specs - * requires us to zero the stuff ourself. */ - memset(spritegroup, 0, sizeof(spritegroup)); - } - - uint16_t local_id; ///< id defined by the grf file for this entity - const struct GRFFile *grffile; ///< grf file that introduced this entity - const struct SpriteGroup *spritegroup[Tcnt]; ///< pointer to the different sprites of the entity + uint16_t local_id = 0; ///< id defined by the grf file for this entity + const struct GRFFile *grffile = nullptr; ///< grf file that introduced this entity + std::array spritegroup{}; ///< pointers to the different sprites of the entity }; /** Data related to the handling of grf files. */ struct GRFFileProps : GRFFilePropsBase<1> { /** Set all default data constructor for the props. */ - GRFFileProps(uint16_t subst_id = 0) : - GRFFilePropsBase<1>(), subst_id(subst_id), override(subst_id) - { - } + constexpr GRFFileProps(uint16_t subst_id = 0) : subst_id(subst_id), override(subst_id) {} uint16_t subst_id; uint16_t override; ///< id of the entity been replaced by diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 08e461d06c596..0b7e6e08e70a1 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -53,7 +53,7 @@ const SpriteGroup *GetWagonOverrideSpriteSet(EngineID engine, CargoID cargo, Eng void SetCustomEngineSprites(EngineID engine, uint8_t cargo, const SpriteGroup *group) { Engine *e = Engine::Get(engine); - assert(cargo < lengthof(e->grf_prop.spritegroup)); + assert(cargo < std::size(e->grf_prop.spritegroup)); if (e->grf_prop.spritegroup[cargo] != nullptr) { GrfMsg(6, "SetCustomEngineSprites: engine {} cargo {} already has group -- replacing", engine, cargo); @@ -1062,7 +1062,7 @@ VehicleResolverObject::VehicleResolverObject(EngineID engine_type, const Vehicle if (this->root_spritegroup == nullptr) { const Engine *e = Engine::Get(engine_type); CargoID cargo = v != nullptr ? v->cargo_type : SpriteGroupCargo::SG_PURCHASE; - assert(cargo < lengthof(e->grf_prop.spritegroup)); + assert(cargo < std::size(e->grf_prop.spritegroup)); this->root_spritegroup = e->grf_prop.spritegroup[cargo] != nullptr ? e->grf_prop.spritegroup[cargo] : e->grf_prop.spritegroup[SpriteGroupCargo::SG_DEFAULT]; } }