1
0
Fork 0

Codechange: Use EnumBitSet for RoadStopStatusFlags. (#14068)

pull/14069/head
Peter Nelson 2025-04-21 20:16:23 +01:00 committed by GitHub
parent 81edd1123a
commit 3aa82d5e63
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 38 deletions

View File

@ -436,8 +436,8 @@ uint32_t Station::GetNewGRFVariable(const ResolverObject &object, uint8_t variab
case 0x8A: return this->had_vehicle_of_type; case 0x8A: return this->had_vehicle_of_type;
case 0xF1: return (this->airport.tile != INVALID_TILE) ? this->airport.GetSpec()->ttd_airport_type : ATP_TTDP_LARGE; case 0xF1: return (this->airport.tile != INVALID_TILE) ? this->airport.GetSpec()->ttd_airport_type : ATP_TTDP_LARGE;
case 0xF2: return (this->truck_stops != nullptr) ? this->truck_stops->status : 0; case 0xF2: return (this->truck_stops != nullptr) ? this->truck_stops->status.base() : 0;
case 0xF3: return (this->bus_stops != nullptr) ? this->bus_stops->status : 0; case 0xF3: return (this->bus_stops != nullptr) ? this->bus_stops->status.base() : 0;
case 0xF6: return this->airport.blocks.base(); case 0xF6: return this->airport.blocks.base();
case 0xF7: return GB(this->airport.blocks.base(), 8, 8); case 0xF7: return GB(this->airport.blocks.base(), 8, 8);
} }

View File

@ -26,7 +26,7 @@ INSTANTIATE_POOL_METHODS(RoadStop)
RoadStop::~RoadStop() RoadStop::~RoadStop()
{ {
/* When we are the head we need to free the entries */ /* When we are the head we need to free the entries */
if (HasBit(this->status, RSSFB_BASE_ENTRY)) { if (this->status.Test(RoadStopStatusFlag::BaseEntry)) {
delete this->east; delete this->east;
delete this->west; delete this->west;
} }
@ -86,7 +86,7 @@ void RoadStop::MakeDriveThrough()
if (south && rs_south->east != nullptr) { // (east != nullptr) == (west != nullptr) if (south && rs_south->east != nullptr) { // (east != nullptr) == (west != nullptr)
/* There more southern tiles too, they must 'join' us too */ /* There more southern tiles too, they must 'join' us too */
ClrBit(rs_south->status, RSSFB_BASE_ENTRY); rs_south->status.Reset(RoadStopStatusFlag::BaseEntry);
this->east->occupied += rs_south->east->occupied; this->east->occupied += rs_south->east->occupied;
this->west->occupied += rs_south->west->occupied; this->west->occupied += rs_south->west->occupied;
@ -107,13 +107,13 @@ void RoadStop::MakeDriveThrough()
/* There is one to the south, but not to the north... so we become 'parent' */ /* There is one to the south, but not to the north... so we become 'parent' */
this->east = rs_south->east; this->east = rs_south->east;
this->west = rs_south->west; this->west = rs_south->west;
SetBit(this->status, RSSFB_BASE_ENTRY); this->status.Set(RoadStopStatusFlag::BaseEntry);
ClrBit(rs_south->status, RSSFB_BASE_ENTRY); rs_south->status.Reset(RoadStopStatusFlag::BaseEntry);
} else { } else {
/* We are the only... so we are automatically the master */ /* We are the only... so we are automatically the master */
this->east = new Entry(); this->east = new Entry();
this->west = new Entry(); this->west = new Entry();
SetBit(this->status, RSSFB_BASE_ENTRY); this->status.Set(RoadStopStatusFlag::BaseEntry);
} }
/* Now update the lengths */ /* Now update the lengths */
@ -153,7 +153,7 @@ void RoadStop::ClearDriveThrough()
if (south) { if (south) {
/* There are more southern tiles too, they must be split; /* There are more southern tiles too, they must be split;
* first make the new southern 'base' */ * first make the new southern 'base' */
SetBit(rs_south->status, RSSFB_BASE_ENTRY); rs_south->status.Set(RoadStopStatusFlag::BaseEntry);
rs_south->east = new Entry(); rs_south->east = new Entry();
rs_south->west = new Entry(); rs_south->west = new Entry();
@ -182,7 +182,7 @@ void RoadStop::ClearDriveThrough()
rs_south_base->east->Rebuild(rs_south_base); rs_south_base->east->Rebuild(rs_south_base);
rs_south_base->west->Rebuild(rs_south_base); rs_south_base->west->Rebuild(rs_south_base);
assert(HasBit(rs_north->status, RSSFB_BASE_ENTRY)); assert(rs_north->status.Test(RoadStopStatusFlag::BaseEntry));
rs_north->east->Rebuild(rs_north); rs_north->east->Rebuild(rs_north);
rs_north->west->Rebuild(rs_north); rs_north->west->Rebuild(rs_north);
} else { } else {
@ -192,7 +192,7 @@ void RoadStop::ClearDriveThrough()
} }
} else if (south) { } else if (south) {
/* There is only something to the south. Hand over the base entry */ /* There is only something to the south. Hand over the base entry */
SetBit(rs_south->status, RSSFB_BASE_ENTRY); rs_south->status.Set(RoadStopStatusFlag::BaseEntry);
rs_south->east->length -= TILE_SIZE; rs_south->east->length -= TILE_SIZE;
rs_south->west->length -= TILE_SIZE; rs_south->west->length -= TILE_SIZE;
} else { } else {
@ -202,7 +202,7 @@ void RoadStop::ClearDriveThrough()
} }
/* Make sure we don't get used for something 'incorrect' */ /* Make sure we don't get used for something 'incorrect' */
ClrBit(this->status, RSSFB_BASE_ENTRY); this->status.Reset(RoadStopStatusFlag::BaseEntry);
this->east = nullptr; this->east = nullptr;
this->west = nullptr; this->west = nullptr;
} }
@ -364,7 +364,7 @@ static DiagDirection GetEntryDirection(bool east, Axis axis)
*/ */
void RoadStop::Entry::Rebuild(const RoadStop *rs, int side) void RoadStop::Entry::Rebuild(const RoadStop *rs, int side)
{ {
assert(HasBit(rs->status, RSSFB_BASE_ENTRY)); assert(rs->status.Test(RoadStopStatusFlag::BaseEntry));
Axis axis = GetDriveThroughStopAxis(rs->xy); Axis axis = GetDriveThroughStopAxis(rs->xy);
if (side == -1) side = (rs->east == this); if (side == -1) side = (rs->east == this);
@ -392,7 +392,7 @@ void RoadStop::Entry::Rebuild(const RoadStop *rs, int side)
*/ */
void RoadStop::Entry::CheckIntegrity(const RoadStop *rs) const void RoadStop::Entry::CheckIntegrity(const RoadStop *rs) const
{ {
if (!HasBit(rs->status, RSSFB_BASE_ENTRY)) return; if (!rs->status.Test(RoadStopStatusFlag::BaseEntry)) return;
/* The tile 'before' the road stop must not be part of this 'line' */ /* The tile 'before' the road stop must not be part of this 'line' */
assert(IsDriveThroughStopTile(rs->xy)); assert(IsDriveThroughStopTile(rs->xy));

View File

@ -20,14 +20,13 @@ extern RoadStopPool _roadstop_pool;
/** A Stop for a Road Vehicle */ /** A Stop for a Road Vehicle */
struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> { struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
enum RoadStopStatusFlags : uint8_t { enum class RoadStopStatusFlag : uint8_t {
RSSFB_BAY0_FREE = 0, ///< Non-zero when bay 0 is free Bay0Free = 0, ///< Non-zero when bay 0 is free
RSSFB_BAY1_FREE = 1, ///< Non-zero when bay 1 is free Bay1Free = 1, ///< Non-zero when bay 1 is free
RSSFB_BASE_ENTRY = 6, ///< Non-zero when the entries on this road stop are the primary, i.e. the ones to delete BaseEntry = 6, ///< Non-zero when the entries on this road stop are the primary, i.e. the ones to delete
RSSFB_ENTRY_BUSY = 7, ///< Non-zero when roadstop entry is busy EntryBusy = 7, ///< Non-zero when roadstop entry is busy
}; };
using RoadStopStatusFlags = EnumBitSet<RoadStopStatusFlag, uint8_t>;
static constexpr uint8_t BAY_COUNT = 2; ///< Max. number of bays
/** Container for each entry point of a drive through road stop */ /** Container for each entry point of a drive through road stop */
struct Entry { struct Entry {
@ -65,15 +64,12 @@ struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
void Rebuild(const RoadStop *rs, int side = -1); void Rebuild(const RoadStop *rs, int side = -1);
}; };
uint8_t status = 0; ///< Current status of the Stop, @see RoadStopSatusFlag. Access using *Bay and *Busy functions. RoadStopStatusFlags status{RoadStopStatusFlag::Bay0Free, RoadStopStatusFlag::Bay1Free}; ///< Current status of the Stop. Access using *Bay and *Busy functions.
TileIndex xy = INVALID_TILE; ///< Position on the map TileIndex xy = INVALID_TILE; ///< Position on the map
RoadStop *next = nullptr; ///< Next stop of the given type at this station RoadStop *next = nullptr; ///< Next stop of the given type at this station
/** Initializes a RoadStop */ /** Initializes a RoadStop */
inline RoadStop(TileIndex tile = INVALID_TILE) : inline RoadStop(TileIndex tile = INVALID_TILE) : xy(tile) { }
status((1 << BAY_COUNT) - 1),
xy(tile)
{ }
~RoadStop(); ~RoadStop();
@ -83,7 +79,7 @@ struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
*/ */
inline bool HasFreeBay() const inline bool HasFreeBay() const
{ {
return GB(this->status, 0, BAY_COUNT) != 0; return this->status.Any({RoadStopStatusFlag::Bay0Free, RoadStopStatusFlag::Bay1Free});
} }
/** /**
@ -93,8 +89,11 @@ struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
*/ */
inline bool IsFreeBay(uint nr) const inline bool IsFreeBay(uint nr) const
{ {
assert(nr < BAY_COUNT); switch (nr) {
return HasBit(this->status, nr); case 0: return this->status.Test(RoadStopStatusFlag::Bay0Free);
case 1: return this->status.Test(RoadStopStatusFlag::Bay1Free);
default: NOT_REACHED();
}
} }
/** /**
@ -103,7 +102,7 @@ struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
*/ */
inline bool IsEntranceBusy() const inline bool IsEntranceBusy() const
{ {
return HasBit(this->status, RSSFB_ENTRY_BUSY); return this->status.Test(RoadStopStatusFlag::EntryBusy);
} }
/** /**
@ -112,7 +111,7 @@ struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> {
*/ */
inline void SetEntranceBusy(bool busy) inline void SetEntranceBusy(bool busy)
{ {
SB(this->status, RSSFB_ENTRY_BUSY, 1, busy); this->status.Set(RoadStopStatusFlag::EntryBusy, busy);
} }
/** /**
@ -160,11 +159,11 @@ private:
{ {
assert(this->HasFreeBay()); assert(this->HasFreeBay());
/* Find the first free bay. If the bit is set, the bay is free. */ /* Find the first free bay. */
uint bay_nr = 0; uint bay_nr = 0;
while (!HasBit(this->status, bay_nr)) bay_nr++; while (!this->IsFreeBay(bay_nr)) ++bay_nr;
ClrBit(this->status, bay_nr); this->AllocateDriveThroughBay(bay_nr);
return bay_nr; return bay_nr;
} }
@ -174,8 +173,11 @@ private:
*/ */
inline void AllocateDriveThroughBay(uint nr) inline void AllocateDriveThroughBay(uint nr)
{ {
assert(nr < BAY_COUNT); switch (nr) {
ClrBit(this->status, nr); case 0: this->status.Reset(RoadStopStatusFlag::Bay0Free); break;
case 1: this->status.Reset(RoadStopStatusFlag::Bay1Free); break;
default: NOT_REACHED();
}
} }
/** /**
@ -184,8 +186,11 @@ private:
*/ */
inline void FreeBay(uint nr) inline void FreeBay(uint nr)
{ {
assert(nr < BAY_COUNT); switch (nr) {
SetBit(this->status, nr); case 0: this->status.Set(RoadStopStatusFlag::Bay0Free); break;
case 1: this->status.Set(RoadStopStatusFlag::Bay1Free); break;
default: NOT_REACHED();
}
} }
}; };

View File

@ -149,7 +149,7 @@ void AfterLoadRoadStops()
} }
/* And then rebuild the data in those entries */ /* And then rebuild the data in those entries */
for (RoadStop *rs : RoadStop::Iterate()) { for (RoadStop *rs : RoadStop::Iterate()) {
if (!HasBit(rs->status, RoadStop::RSSFB_BASE_ENTRY)) continue; if (!rs->status.Test(RoadStop::RoadStopStatusFlag::BaseEntry)) continue;
rs->GetEntry(DIAGDIR_NE)->Rebuild(rs); rs->GetEntry(DIAGDIR_NE)->Rebuild(rs);
rs->GetEntry(DIAGDIR_NW)->Rebuild(rs); rs->GetEntry(DIAGDIR_NW)->Rebuild(rs);