From 42d6a12874313501ca81caa89e958850a737451f Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 1 Feb 2025 11:41:24 +0100 Subject: [PATCH] Codechange: replace union with std::variant for ViewportSignKdtreeItem::id --- src/viewport.cpp | 24 ++++++++++++------------ src/viewport_kdtree.h | 30 +++--------------------------- 2 files changed, 15 insertions(+), 39 deletions(-) diff --git a/src/viewport.cpp b/src/viewport.cpp index d0010c2bf0..4b1132c4c8 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -1447,7 +1447,7 @@ static void ViewportAddKdtreeSigns(DrawPixelInfo *dpi) switch (item.type) { case ViewportSignKdtreeItem::VKI_STATION: { if (!show_stations) break; - const BaseStation *st = BaseStation::Get(item.id.station); + const BaseStation *st = BaseStation::Get(std::get(item.id)); /* If no facilities are present the station is a ghost station. */ StationFacility facilities = st->facilities; @@ -1464,7 +1464,7 @@ static void ViewportAddKdtreeSigns(DrawPixelInfo *dpi) case ViewportSignKdtreeItem::VKI_WAYPOINT: { if (!show_waypoints) break; - const BaseStation *st = BaseStation::Get(item.id.station); + const BaseStation *st = BaseStation::Get(std::get(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. */ 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: if (!show_towns) break; - towns.push_back(Town::Get(item.id.town)); + towns.push_back(Town::Get(std::get(item.id))); break; case ViewportSignKdtreeItem::VKI_SIGN: { if (!show_signs) break; - const Sign *si = Sign::Get(item.id.sign); + const Sign *si = Sign::Get(std::get(item.id)); /* 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 @@ -2297,27 +2297,27 @@ static bool CheckClickOnViewportSign(const Viewport *vp, int x, int y) switch (item.type) { case ViewportSignKdtreeItem::VKI_STATION: if (!show_stations) break; - st = BaseStation::Get(item.id.station); + st = BaseStation::Get(std::get(item.id)); if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break; if (CheckClickOnViewportSign(vp, x, y, &st->sign)) last_st = st; break; case ViewportSignKdtreeItem::VKI_WAYPOINT: if (!show_waypoints) break; - st = BaseStation::Get(item.id.station); + st = BaseStation::Get(std::get(item.id)); if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break; if (CheckClickOnViewportSign(vp, x, y, &st->sign)) last_st = st; break; case ViewportSignKdtreeItem::VKI_TOWN: if (!show_towns) break; - t = Town::Get(item.id.town); + t = Town::Get(std::get(item.id)); if (CheckClickOnViewportSign(vp, x, y, &t->cache.sign)) last_t = t; break; case ViewportSignKdtreeItem::VKI_SIGN: if (!show_signs) break; - si = Sign::Get(item.id.sign); + si = Sign::Get(std::get(item.id)); if (!show_competitors && _local_company != si->owner && si->owner != OWNER_DEITY) break; if (CheckClickOnViewportSign(vp, x, y, &si->sign)) last_si = si; break; @@ -2351,7 +2351,7 @@ ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeStation(StationID id) { ViewportSignKdtreeItem item; item.type = VKI_STATION; - item.id.station = id; + item.id = id; const Station *st = Station::Get(id); assert(st->sign.kdtree_valid); @@ -2368,7 +2368,7 @@ ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeWaypoint(StationID id) { ViewportSignKdtreeItem item; item.type = VKI_WAYPOINT; - item.id.station = id; + item.id = id; const Waypoint *st = Waypoint::Get(id); assert(st->sign.kdtree_valid); @@ -2385,7 +2385,7 @@ ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeTown(TownID id) { ViewportSignKdtreeItem item; item.type = VKI_TOWN; - item.id.town = id; + item.id = id; const Town *town = Town::Get(id); assert(town->cache.sign.kdtree_valid); @@ -2402,7 +2402,7 @@ ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeSign(SignID id) { ViewportSignKdtreeItem item; item.type = VKI_SIGN; - item.id.sign = id; + item.id = id; const Sign *sign = Sign::Get(id); assert(sign->sign.kdtree_valid); diff --git a/src/viewport_kdtree.h b/src/viewport_kdtree.h index 141315b6bd..3d3330e067 100644 --- a/src/viewport_kdtree.h +++ b/src/viewport_kdtree.h @@ -24,44 +24,20 @@ struct ViewportSignKdtreeItem { VKI_SIGN, }; ItemType type; - union { - StationID station; - TownID town; - SignID sign; - } id; + std::variant id; int32_t center; int32_t top; bool operator== (const ViewportSignKdtreeItem &other) const { if (this->type != other.type) return false; - switch (this->type) { - 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(); - } + return this->id == other.id; } bool operator< (const ViewportSignKdtreeItem &other) const { if (this->type != other.type) return this->type < other.type; - switch (this->type) { - 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(); - } + return this->id < other.id; } static ViewportSignKdtreeItem MakeStation(StationID id);