diff --git a/src/aircraft.h b/src/aircraft.h index 9865f12e83..8a0ec77403 100644 --- a/src/aircraft.h +++ b/src/aircraft.h @@ -68,6 +68,8 @@ struct AircraftCache { uint32_t cached_max_range_sqr; ///< Cached squared maximum range. uint16_t cached_max_range; ///< Cached maximum range. uint8_t image_movement_state; ///< Cached image aircraft movement state + + bool operator==(const AircraftCache &) const = default; }; /** diff --git a/src/company_base.h b/src/company_base.h index 6a08b8c46d..7e7ca8ad4e 100644 --- a/src/company_base.h +++ b/src/company_base.h @@ -50,6 +50,8 @@ struct CompanyInfrastructure { uint32_t GetTramTotal() const; char *Dump(char *buffer, const char *last) const; + + bool operator==(const CompanyInfrastructure &) const = default; }; class FreeUnitIDGenerator { diff --git a/src/ground_vehicle.hpp b/src/ground_vehicle.hpp index a93f7d4dea..5982e2113b 100644 --- a/src/ground_vehicle.hpp +++ b/src/ground_vehicle.hpp @@ -47,6 +47,8 @@ struct GroundVehicleCache { /* Cached UI information. */ uint16_t last_speed; ///< The last speed we did display, so we only have to redraw when this changes. + + bool operator==(const GroundVehicleCache &) const = default; }; /** Ground vehicle flags. */ diff --git a/src/openttd.cpp b/src/openttd.cpp index 5b1d2ff20f..2381a491bc 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1749,7 +1749,7 @@ void CheckCaches(bool force_check, std::function log, CheckC if (old_town_caches[i].part_of_subsidy != t->cache.part_of_subsidy) { CCLOG("town cache population mismatch: town %i, (old size: %u, new size: %u)", (int)t->index, old_town_caches[i].part_of_subsidy, t->cache.part_of_subsidy); } - if (MemCmpT(old_town_caches[i].squared_town_zone_radius, t->cache.squared_town_zone_radius, lengthof(t->cache.squared_town_zone_radius)) != 0) { + if (old_town_caches[i].squared_town_zone_radius != t->cache.squared_town_zone_radius) { CCLOG("town cache squared_town_zone_radius mismatch: town %i", (int)t->index); } if (old_town_caches[i].building_counts != t->cache.building_counts) { @@ -1807,7 +1807,7 @@ void CheckCaches(bool force_check, std::function log, CheckC uint i = 0; for (const Company *c : Company::Iterate()) { - if (MemCmpT(old_infrastructure.data() + i, &c->infrastructure) != 0) { + if (old_infrastructure[i] != c->infrastructure) { CCLOG("infrastructure cache mismatch: company %i", (int)c->index); char buffer[4096]; old_infrastructure[i].Dump(buffer, lastof(buffer)); @@ -1963,10 +1963,10 @@ void CheckCaches(bool force_check, std::function log, CheckC }; switch (u->type) { case VEH_TRAIN: - if (memcmp(&gro_cache[length], &Train::From(u)->gcache, sizeof(GroundVehicleCache)) != 0) { + if (gro_cache[length] != Train::From(u)->gcache) { print_gv_cache_diff("train", gro_cache[length], Train::From(u)->gcache); } - if (memcmp(&tra_cache[length], &Train::From(u)->tcache, sizeof(TrainCache)) != 0) { + if (tra_cache[length] != Train::From(u)->tcache) { CCLOGV("train cache mismatch: %c%c%c%c%c%c%c%c%c%c%c", tra_cache[length].cached_override != Train::From(u)->tcache.cached_override ? 'o' : '-', tra_cache[length].cached_curve_speed_mod != Train::From(u)->tcache.cached_curve_speed_mod ? 'C' : '-', @@ -1991,12 +1991,12 @@ void CheckCaches(bool force_check, std::function log, CheckC } break; case VEH_ROAD: - if (memcmp(&gro_cache[length], &RoadVehicle::From(u)->gcache, sizeof(GroundVehicleCache)) != 0) { + if (gro_cache[length] != RoadVehicle::From(u)->gcache) { print_gv_cache_diff("road vehicle", gro_cache[length], Train::From(u)->gcache); } break; case VEH_AIRCRAFT: - if (memcmp(&air_cache[length], &Aircraft::From(u)->acache, sizeof(AircraftCache)) != 0) { + if (air_cache[length] != Aircraft::From(u)->acache) { CCLOGV("Aircraft vehicle cache mismatch: %c%c", air_cache[length].cached_max_range != Aircraft::From(u)->acache.cached_max_range ? 'r' : '-', air_cache[length].cached_max_range_sqr != Aircraft::From(u)->acache.cached_max_range_sqr ? 's' : '-'); diff --git a/src/sl/town_sl.cpp b/src/sl/town_sl.cpp index b4874ac6f0..754cbda0e7 100644 --- a/src/sl/town_sl.cpp +++ b/src/sl/town_sl.cpp @@ -377,6 +377,8 @@ void SlResetTNNC() _town_zone_radii_no_update = false; } +static_assert(std::tuple_size::value == HZB_END); + void Save_TNNC() { assert(_sl_xv_feature_versions[XSLFI_TNNC_CHUNK] != 0); @@ -391,7 +393,7 @@ void Save_TNNC() if (IsGetTownZonesCallbackHandlerPresent()) { flags |= 1; - length += Town::GetNumItems() * lengthof(TownCache::squared_town_zone_radius) * 4; + length += Town::GetNumItems() * HZB_END * 4; } SlSetLength(length); @@ -403,7 +405,7 @@ void Save_TNNC() SlWriteUint32(t->index); SlWriteUint16(t->noise_reached); if (flags & 1) { - for (uint i = 0; i < lengthof(TownCache::squared_town_zone_radius); i++) { + for (uint i = 0; i < HZB_END; i++) { SlWriteUint32(t->cache.squared_town_zone_radius[i]); } } @@ -430,7 +432,7 @@ void Load_TNNC() if (t == nullptr) SlErrorCorrupt("TNNC: invalid town ID"); t->noise_reached = SlReadUint16(); if (flags & 1) { - for (uint i = 0; i < lengthof(TownCache::squared_town_zone_radius); i++) { + for (uint i = 0; i < HZB_END; i++) { t->cache.squared_town_zone_radius[i] = SlReadUint32(); } } diff --git a/src/town.h b/src/town.h index 2f461ee1f2..0b82538ab2 100644 --- a/src/town.h +++ b/src/town.h @@ -57,7 +57,7 @@ struct TownCache { uint32_t population; ///< Current population of people TrackedViewportSign sign; ///< Location of name sign, UpdateVirtCoord updates this PartOfSubsidy part_of_subsidy; ///< Is this town a source/destination of a subsidy? - uint32_t squared_town_zone_radius[HZB_END]; ///< UpdateTownRadius updates this given the house count + std::array squared_town_zone_radius; ///< UpdateTownRadius updates this given the house count BuildingCounts building_counts; ///< The number of each type of building in the town }; diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 8a7e86900b..84f5b8ca44 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -2105,7 +2105,7 @@ static bool GrowTown(Town *t) */ void UpdateTownRadius(Town *t) { - static const uint32_t _town_squared_town_zone_radius_data[23][HZB_END] = { + static const std::array, 23> _town_squared_town_zone_radius_data = {{ { 4, 0, 0, 0, 0}, // 0 { 16, 0, 0, 0, 0}, { 25, 0, 0, 0, 0}, @@ -2129,7 +2129,7 @@ void UpdateTownRadius(Town *t) {121, 81, 0, 49, 25}, // 80 {121, 81, 0, 49, 25}, {121, 81, 0, 49, 36}, // 88 - }; + }}; if (_settings_game.economy.town_zone_calc_mode) { int mass = t->cache.num_houses / 8; @@ -2149,7 +2149,7 @@ void UpdateTownRadius(Town *t) return; } - MemSetT(t->cache.squared_town_zone_radius, 0, lengthof(t->cache.squared_town_zone_radius)); + t->cache.squared_town_zone_radius = {}; uint16_t cb_result = GetTownZonesCallback(t); if (cb_result == 0) { @@ -2162,7 +2162,7 @@ void UpdateTownRadius(Town *t) } if (t->cache.num_houses < 92) { - memcpy(t->cache.squared_town_zone_radius, _town_squared_town_zone_radius_data[t->cache.num_houses / 4], sizeof(t->cache.squared_town_zone_radius)); + t->cache.squared_town_zone_radius = _town_squared_town_zone_radius_data[t->cache.num_houses / 4]; } else { int mass = t->cache.num_houses / 8; /* Actually we are proportional to sqrt() but that's right because we are covering an area. diff --git a/src/train.h b/src/train.h index 4017b29a74..fa8a78494d 100644 --- a/src/train.h +++ b/src/train.h @@ -123,6 +123,8 @@ struct TrainCache { int16_t cached_curve_speed_mod; ///< curve speed modifier of the entire train uint16_t cached_max_curve_speed; ///< max consist speed limited by curves + + bool operator==(const TrainCache &) const = default; }; /**