From 1e24b41f229ffb7e58beb3ce590bf7504e2cc954 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 19 Jan 2025 17:52:52 +0100 Subject: [PATCH] Codechange: strongly type GroupID --- src/autoreplace_gui.cpp | 6 +++--- src/company_gui.cpp | 14 +++++++------- src/group.h | 2 +- src/group_gui.cpp | 2 +- src/group_type.h | 11 ++++++----- src/saveload/afterload.cpp | 2 +- src/script/api/script_group.cpp | 4 ++-- 7 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp index 5dbd792300..a0b1dc09c7 100644 --- a/src/autoreplace_gui.cpp +++ b/src/autoreplace_gui.cpp @@ -381,12 +381,12 @@ public: switch (widget) { case WID_RV_CAPTION: SetDParam(0, STR_REPLACE_VEHICLE_TRAIN + this->window_number); - switch (this->sel_group) { - case ALL_GROUP: + switch (this->sel_group.base()) { + case ALL_GROUP.base(): SetDParam(1, STR_GROUP_ALL_TRAINS + this->window_number); break; - case DEFAULT_GROUP: + case DEFAULT_GROUP.base(): SetDParam(1, STR_GROUP_DEFAULT_TRAINS + this->window_number); break; diff --git a/src/company_gui.cpp b/src/company_gui.cpp index 9b9bd46c30..3e14337311 100644 --- a/src/company_gui.cpp +++ b/src/company_gui.cpp @@ -723,7 +723,7 @@ public: case VEH_AIRCRAFT: this->livery_class = LC_GROUP_AIRCRAFT; break; default: NOT_REACHED(); } - this->sel = group; + this->sel = group.base(); this->LowerWidget(WID_SCL_CLASS_GENERAL + this->livery_class); this->groups.ForceRebuild(); @@ -944,12 +944,12 @@ public: } } } else { - this->sel = INVALID_GROUP; + this->sel = INVALID_GROUP.base(); this->groups.ForceRebuild(); this->BuildGroupList(this->window_number); if (!this->groups.empty()) { - this->sel = this->groups[0].group->index; + this->sel = this->groups[0].group->index.base(); } } @@ -986,7 +986,7 @@ public: auto it = this->vscroll->GetScrolledItemFromWidget(this->groups, pt.y, this, widget); if (it == std::end(this->groups)) return; - this->sel = it->group->index; + this->sel = it->group->index.base(); } this->SetDirty(); break; @@ -1017,7 +1017,7 @@ public: } } else { /* Setting group livery */ - Command::Post(this->sel, widget == WID_SCL_PRI_COL_DROPDOWN, colour); + Command::Post(static_cast(this->sel), widget == WID_SCL_PRI_COL_DROPDOWN, colour); } } @@ -1038,8 +1038,8 @@ public: this->SetRows(); if (!Group::IsValidID(this->sel)) { - this->sel = INVALID_GROUP; - if (!this->groups.empty()) this->sel = this->groups[0].group->index; + this->sel = INVALID_GROUP.base(); + if (!this->groups.empty()) this->sel = this->groups[0].group->index.base(); } this->SetDirty(); diff --git a/src/group.h b/src/group.h index 5b74b8c655..32d1132b01 100644 --- a/src/group.h +++ b/src/group.h @@ -17,7 +17,7 @@ #include "engine_type.h" #include "livery.h" -typedef Pool GroupPool; +using GroupPool = Pool; extern GroupPool _group_pool; ///< Pool of groups. /** Statistics and caches on the vehicles in a group. */ diff --git a/src/group_gui.cpp b/src/group_gui.cpp index 14a8ae4605..3d2fc6b651 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -986,7 +986,7 @@ public: void OnQueryTextFinished(std::optional str) override { - if (str.has_value()) Command::Post(STR_ERROR_GROUP_CAN_T_RENAME, AlterGroupMode::Rename, this->group_rename, 0, *str); + if (str.has_value()) Command::Post(STR_ERROR_GROUP_CAN_T_RENAME, AlterGroupMode::Rename, this->group_rename, INVALID_GROUP, *str); this->group_rename = INVALID_GROUP; } diff --git a/src/group_type.h b/src/group_type.h index 32c4926197..09b87fc64a 100644 --- a/src/group_type.h +++ b/src/group_type.h @@ -10,12 +10,13 @@ #ifndef GROUP_TYPE_H #define GROUP_TYPE_H -typedef uint16_t GroupID; ///< Type for all group identifiers. +#include "core/pool_type.hpp" -static const GroupID NEW_GROUP = 0xFFFC; ///< Sentinel for a to-be-created group. -static const GroupID ALL_GROUP = 0xFFFD; ///< All vehicles are in this group. -static const GroupID DEFAULT_GROUP = 0xFFFE; ///< Ungrouped vehicles are in this group. -static const GroupID INVALID_GROUP = 0xFFFF; ///< Sentinel for invalid groups. +using GroupID = PoolID; +static constexpr GroupID NEW_GROUP{0xFFFC}; ///< Sentinel for a to-be-created group. +static constexpr GroupID ALL_GROUP{0xFFFD}; ///< All vehicles are in this group. +static constexpr GroupID DEFAULT_GROUP{0xFFFE}; ///< Ungrouped vehicles are in this group. +static constexpr GroupID INVALID_GROUP = GroupID::Invalid(); ///< Sentinel for invalid groups. static const uint MAX_LENGTH_GROUP_NAME_CHARS = 32; ///< The maximum length of a group name in characters including '\0' diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 572e739882..4374b5d4ca 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -3335,7 +3335,7 @@ bool AfterLoadGame() Company *c = Company::Get(g->owner); if (IsSavegameVersionBefore(SLV_GROUP_NUMBERS)) { /* Use the index as group number when converting old savegames. */ - g->number = c->freegroups.UseID(g->index); + g->number = c->freegroups.UseID(g->index.base()); } else { c->freegroups.UseID(g->number); } diff --git a/src/script/api/script_group.cpp b/src/script/api/script_group.cpp index 5c50713d55..27505e67c7 100644 --- a/src/script/api/script_group.cpp +++ b/src/script/api/script_group.cpp @@ -36,7 +36,7 @@ if (!ScriptObject::Command::Do(&ScriptInstance::DoCommandReturnGroupID, (::VehicleType)vehicle_type, parent_group_id)) return GROUP_INVALID; /* In case of test-mode, we return GroupID 0 */ - return static_cast(0); + return GroupID::Begin(); } /* static */ bool ScriptGroup::DeleteGroup(GroupID group_id) @@ -65,7 +65,7 @@ EnforcePreconditionEncodedText(false, text); EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_GROUP_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG); - return ScriptObject::Command::Do(AlterGroupMode::Rename, group_id, 0, text); + return ScriptObject::Command::Do(AlterGroupMode::Rename, group_id, ::INVALID_GROUP, text); } /* static */ std::optional ScriptGroup::GetName(GroupID group_id)