From 4ca1fe6c32f5868f09c8d8b0e16d97ff092ef92c Mon Sep 17 00:00:00 2001 From: Rubidium Date: Thu, 30 Jan 2025 17:09:47 +0100 Subject: [PATCH] Codechange: replace MAX_UVALUE with std::numeric_limits::max --- src/company_cmd.cpp | 4 ++-- src/core/pool_type.hpp | 11 +++++++++-- src/economy.cpp | 2 +- src/engine.cpp | 8 ++++---- src/gfx_type.h | 2 +- src/industry_cmd.cpp | 2 +- src/newgrf_airporttiles.cpp | 2 +- src/object_gui.cpp | 2 +- src/saveload/afterload.cpp | 6 +++--- src/saveload/engine_sl.cpp | 2 +- src/saveload/oldloader_sl.cpp | 6 +++--- src/smallmap_gui.cpp | 2 +- src/stdafx.h | 6 ------ src/town_cmd.cpp | 2 +- 14 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index e955091c5b..061049d711 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -718,7 +718,7 @@ static void HandleBankruptcyTakeover(Company *c) } /* Did we ask everyone for bankruptcy? If so, bail out. */ - if (c->bankrupt_asked == MAX_UVALUE(CompanyMask)) return; + if (c->bankrupt_asked == std::numeric_limits::max()) return; Company *best = nullptr; int32_t best_performance = -1; @@ -736,7 +736,7 @@ static void HandleBankruptcyTakeover(Company *c) /* Asked all companies? */ if (best_performance == -1) { - c->bankrupt_asked = MAX_UVALUE(CompanyMask); + c->bankrupt_asked = std::numeric_limits::max(); return; } diff --git a/src/core/pool_type.hpp b/src/core/pool_type.hpp index 2cdb3254c4..49644a7375 100644 --- a/src/core/pool_type.hpp +++ b/src/core/pool_type.hpp @@ -78,8 +78,15 @@ private: */ template struct Pool : PoolBase { +private: + /** Some helper functions to get the maximum value of the provided index. */ + template + static constexpr size_t GetMaxIndexValue(T) { return std::numeric_limits::max(); } + template requires std::is_enum_v + static constexpr size_t GetMaxIndexValue(T) { return std::numeric_limits>::max(); } +public: /* Ensure the highest possible index, i.e. Tmax_size -1, is within the bounds of Tindex. */ - static_assert(Tmax_size - 1 <= MAX_UVALUE(Tindex)); + static_assert(Tmax_size - 1 <= GetMaxIndexValue(Tindex{})); static constexpr size_t MAX_SIZE = Tmax_size; ///< Make template parameter accessible from outside @@ -388,7 +395,7 @@ struct Pool : PoolBase { }; private: - static const size_t NO_FREE_ITEM = MAX_UVALUE(size_t); ///< Constant to indicate we can't allocate any more items + static const size_t NO_FREE_ITEM = std::numeric_limits::max(); ///< Constant to indicate we can't allocate any more items /** * Helper struct to cache 'freed' PoolItems so we diff --git a/src/economy.cpp b/src/economy.cpp index 2992ac3332..d1d9019d91 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -627,7 +627,7 @@ static void CompanyCheckBankrupt(Company *c) * is no THE-END, otherwise mark the client as spectator to make sure * they are no longer in control of this company. However... when you * join another company (cheat) the "unowned" company can bankrupt. */ - c->bankrupt_asked = MAX_UVALUE(CompanyMask); + c->bankrupt_asked = std::numeric_limits::max(); break; } diff --git a/src/engine.cpp b/src/engine.cpp index 9f767940dd..dbc68b73d7 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -744,7 +744,7 @@ void StartupOneEngine(Engine *e, const TimerGameCalendar::YearMonthDay &aging_ym int intro_months = intro_ymd.year.base() * 12 + intro_ymd.month; if (intro_ymd.day > 1) intro_months++; // Engines are introduced at the first month start at/after intro date. e->age = aging_months - intro_months; - e->company_avail = MAX_UVALUE(CompanyMask); + e->company_avail = std::numeric_limits::max(); e->flags.Set(EngineFlag::Available); } @@ -885,7 +885,7 @@ static void AcceptEnginePreview(EngineID eid, CompanyID company, int recursion_d Engine *e = Engine::Get(eid); e->preview_company = INVALID_COMPANY; - e->preview_asked = MAX_UVALUE(CompanyMask); + e->preview_asked = std::numeric_limits::max(); EnableEngineForCompany(eid, company); @@ -980,7 +980,7 @@ static IntervalTimer _calendar_engines_daily({TimerGameCalend e->preview_company = GetPreviewCompany(e); if (e->preview_company == INVALID_COMPANY) { - e->preview_asked = MAX_UVALUE(CompanyMask); + e->preview_asked = std::numeric_limits::max(); continue; } @@ -1109,7 +1109,7 @@ static void NewVehicleAvailable(Engine *e) AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type); /* Now available for all companies */ - e->company_avail = MAX_UVALUE(CompanyMask); + e->company_avail = std::numeric_limits::max(); /* Do not introduce new rail wagons */ if (IsWagon(index)) return; diff --git a/src/gfx_type.h b/src/gfx_type.h index 59a0933267..85eb55b094 100644 --- a/src/gfx_type.h +++ b/src/gfx_type.h @@ -106,7 +106,7 @@ enum WindowKeyCodes : uint16_t { /** A single sprite of a list of animated cursors */ struct AnimCursor { - static const CursorID LAST = MAX_UVALUE(CursorID); + static const CursorID LAST = std::numeric_limits::max(); CursorID sprite; ///< Must be set to LAST_ANIM when it is the last sprite of the loop uint8_t display_time; ///< Amount of ticks this sprite will be shown }; diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index f5c7ea50fd..1d50df13b2 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -224,7 +224,7 @@ void Industry::PostDestructor(size_t) { if (Industry::GetNumItems() == 0) return nullptr; int num = RandomRange((uint16_t)Industry::GetNumItems()); - size_t index = MAX_UVALUE(size_t); + size_t index = std::numeric_limits::max(); while (num >= 0) { num--; diff --git a/src/newgrf_airporttiles.cpp b/src/newgrf_airporttiles.cpp index ac98b84e29..f1624b7801 100644 --- a/src/newgrf_airporttiles.cpp +++ b/src/newgrf_airporttiles.cpp @@ -38,7 +38,7 @@ AirportTileOverrideManager _airporttile_mngr(NEW_AIRPORTTILE_OFFSET, NUM_AIRPORT { /* should be assert(gfx < lengthof(tiles)), but that gives compiler warnings * since it's always true if the following holds: */ - static_assert(MAX_UVALUE(StationGfx) + 1 == lengthof(tiles)); + static_assert(std::numeric_limits::max() + 1 == lengthof(tiles)); return &AirportTileSpec::tiles[gfx]; } diff --git a/src/object_gui.cpp b/src/object_gui.cpp index a66d3dc112..c1e261a83e 100644 --- a/src/object_gui.cpp +++ b/src/object_gui.cpp @@ -300,7 +300,7 @@ public: { switch (widget) { case WID_BO_OBJECT_SPRITE: - if (_object_gui.sel_type != MAX_UVALUE(uint16_t)) { + if (_object_gui.sel_type != std::numeric_limits::max()) { _object_gui.sel_view = this->GetWidget(widget)->GetParentWidget()->GetCurrentElement(); this->InvalidateData(PickerWindow::PFI_POSITION); if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP); diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 9f373a521f..69d3f56900 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -2041,15 +2041,15 @@ bool AfterLoadGame() /* More companies ... */ for (Company *c : Company::Iterate()) { - if (c->bankrupt_asked == 0xFF) c->bankrupt_asked = MAX_UVALUE(CompanyMask); + if (c->bankrupt_asked == 0xFF) c->bankrupt_asked = std::numeric_limits::max(); } for (Engine *e : Engine::Iterate()) { - if (e->company_avail == 0xFF) e->company_avail = MAX_UVALUE(CompanyMask); + if (e->company_avail == 0xFF) e->company_avail = std::numeric_limits::max(); } for (Town *t : Town::Iterate()) { - if (t->have_ratings == 0xFF) t->have_ratings = MAX_UVALUE(CompanyMask); + if (t->have_ratings == 0xFF) t->have_ratings = std::numeric_limits::max(); for (uint i = 8; i != MAX_COMPANIES; i++) t->ratings[i] = RATING_INITIAL; } } diff --git a/src/saveload/engine_sl.cpp b/src/saveload/engine_sl.cpp index f624e604a3..26aee110ba 100644 --- a/src/saveload/engine_sl.cpp +++ b/src/saveload/engine_sl.cpp @@ -109,7 +109,7 @@ struct ENGNChunkHandler : ChunkHandler { * Just cancel any previews. */ e->flags.Reset(EngineFlag{4}); // ENGINE_OFFER_WINDOW_OPEN e->preview_company = INVALID_COMPANY; - e->preview_asked = MAX_UVALUE(CompanyMask); + e->preview_asked = std::numeric_limits::max(); } } } diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index f1eb605c1a..292c129a60 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -406,7 +406,7 @@ static bool FixTTOEngines() /* Make sure for example monorail and maglev are available when they should be */ if (TimerGameCalendar::date >= e->intro_date && HasBit(e->info.climates, 0)) { e->flags.Set(EngineFlag::Available); - e->company_avail = MAX_UVALUE(CompanyMask); + e->company_avail = std::numeric_limits::max(); e->age = TimerGameCalendar::date > e->intro_date ? (TimerGameCalendar::date - e->intro_date).base() / 30 : 0; } } else { @@ -431,7 +431,7 @@ static bool FixTTOEngines() * if at least one of them was available. */ for (uint j = 0; j < lengthof(tto_to_ttd); j++) { if (tto_to_ttd[j] == i && _old_engines[j].company_avail != 0) { - e->company_avail = MAX_UVALUE(CompanyMask); + e->company_avail = std::numeric_limits::max(); e->flags.Set(EngineFlag::Available); break; } @@ -441,7 +441,7 @@ static bool FixTTOEngines() } e->preview_company = INVALID_COMPANY; - e->preview_asked = MAX_UVALUE(CompanyMask); + e->preview_asked = std::numeric_limits::max(); e->preview_wait = 0; e->name = std::string{}; } diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp index 94162a8bfd..8189f8a9be 100644 --- a/src/smallmap_gui.cpp +++ b/src/smallmap_gui.cpp @@ -724,7 +724,7 @@ protected: */ inline CompanyMask GetOverlayCompanyMask() const { - return Company::IsValidID(_local_company) ? 1U << _local_company : MAX_UVALUE(CompanyMask); + return Company::IsValidID(_local_company) ? 1U << _local_company : std::numeric_limits::max(); } /** Blink the industries (if selected) on a regular interval. */ diff --git a/src/stdafx.h b/src/stdafx.h index e26f3f199a..bdfc049cb6 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -333,12 +333,6 @@ inline void free(const void *ptr) free(const_cast(ptr)); } -/** - * The largest value that can be entered in a variable - * @param type the type of the variable - */ -#define MAX_UVALUE(type) (static_cast(~static_cast(0))) - #if defined(_MSC_VER) && !defined(_DEBUG) # define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700)) # define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop)) diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index f23d2a187c..aaf8d4b2df 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -197,7 +197,7 @@ void Town::InitializeLayout(TownLayout layout) { if (Town::GetNumItems() == 0) return nullptr; int num = RandomRange((uint16_t)Town::GetNumItems()); - size_t index = MAX_UVALUE(size_t); + size_t index = std::numeric_limits::max(); while (num >= 0) { num--;