1
0
Fork 0

Codechange: Use EnumBitSet for GroupFlags.

pull/11338/merge
Peter Nelson 2025-01-29 17:47:50 +00:00 committed by Peter Nelson
parent 4b573b2703
commit 5ef495da78
9 changed files with 25 additions and 26 deletions

View File

@ -65,7 +65,7 @@ void RemoveAllEngineReplacement(EngineRenewList *erl)
EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group, bool *replace_when_old) EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group, bool *replace_when_old)
{ {
const EngineRenew *er = GetEngineReplacement(erl, engine, group); const EngineRenew *er = GetEngineReplacement(erl, engine, group);
if (er == nullptr && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !HasFlag(Group::Get(group)->flags, GroupFlags::ReplaceProtection)))) { if (er == nullptr && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !Group::Get(group)->flags.Test(GroupFlag::ReplaceProtection)))) {
/* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */ /* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */
er = GetEngineReplacement(erl, engine, ALL_GROUP); er = GetEngineReplacement(erl, engine, ALL_GROUP);
} }

View File

@ -754,7 +754,7 @@ CommandCost CmdAutoreplaceVehicle(DoCommandFlag flags, VehicleID veh_id)
bool wagon_removal = c->settings.renew_keep_length; bool wagon_removal = c->settings.renew_keep_length;
const Group *g = Group::GetIfValid(v->group_id); const Group *g = Group::GetIfValid(v->group_id);
if (g != nullptr) wagon_removal = HasFlag(g->flags, GroupFlags::ReplaceWagonRemoval); if (g != nullptr) wagon_removal = g->flags.Test(GroupFlag::ReplaceWagonRemoval);
/* Test whether any replacement is set, before issuing a whole lot of commands that would end in nothing changed */ /* Test whether any replacement is set, before issuing a whole lot of commands that would end in nothing changed */
Vehicle *w = v; Vehicle *w = v;

View File

@ -405,7 +405,7 @@ public:
bool remove_wagon; bool remove_wagon;
const Group *g = Group::GetIfValid(this->sel_group); const Group *g = Group::GetIfValid(this->sel_group);
if (g != nullptr) { if (g != nullptr) {
remove_wagon = HasFlag(g->flags, GroupFlags::ReplaceWagonRemoval); remove_wagon = g->flags.Test(GroupFlag::ReplaceWagonRemoval);
SetDParam(0, STR_GROUP_NAME); SetDParam(0, STR_GROUP_NAME);
SetDParam(1, sel_group); SetDParam(1, sel_group);
} else { } else {
@ -554,7 +554,7 @@ public:
case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: { case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
const Group *g = Group::GetIfValid(this->sel_group); const Group *g = Group::GetIfValid(this->sel_group);
if (g != nullptr) { if (g != nullptr) {
Command<CMD_SET_GROUP_FLAG>::Post(this->sel_group, GroupFlags::ReplaceWagonRemoval, !HasFlag(g->flags, GroupFlags::ReplaceWagonRemoval), _ctrl_pressed); Command<CMD_SET_GROUP_FLAG>::Post(this->sel_group, GroupFlag::ReplaceWagonRemoval, !g->flags.Test(GroupFlag::ReplaceWagonRemoval), _ctrl_pressed);
} else { } else {
// toggle renew_keep_length // toggle renew_keep_length
Command<CMD_CHANGE_COMPANY_SETTING>::Post("company.renew_keep_length", Company::Get(_local_company)->settings.renew_keep_length ? 0 : 1); Command<CMD_CHANGE_COMPANY_SETTING>::Post("company.renew_keep_length", Company::Get(_local_company)->settings.renew_keep_length ? 0 : 1);

View File

@ -62,12 +62,11 @@ struct GroupStatistics {
static void UpdateAutoreplace(CompanyID company); static void UpdateAutoreplace(CompanyID company);
}; };
enum class GroupFlags : uint8_t { enum class GroupFlag : uint8_t {
None = 0, ReplaceProtection = 0, ///< If set, the global autoreplace has no effect on the group
ReplaceProtection = 1U << 0, ///< If set, the global autoreplace has no effect on the group ReplaceWagonRemoval = 1, ///< If set, autoreplace will perform wagon removal on vehicles in this group.
ReplaceWagonRemoval = 1U << 1, ///< If set, autoreplace will perform wagon removal on vehicles in this group.
}; };
DECLARE_ENUM_AS_BIT_SET(GroupFlags) using GroupFlags = EnumBitSet<GroupFlag, uint8_t>;
/** Group data. */ /** Group data. */
struct Group : GroupPool::PoolItem<&_group_pool> { struct Group : GroupPool::PoolItem<&_group_pool> {
@ -75,7 +74,7 @@ struct Group : GroupPool::PoolItem<&_group_pool> {
Owner owner; ///< Group Owner Owner owner; ///< Group Owner
VehicleType vehicle_type; ///< Vehicle type of the group VehicleType vehicle_type; ///< Vehicle type of the group
GroupFlags flags = GroupFlags::None; ///< Group flags GroupFlags flags{}; ///< Group flags
Livery livery; ///< Custom colour scheme for vehicles in this group Livery livery; ///< Custom colour scheme for vehicles in this group
GroupStatistics statistics; ///< NOSAVE: Statistics and caches on the vehicles in the group. GroupStatistics statistics; ///< NOSAVE: Statistics and caches on the vehicles in the group.

View File

@ -355,7 +355,7 @@ std::tuple<CommandCost, GroupID> CmdCreateGroup(DoCommandFlag flags, VehicleType
if (pg == nullptr) { if (pg == nullptr) {
g->livery.colour1 = c->livery[LS_DEFAULT].colour1; g->livery.colour1 = c->livery[LS_DEFAULT].colour1;
g->livery.colour2 = c->livery[LS_DEFAULT].colour2; g->livery.colour2 = c->livery[LS_DEFAULT].colour2;
if (c->settings.renew_keep_length) g->flags |= GroupFlags::ReplaceWagonRemoval; if (c->settings.renew_keep_length) g->flags.Set(GroupFlag::ReplaceWagonRemoval);
} else { } else {
g->parent = pg->index; g->parent = pg->index;
g->livery.colour1 = pg->livery.colour1; g->livery.colour1 = pg->livery.colour1;
@ -695,12 +695,12 @@ CommandCost CmdSetGroupLivery(DoCommandFlag flags, GroupID group_id, bool primar
* @param g initial group. * @param g initial group.
* @param set 1 to set or 0 to clear protection. * @param set 1 to set or 0 to clear protection.
*/ */
static void SetGroupFlag(Group *g, GroupFlags flag, bool set, bool children) static void SetGroupFlag(Group *g, GroupFlag flag, bool set, bool children)
{ {
if (set) { if (set) {
g->flags |= flag; g->flags.Set(flag);
} else { } else {
g->flags &= ~flag; g->flags.Reset(flag);
} }
if (!children) return; if (!children) return;
@ -719,12 +719,12 @@ static void SetGroupFlag(Group *g, GroupFlags flag, bool set, bool children)
* @param recursive to apply to sub-groups. * @param recursive to apply to sub-groups.
* @return the cost of this operation or an error * @return the cost of this operation or an error
*/ */
CommandCost CmdSetGroupFlag(DoCommandFlag flags, GroupID group_id, GroupFlags flag, bool value, bool recursive) CommandCost CmdSetGroupFlag(DoCommandFlag flags, GroupID group_id, GroupFlag flag, bool value, bool recursive)
{ {
Group *g = Group::GetIfValid(group_id); Group *g = Group::GetIfValid(group_id);
if (g == nullptr || g->owner != _current_company) return CMD_ERROR; if (g == nullptr || g->owner != _current_company) return CMD_ERROR;
if (flag != GroupFlags::ReplaceProtection && flag != GroupFlags::ReplaceWagonRemoval) return CMD_ERROR; if (flag != GroupFlag::ReplaceProtection && flag != GroupFlag::ReplaceWagonRemoval) return CMD_ERROR;
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
SetGroupFlag(g, flag, value, recursive); SetGroupFlag(g, flag, value, recursive);

View File

@ -17,7 +17,7 @@
#include "vehiclelist_cmd.h" #include "vehiclelist_cmd.h"
enum Colours : uint8_t; enum Colours : uint8_t;
enum class GroupFlags : uint8_t; enum class GroupFlag : uint8_t;
/** Action for \c CmdAlterGroup. */ /** Action for \c CmdAlterGroup. */
enum class AlterGroupMode : uint8_t { enum class AlterGroupMode : uint8_t {
@ -31,7 +31,7 @@ CommandCost CmdDeleteGroup(DoCommandFlag flags, GroupID group_id);
std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared, const VehicleListIdentifier &vli); std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared, const VehicleListIdentifier &vli);
CommandCost CmdAddSharedVehicleGroup(DoCommandFlag flags, GroupID id_g, VehicleType type); CommandCost CmdAddSharedVehicleGroup(DoCommandFlag flags, GroupID id_g, VehicleType type);
CommandCost CmdRemoveAllVehiclesGroup(DoCommandFlag flags, GroupID group_id); CommandCost CmdRemoveAllVehiclesGroup(DoCommandFlag flags, GroupID group_id);
CommandCost CmdSetGroupFlag(DoCommandFlag flags, GroupID group_id, GroupFlags flag, bool value, bool recursive); CommandCost CmdSetGroupFlag(DoCommandFlag flags, GroupID group_id, GroupFlag flag, bool value, bool recursive);
CommandCost CmdSetGroupLivery(DoCommandFlag flags, GroupID group_id, bool primary, Colours colour); CommandCost CmdSetGroupLivery(DoCommandFlag flags, GroupID group_id, bool primary, Colours colour);
DEF_CMD_TRAIT(CMD_CREATE_GROUP, CmdCreateGroup, 0, CMDT_ROUTE_MANAGEMENT) DEF_CMD_TRAIT(CMD_CREATE_GROUP, CmdCreateGroup, 0, CMDT_ROUTE_MANAGEMENT)

View File

@ -584,7 +584,7 @@ public:
/* If not a default group and the group has replace protection, show an enabled replace sprite. */ /* If not a default group and the group has replace protection, show an enabled replace sprite. */
uint16_t protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN; uint16_t protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN;
if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && HasFlag(Group::Get(this->vli.index)->flags, GroupFlags::ReplaceProtection)) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN; if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && Group::Get(this->vli.index)->flags.Test(GroupFlag::ReplaceProtection)) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN;
this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->SetSprite(protect_sprite + this->vli.vtype); this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->SetSprite(protect_sprite + this->vli.vtype);
/* Set text of "group by" dropdown widget. */ /* Set text of "group by" dropdown widget. */
@ -650,7 +650,7 @@ public:
assert(g->owner == this->owner); assert(g->owner == this->owner);
DrawGroupInfo(y1, r.left, r.right, g->index, it->level_mask, it->indent, HasFlag(g->flags, GroupFlags::ReplaceProtection), g->folded || (std::next(it) != std::end(this->groups) && std::next(it)->indent > it->indent)); DrawGroupInfo(y1, r.left, r.right, g->index, it->level_mask, it->indent, g->flags.Test(GroupFlag::ReplaceProtection), g->folded || (std::next(it) != std::end(this->groups) && std::next(it)->indent > it->indent));
y1 += this->tiny_step_height; y1 += this->tiny_step_height;
} }
@ -868,7 +868,7 @@ public:
case WID_GL_REPLACE_PROTECTION: { case WID_GL_REPLACE_PROTECTION: {
const Group *g = Group::GetIfValid(this->vli.index); const Group *g = Group::GetIfValid(this->vli.index);
if (g != nullptr) { if (g != nullptr) {
Command<CMD_SET_GROUP_FLAG>::Post(this->vli.index, GroupFlags::ReplaceProtection, !HasFlag(g->flags, GroupFlags::ReplaceProtection), _ctrl_pressed); Command<CMD_SET_GROUP_FLAG>::Post(this->vli.index, GroupFlag::ReplaceProtection, !g->flags.Test(GroupFlag::ReplaceProtection), _ctrl_pressed);
} }
break; break;
} }

View File

@ -3201,11 +3201,11 @@ bool AfterLoadGame()
if (c->settings.renew_keep_length) SetBit(wagon_removal, c->index); if (c->settings.renew_keep_length) SetBit(wagon_removal, c->index);
} }
for (Group *g : Group::Iterate()) { for (Group *g : Group::Iterate()) {
if (to_underlying(g->flags) != 0) { if (g->flags != GroupFlags{}) {
/* Convert old replace_protection value to flag. */ /* Convert old replace_protection value to flag. */
g->flags = GroupFlags::ReplaceProtection; g->flags = GroupFlag::ReplaceProtection;
} }
if (HasBit(wagon_removal, g->owner)) g->flags |= GroupFlags::ReplaceWagonRemoval; if (HasBit(wagon_removal, g->owner)) g->flags.Set(GroupFlag::ReplaceWagonRemoval);
} }
} }

View File

@ -98,14 +98,14 @@
EnforceCompanyModeValid(false); EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidGroup(group_id)); EnforcePrecondition(false, IsValidGroup(group_id));
return ScriptObject::Command<CMD_SET_GROUP_FLAG>::Do(group_id, GroupFlags::ReplaceProtection, enable, false); return ScriptObject::Command<CMD_SET_GROUP_FLAG>::Do(group_id, GroupFlag::ReplaceProtection, enable, false);
} }
/* static */ bool ScriptGroup::GetAutoReplaceProtection(GroupID group_id) /* static */ bool ScriptGroup::GetAutoReplaceProtection(GroupID group_id)
{ {
if (!IsValidGroup(group_id)) return false; if (!IsValidGroup(group_id)) return false;
return HasFlag(::Group::Get(group_id)->flags, GroupFlags::ReplaceProtection); return ::Group::Get(group_id)->flags.Test(GroupFlag::ReplaceProtection);
} }
/* static */ SQInteger ScriptGroup::GetNumEngines(GroupID group_id, EngineID engine_id) /* static */ SQInteger ScriptGroup::GetNumEngines(GroupID group_id, EngineID engine_id)