Codechange: strongly type GroupID

This commit is contained in:
Rubidium 2025-01-19 17:52:52 +01:00 committed by rubidium42
parent 798e9f229c
commit 1e24b41f22
7 changed files with 21 additions and 20 deletions

View File

@ -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;

View File

@ -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<CMD_SET_GROUP_LIVERY>::Post(this->sel, widget == WID_SCL_PRI_COL_DROPDOWN, colour);
Command<CMD_SET_GROUP_LIVERY>::Post(static_cast<GroupID>(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();

View File

@ -17,7 +17,7 @@
#include "engine_type.h"
#include "livery.h"
typedef Pool<Group, GroupID, 16, 64000> GroupPool;
using GroupPool = Pool<Group, GroupID, 16, GroupID::End().base()>;
extern GroupPool _group_pool; ///< Pool of groups.
/** Statistics and caches on the vehicles in a group. */

View File

@ -986,7 +986,7 @@ public:
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (str.has_value()) Command<CMD_ALTER_GROUP>::Post(STR_ERROR_GROUP_CAN_T_RENAME, AlterGroupMode::Rename, this->group_rename, 0, *str);
if (str.has_value()) Command<CMD_ALTER_GROUP>::Post(STR_ERROR_GROUP_CAN_T_RENAME, AlterGroupMode::Rename, this->group_rename, INVALID_GROUP, *str);
this->group_rename = INVALID_GROUP;
}

View File

@ -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<uint16_t, struct GroupIDTag, 64000, 0xFFFF>;
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'

View File

@ -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);
}

View File

@ -36,7 +36,7 @@
if (!ScriptObject::Command<CMD_CREATE_GROUP>::Do(&ScriptInstance::DoCommandReturnGroupID, (::VehicleType)vehicle_type, parent_group_id)) return GROUP_INVALID;
/* In case of test-mode, we return GroupID 0 */
return static_cast<GroupID>(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<CMD_ALTER_GROUP>::Do(AlterGroupMode::Rename, group_id, 0, text);
return ScriptObject::Command<CMD_ALTER_GROUP>::Do(AlterGroupMode::Rename, group_id, ::INVALID_GROUP, text);
}
/* static */ std::optional<std::string> ScriptGroup::GetName(GroupID group_id)