From 6c8915fdbc01e808201a6e43765e5cf5a3e40ff7 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Fri, 31 Jan 2025 21:37:32 +0100 Subject: [PATCH] Codechange: strongly type LeagueTableID and LeagueTableElementID --- src/league_base.h | 4 ++-- src/league_gui.cpp | 3 +-- src/league_type.h | 10 ++++++---- src/script/api/script_league.cpp | 4 ++-- src/toolbar_gui.cpp | 2 +- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/league_base.h b/src/league_base.h index 1799b92cf3..27c354962c 100644 --- a/src/league_base.h +++ b/src/league_base.h @@ -17,10 +17,10 @@ bool IsValidLink(Link link); -typedef Pool LeagueTableElementPool; +using LeagueTableElementPool = Pool; extern LeagueTableElementPool _league_table_element_pool; -typedef Pool LeagueTablePool; +using LeagueTablePool = Pool; extern LeagueTablePool _league_table_pool; diff --git a/src/league_gui.cpp b/src/league_gui.cpp index 5841211758..a48812706a 100644 --- a/src/league_gui.cpp +++ b/src/league_gui.cpp @@ -298,9 +298,8 @@ private: } public: - ScriptLeagueWindow(WindowDesc &desc, LeagueTableID table) : Window(desc) + ScriptLeagueWindow(WindowDesc &desc, WindowNumber table) : Window(desc), table(table) { - this->table = table; this->BuildTable(); this->InitNested(table); } diff --git a/src/league_type.h b/src/league_type.h index 8954d66e5f..e423a7421d 100644 --- a/src/league_type.h +++ b/src/league_type.h @@ -10,6 +10,8 @@ #ifndef LEAGUE_TYPE_H #define LEAGUE_TYPE_H +#include "core/pool_type.hpp" + /** Types of the possible link targets. */ enum LinkType : uint8_t { LT_NONE = 0, ///< No link @@ -29,12 +31,12 @@ struct Link { Link(): Link(LT_NONE, 0) {} }; -typedef uint8_t LeagueTableID; ///< ID of a league table +using LeagueTableID = PoolID; ///< ID of a league table struct LeagueTable; -static const LeagueTableID INVALID_LEAGUE_TABLE = 0xFF; ///< Invalid/unknown index of LeagueTable +static constexpr LeagueTableID INVALID_LEAGUE_TABLE = LeagueTableID::Invalid(); ///< Invalid/unknown index of LeagueTable -typedef uint16_t LeagueTableElementID; ///< ID of a league table element +using LeagueTableElementID = PoolID; ///< ID of a league table struct LeagueTableElement; -static const LeagueTableElementID INVALID_LEAGUE_TABLE_ELEMENT = 0xFFFF; ///< Invalid/unknown index of LeagueTableElement +static constexpr LeagueTableElementID INVALID_LEAGUE_TABLE_ELEMENT = LeagueTableElementID::Invalid(); ///< Invalid/unknown index of LeagueTableElement #endif /* LEAGUE_TYPE_H */ diff --git a/src/script/api/script_league.cpp b/src/script/api/script_league.cpp index 5b831118eb..90a04dabbc 100644 --- a/src/script/api/script_league.cpp +++ b/src/script/api/script_league.cpp @@ -41,7 +41,7 @@ if (!ScriptObject::Command::Do(&ScriptInstance::DoCommandReturnLeagueTableID, encoded_title, encoded_header, encoded_footer)) return LEAGUE_TABLE_INVALID; /* In case of test-mode, we return LeagueTableID 0 */ - return static_cast(0); + return LeagueTableID::Begin(); } /* static */ bool ScriptLeagueTable::IsValidLeagueTableElement(LeagueTableElementID element_id) @@ -74,7 +74,7 @@ if (!ScriptObject::Command::Do(&ScriptInstance::DoCommandReturnLeagueTableElementID, table, rating, c, encoded_text, encoded_score, (::LinkType)link_type, (::LinkTargetID)link_target)) return LEAGUE_TABLE_ELEMENT_INVALID; /* In case of test-mode, we return LeagueTableElementID 0 */ - return static_cast(0); + return LeagueTableElementID::Begin(); } /* static */ bool ScriptLeagueTable::UpdateElementData(LeagueTableElementID element, ScriptCompany::CompanyID company, Text *text, LinkType link_type, SQInteger link_target) diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index 44e3eecbe5..f10831c5e4 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -663,7 +663,7 @@ static void AddDropDownLeagueTableOptions(DropDownList &list) { if (LeagueTable::GetNumItems() > 0) { for (LeagueTable *lt : LeagueTable::Iterate()) { - list.push_back(MakeDropDownListStringItem(lt->title, lt->index)); + list.push_back(MakeDropDownListStringItem(lt->title, lt->index.base())); } } else { list.push_back(MakeDropDownListStringItem(STR_GRAPH_MENU_COMPANY_LEAGUE_TABLE, LTMN_PERFORMANCE_LEAGUE));