1
0
Fork 0

Codechange: replace union with std::variant for ViewportSignKdtreeItem::id

pull/13512/head
Rubidium 2025-02-01 11:41:24 +01:00 committed by rubidium42
parent cd4482b5df
commit 42d6a12874
2 changed files with 15 additions and 39 deletions

View File

@ -1447,7 +1447,7 @@ static void ViewportAddKdtreeSigns(DrawPixelInfo *dpi)
switch (item.type) { switch (item.type) {
case ViewportSignKdtreeItem::VKI_STATION: { case ViewportSignKdtreeItem::VKI_STATION: {
if (!show_stations) break; if (!show_stations) break;
const BaseStation *st = BaseStation::Get(item.id.station); const BaseStation *st = BaseStation::Get(std::get<StationID>(item.id));
/* If no facilities are present the station is a ghost station. */ /* If no facilities are present the station is a ghost station. */
StationFacility facilities = st->facilities; StationFacility facilities = st->facilities;
@ -1464,7 +1464,7 @@ static void ViewportAddKdtreeSigns(DrawPixelInfo *dpi)
case ViewportSignKdtreeItem::VKI_WAYPOINT: { case ViewportSignKdtreeItem::VKI_WAYPOINT: {
if (!show_waypoints) break; if (!show_waypoints) break;
const BaseStation *st = BaseStation::Get(item.id.station); const BaseStation *st = BaseStation::Get(std::get<StationID>(item.id));
/* Don't draw if station is owned by another company and competitor station names are hidden. Stations owned by none are never ignored. */ /* Don't draw if station is owned by another company and competitor station names are hidden. Stations owned by none are never ignored. */
if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break; if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break;
@ -1475,12 +1475,12 @@ static void ViewportAddKdtreeSigns(DrawPixelInfo *dpi)
case ViewportSignKdtreeItem::VKI_TOWN: case ViewportSignKdtreeItem::VKI_TOWN:
if (!show_towns) break; if (!show_towns) break;
towns.push_back(Town::Get(item.id.town)); towns.push_back(Town::Get(std::get<TownID>(item.id)));
break; break;
case ViewportSignKdtreeItem::VKI_SIGN: { case ViewportSignKdtreeItem::VKI_SIGN: {
if (!show_signs) break; if (!show_signs) break;
const Sign *si = Sign::Get(item.id.sign); const Sign *si = Sign::Get(std::get<SignID>(item.id));
/* Don't draw if sign is owned by another company and competitor signs should be hidden. /* Don't draw if sign is owned by another company and competitor signs should be hidden.
* Note: It is intentional that also signs owned by OWNER_NONE are hidden. Bankrupt * Note: It is intentional that also signs owned by OWNER_NONE are hidden. Bankrupt
@ -2297,27 +2297,27 @@ static bool CheckClickOnViewportSign(const Viewport *vp, int x, int y)
switch (item.type) { switch (item.type) {
case ViewportSignKdtreeItem::VKI_STATION: case ViewportSignKdtreeItem::VKI_STATION:
if (!show_stations) break; if (!show_stations) break;
st = BaseStation::Get(item.id.station); st = BaseStation::Get(std::get<StationID>(item.id));
if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break; if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break;
if (CheckClickOnViewportSign(vp, x, y, &st->sign)) last_st = st; if (CheckClickOnViewportSign(vp, x, y, &st->sign)) last_st = st;
break; break;
case ViewportSignKdtreeItem::VKI_WAYPOINT: case ViewportSignKdtreeItem::VKI_WAYPOINT:
if (!show_waypoints) break; if (!show_waypoints) break;
st = BaseStation::Get(item.id.station); st = BaseStation::Get(std::get<StationID>(item.id));
if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break; if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break;
if (CheckClickOnViewportSign(vp, x, y, &st->sign)) last_st = st; if (CheckClickOnViewportSign(vp, x, y, &st->sign)) last_st = st;
break; break;
case ViewportSignKdtreeItem::VKI_TOWN: case ViewportSignKdtreeItem::VKI_TOWN:
if (!show_towns) break; if (!show_towns) break;
t = Town::Get(item.id.town); t = Town::Get(std::get<TownID>(item.id));
if (CheckClickOnViewportSign(vp, x, y, &t->cache.sign)) last_t = t; if (CheckClickOnViewportSign(vp, x, y, &t->cache.sign)) last_t = t;
break; break;
case ViewportSignKdtreeItem::VKI_SIGN: case ViewportSignKdtreeItem::VKI_SIGN:
if (!show_signs) break; if (!show_signs) break;
si = Sign::Get(item.id.sign); si = Sign::Get(std::get<SignID>(item.id));
if (!show_competitors && _local_company != si->owner && si->owner != OWNER_DEITY) break; if (!show_competitors && _local_company != si->owner && si->owner != OWNER_DEITY) break;
if (CheckClickOnViewportSign(vp, x, y, &si->sign)) last_si = si; if (CheckClickOnViewportSign(vp, x, y, &si->sign)) last_si = si;
break; break;
@ -2351,7 +2351,7 @@ ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeStation(StationID id)
{ {
ViewportSignKdtreeItem item; ViewportSignKdtreeItem item;
item.type = VKI_STATION; item.type = VKI_STATION;
item.id.station = id; item.id = id;
const Station *st = Station::Get(id); const Station *st = Station::Get(id);
assert(st->sign.kdtree_valid); assert(st->sign.kdtree_valid);
@ -2368,7 +2368,7 @@ ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeWaypoint(StationID id)
{ {
ViewportSignKdtreeItem item; ViewportSignKdtreeItem item;
item.type = VKI_WAYPOINT; item.type = VKI_WAYPOINT;
item.id.station = id; item.id = id;
const Waypoint *st = Waypoint::Get(id); const Waypoint *st = Waypoint::Get(id);
assert(st->sign.kdtree_valid); assert(st->sign.kdtree_valid);
@ -2385,7 +2385,7 @@ ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeTown(TownID id)
{ {
ViewportSignKdtreeItem item; ViewportSignKdtreeItem item;
item.type = VKI_TOWN; item.type = VKI_TOWN;
item.id.town = id; item.id = id;
const Town *town = Town::Get(id); const Town *town = Town::Get(id);
assert(town->cache.sign.kdtree_valid); assert(town->cache.sign.kdtree_valid);
@ -2402,7 +2402,7 @@ ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeSign(SignID id)
{ {
ViewportSignKdtreeItem item; ViewportSignKdtreeItem item;
item.type = VKI_SIGN; item.type = VKI_SIGN;
item.id.sign = id; item.id = id;
const Sign *sign = Sign::Get(id); const Sign *sign = Sign::Get(id);
assert(sign->sign.kdtree_valid); assert(sign->sign.kdtree_valid);

View File

@ -24,44 +24,20 @@ struct ViewportSignKdtreeItem {
VKI_SIGN, VKI_SIGN,
}; };
ItemType type; ItemType type;
union { std::variant<StationID, TownID, SignID> id;
StationID station;
TownID town;
SignID sign;
} id;
int32_t center; int32_t center;
int32_t top; int32_t top;
bool operator== (const ViewportSignKdtreeItem &other) const bool operator== (const ViewportSignKdtreeItem &other) const
{ {
if (this->type != other.type) return false; if (this->type != other.type) return false;
switch (this->type) { return this->id == other.id;
case VKI_STATION:
case VKI_WAYPOINT:
return this->id.station == other.id.station;
case VKI_TOWN:
return this->id.town == other.id.town;
case VKI_SIGN:
return this->id.sign == other.id.sign;
default:
NOT_REACHED();
}
} }
bool operator< (const ViewportSignKdtreeItem &other) const bool operator< (const ViewportSignKdtreeItem &other) const
{ {
if (this->type != other.type) return this->type < other.type; if (this->type != other.type) return this->type < other.type;
switch (this->type) { return this->id < other.id;
case VKI_STATION:
case VKI_WAYPOINT:
return this->id.station < other.id.station;
case VKI_TOWN:
return this->id.town < other.id.town;
case VKI_SIGN:
return this->id.sign < other.id.sign;
default:
NOT_REACHED();
}
} }
static ViewportSignKdtreeItem MakeStation(StationID id); static ViewportSignKdtreeItem MakeStation(StationID id);