From 77d6f6c69fec4296a5350be86eae506becbdca15 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Mon, 19 May 2025 17:11:28 +0100 Subject: [PATCH] Codechange: Make ContentType::State an enum class. (#14279) --- src/console_cmds.cpp | 4 +-- src/network/core/tcp_content.cpp | 10 +++--- src/network/core/tcp_content_type.h | 16 ++++----- src/network/network_content.cpp | 50 ++++++++++++++--------------- src/network/network_content_gui.cpp | 34 ++++++++++---------- src/newgrf_gui.cpp | 2 +- 6 files changed, 58 insertions(+), 58 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index b18cbd1983..ebc6293355 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -2220,7 +2220,7 @@ static void OutputContentState(const ContentInfo &ci) static const std::string_view states[] = { "Not selected", "Selected", "Dep Selected", "Installed", "Unknown" }; static const TextColour state_to_colour[] = { CC_COMMAND, CC_INFO, CC_INFO, CC_WHITE, CC_ERROR }; - IConsolePrint(state_to_colour[ci.state], "{}, {}, {}, {}, {:08X}, {}", ci.id, types[ci.type - 1], states[ci.state], ci.name, ci.unique_id, FormatArrayAsHex(ci.md5sum)); + IConsolePrint(state_to_colour[to_underlying(ci.state)], "{}, {}, {}, {}, {:08X}, {}", ci.id, types[ci.type - 1], states[to_underlying(ci.state)], ci.name, ci.unique_id, FormatArrayAsHex(ci.md5sum)); } static bool ConContent(std::span argv) @@ -2257,7 +2257,7 @@ static bool ConContent(std::span argv) /* List selected content */ IConsolePrint(CC_WHITE, "id, type, state, name"); for (const ContentInfo &ci : _network_content_client.Info()) { - if (ci.state != ContentInfo::SELECTED && ci.state != ContentInfo::AUTOSELECTED) continue; + if (ci.state != ContentInfo::State::Selected && ci.state != ContentInfo::State::Autoselected) continue; OutputContentState(ci); } } else if (StrEqualsIgnoreCase(argv[2], "all")) { diff --git a/src/network/core/tcp_content.cpp b/src/network/core/tcp_content.cpp index 3fe6aa6b7a..4eef5abb01 100644 --- a/src/network/core/tcp_content.cpp +++ b/src/network/core/tcp_content.cpp @@ -32,9 +32,9 @@ bool ContentInfo::IsSelected() const { switch (this->state) { - case ContentInfo::SELECTED: - case ContentInfo::AUTOSELECTED: - case ContentInfo::ALREADY_HERE: + case ContentInfo::State::Selected: + case ContentInfo::State::Autoselected: + case ContentInfo::State::AlreadyHere: return true; default: @@ -48,7 +48,7 @@ bool ContentInfo::IsSelected() const */ bool ContentInfo::IsValid() const { - return this->state < ContentInfo::INVALID && this->type >= CONTENT_TYPE_BEGIN && this->type < CONTENT_TYPE_END; + return this->state < ContentInfo::State::Invalid && this->type >= CONTENT_TYPE_BEGIN && this->type < CONTENT_TYPE_END; } /** @@ -58,7 +58,7 @@ bool ContentInfo::IsValid() const */ std::optional ContentInfo::GetTextfile(TextfileType type) const { - if (this->state == INVALID) return std::nullopt; + if (this->state == ContentInfo::State::Invalid) return std::nullopt; std::optional tmp; switch (this->type) { default: NOT_REACHED(); diff --git a/src/network/core/tcp_content_type.h b/src/network/core/tcp_content_type.h index c61217001d..ac25c241c4 100644 --- a/src/network/core/tcp_content_type.h +++ b/src/network/core/tcp_content_type.h @@ -55,13 +55,13 @@ static constexpr ContentID INVALID_CONTENT_ID = UINT32_MAX; ///< Sentinel for in /** Container for all important information about a piece of content. */ struct ContentInfo { /** The state the content can be in. */ - enum State : uint8_t { - UNSELECTED, ///< The content has not been selected - SELECTED, ///< The content has been manually selected - AUTOSELECTED, ///< The content has been selected as dependency - ALREADY_HERE, ///< The content is already at the client side - DOES_NOT_EXIST, ///< The content does not exist in the content system - INVALID, ///< The content's invalid + enum class State : uint8_t { + Unselected, ///< The content has not been selected + Selected, ///< The content has been manually selected + Autoselected, ///< The content has been selected as dependency + AlreadyHere, ///< The content is already at the client side + DoesNotExist, ///< The content does not exist in the content system + Invalid, ///< The content's invalid }; ContentType type = INVALID_CONTENT_TYPE; ///< Type of content @@ -76,7 +76,7 @@ struct ContentInfo { MD5Hash md5sum; ///< The MD5 checksum std::vector dependencies; ///< The dependencies (unique server side ids) StringList tags; ///< Tags associated with the content - State state = State::UNSELECTED; ///< Whether the content info is selected (for download) + State state = State::Unselected; ///< Whether the content info is selected (for download) bool upgrade = false; ///< This item is an upgrade bool IsSelected() const; diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp index cdbe3de668..68cb8e8d26 100644 --- a/src/network/network_content.cpp +++ b/src/network/network_content.cpp @@ -118,17 +118,17 @@ bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet &p) HasContentProc *proc = GetHasContentProcforContentType(ci->type); if (proc != nullptr) { if (proc(*ci, true)) { - ci->state = ContentInfo::ALREADY_HERE; + ci->state = ContentInfo::State::AlreadyHere; } else { - ci->state = ContentInfo::UNSELECTED; + ci->state = ContentInfo::State::Unselected; if (proc(*ci, false)) ci->upgrade = true; } } else { - ci->state = ContentInfo::UNSELECTED; + ci->state = ContentInfo::State::Unselected; } /* Something we don't have and has filesize 0 does not exist in the system */ - if (ci->state == ContentInfo::UNSELECTED && ci->filesize == 0) ci->state = ContentInfo::DOES_NOT_EXIST; + if (ci->state == ContentInfo::State::Unselected && ci->filesize == 0) ci->state = ContentInfo::State::DoesNotExist; /* Do we already have a stub for this? */ for (const auto &ici : this->infos) { @@ -295,7 +295,7 @@ void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uin ContentIDList content; for (const auto &ci : this->infos) { - if (!ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) continue; + if (!ci->IsSelected() || ci->state == ContentInfo::State::AlreadyHere) continue; content.push_back(ci->id); bytes += ci->filesize; @@ -845,9 +845,9 @@ ContentInfo *ClientNetworkContentSocketHandler::GetContent(ContentID cid) const void ClientNetworkContentSocketHandler::Select(ContentID cid) { ContentInfo *ci = this->GetContent(cid); - if (ci == nullptr || ci->state != ContentInfo::UNSELECTED) return; + if (ci == nullptr || ci->state != ContentInfo::State::Unselected) return; - ci->state = ContentInfo::SELECTED; + ci->state = ContentInfo::State::Selected; this->CheckDependencyState(*ci); } @@ -860,7 +860,7 @@ void ClientNetworkContentSocketHandler::Unselect(ContentID cid) ContentInfo *ci = this->GetContent(cid); if (ci == nullptr || !ci->IsSelected()) return; - ci->state = ContentInfo::UNSELECTED; + ci->state = ContentInfo::State::Unselected; this->CheckDependencyState(*ci); } @@ -868,8 +868,8 @@ void ClientNetworkContentSocketHandler::Unselect(ContentID cid) void ClientNetworkContentSocketHandler::SelectAll() { for (const auto &ci : this->infos) { - if (ci->state == ContentInfo::UNSELECTED) { - ci->state = ContentInfo::SELECTED; + if (ci->state == ContentInfo::State::Unselected) { + ci->state = ContentInfo::State::Selected; this->CheckDependencyState(*ci); } } @@ -879,8 +879,8 @@ void ClientNetworkContentSocketHandler::SelectAll() void ClientNetworkContentSocketHandler::SelectUpgrade() { for (const auto &ci : this->infos) { - if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) { - ci->state = ContentInfo::SELECTED; + if (ci->state == ContentInfo::State::Unselected && ci->upgrade) { + ci->state = ContentInfo::State::Selected; this->CheckDependencyState(*ci); } } @@ -890,7 +890,7 @@ void ClientNetworkContentSocketHandler::SelectUpgrade() void ClientNetworkContentSocketHandler::UnselectAll() { for (const auto &ci : this->infos) { - if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED; + if (ci->IsSelected() && ci->state != ContentInfo::State::AlreadyHere) ci->state = ContentInfo::State::Unselected; } } @@ -898,12 +898,12 @@ void ClientNetworkContentSocketHandler::UnselectAll() void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo &ci) { switch (ci.state) { - case ContentInfo::SELECTED: - case ContentInfo::AUTOSELECTED: + case ContentInfo::State::Selected: + case ContentInfo::State::Autoselected: this->Unselect(ci.id); break; - case ContentInfo::UNSELECTED: + case ContentInfo::State::Unselected: this->Select(ci.id); break; @@ -955,7 +955,7 @@ void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContent */ void ClientNetworkContentSocketHandler::CheckDependencyState(const ContentInfo &ci) { - if (ci.IsSelected() || ci.state == ContentInfo::ALREADY_HERE) { + if (ci.IsSelected() || ci.state == ContentInfo::State::AlreadyHere) { /* Selection is easy; just walk all children and set the * autoselected state. That way we can see what we automatically * selected and thus can unselect when a dependency is removed. */ @@ -963,15 +963,15 @@ void ClientNetworkContentSocketHandler::CheckDependencyState(const ContentInfo & ContentInfo *c = this->GetContent(dependency); if (c == nullptr) { this->DownloadContentInfo(dependency); - } else if (c->state == ContentInfo::UNSELECTED) { - c->state = ContentInfo::AUTOSELECTED; + } else if (c->state == ContentInfo::State::Unselected) { + c->state = ContentInfo::State::Autoselected; this->CheckDependencyState(*c); } } return; } - if (ci.state != ContentInfo::UNSELECTED) return; + if (ci.state != ContentInfo::State::Unselected) return; /* For unselection we need to find the parents of us. We need to * unselect them. After that we unselect all children that we @@ -991,7 +991,7 @@ void ClientNetworkContentSocketHandler::CheckDependencyState(const ContentInfo & DownloadContentInfo(dependency); continue; } - if (c->state != ContentInfo::AUTOSELECTED) continue; + if (c->state != ContentInfo::State::Autoselected) continue; /* Only unselect when WE are the only parent. */ parents.clear(); @@ -1002,7 +1002,7 @@ void ClientNetworkContentSocketHandler::CheckDependencyState(const ContentInfo & bool force_selection = false; for (const ContentInfo *parent_ci : parents) { if (parent_ci->IsSelected()) sel_count++; - if (parent_ci->state == ContentInfo::SELECTED) force_selection = true; + if (parent_ci->state == ContentInfo::State::Selected) force_selection = true; } if (sel_count == 0) { /* Nothing depends on us */ @@ -1018,7 +1018,7 @@ void ClientNetworkContentSocketHandler::CheckDependencyState(const ContentInfo & /* Is there anything that is "force" selected?, if so... we're done. */ for (const ContentInfo *parent_ci : parents) { - if (parent_ci->state != ContentInfo::SELECTED) continue; + if (parent_ci->state != ContentInfo::State::Selected) continue; force_selection = true; break; @@ -1032,7 +1032,7 @@ void ClientNetworkContentSocketHandler::CheckDependencyState(const ContentInfo & * to unselect. Don't do it immediately because it'll do exactly what * we're doing now. */ for (const ContentInfo *parent : parents) { - if (parent->state == ContentInfo::AUTOSELECTED) this->Unselect(parent->id); + if (parent->state == ContentInfo::State::Autoselected) this->Unselect(parent->id); } for (const ContentInfo *parent : parents) { this->CheckDependencyState(*this->GetContent(parent->id)); @@ -1093,7 +1093,7 @@ void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid) { ContentInfo *ci = this->GetContent(cid); if (ci != nullptr) { - ci->state = ContentInfo::ALREADY_HERE; + ci->state = ContentInfo::State::AlreadyHere; } for (size_t i = 0; i < this->callbacks.size(); /* nothing */) { diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index d7c0278b29..abba86f0d6 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -357,7 +357,7 @@ class NetworkContentListWindow : public Window, ContentCallback { bool first = true; for (const ContentInfo *ci : this->content) { - if (ci->state != ContentInfo::DOES_NOT_EXIST) continue; + if (ci->state != ContentInfo::State::DoesNotExist) continue; if (!first) url.push_back(','); first = false; @@ -409,7 +409,7 @@ class NetworkContentListWindow : public Window, ContentCallback { bool all_available = true; for (const ContentInfo &ci : _network_content_client.Info()) { - if (ci.state == ContentInfo::DOES_NOT_EXIST) all_available = false; + if (ci.state == ContentInfo::State::DoesNotExist) all_available = false; this->content.push_back(&ci); } @@ -443,7 +443,7 @@ class NetworkContentListWindow : public Window, ContentCallback { /** Sort content by state. */ static bool StateSorter(const ContentInfo * const &a, const ContentInfo * const &b) { - int r = a->state - b->state; + int r = to_underlying(a->state) - to_underlying(b->state); if (r == 0) return TypeSorter(a, b); return r < 0; } @@ -460,7 +460,7 @@ class NetworkContentListWindow : public Window, ContentCallback { /** Filter content by tags/name */ static bool TagNameFilter(const ContentInfo * const *a, ContentListFilterData &filter) { - if ((*a)->state == ContentInfo::SELECTED || (*a)->state == ContentInfo::AUTOSELECTED) return true; + if ((*a)->state == ContentInfo::State::Selected || (*a)->state == ContentInfo::State::Autoselected) return true; filter.string_filter.ResetState(); for (auto &tag : (*a)->tags) filter.string_filter.AddLine(tag); @@ -474,7 +474,7 @@ class NetworkContentListWindow : public Window, ContentCallback { { if (filter.types.None()) return true; if (filter.types.Test((*a)->type)) return true; - return ((*a)->state == ContentInfo::SELECTED || (*a)->state == ContentInfo::AUTOSELECTED); + return ((*a)->state == ContentInfo::State::Selected || (*a)->state == ContentInfo::State::Autoselected); } /** Filter the content list */ @@ -655,11 +655,11 @@ public: SpriteID sprite; SpriteID pal = PAL_NONE; switch (ci->state) { - case ContentInfo::UNSELECTED: sprite = SPR_BOX_EMPTY; break; - case ContentInfo::SELECTED: sprite = SPR_BOX_CHECKED; break; - case ContentInfo::AUTOSELECTED: sprite = SPR_BOX_CHECKED; break; - case ContentInfo::ALREADY_HERE: sprite = SPR_BLOT; pal = PALETTE_TO_GREEN; break; - case ContentInfo::DOES_NOT_EXIST: sprite = SPR_BLOT; pal = PALETTE_TO_RED; break; + case ContentInfo::State::Unselected: sprite = SPR_BOX_EMPTY; break; + case ContentInfo::State::Selected: sprite = SPR_BOX_CHECKED; break; + case ContentInfo::State::Autoselected: sprite = SPR_BOX_CHECKED; break; + case ContentInfo::State::AlreadyHere: sprite = SPR_BLOT; pal = PALETTE_TO_GREEN; break; + case ContentInfo::State::DoesNotExist: sprite = SPR_BLOT; pal = PALETTE_TO_RED; break; default: NOT_REACHED(); } DrawSpriteIgnorePadding(sprite, pal, {checkbox.left, mr.top, checkbox.right, mr.bottom}, SA_CENTER); @@ -695,7 +695,7 @@ public: if (this->selected == nullptr) return; /* And fill the rest of the details when there's information to place there */ - DrawStringMultiLine(hr.left, hr.right, hr.top + GetCharacterHeight(FS_NORMAL), hr.bottom, STR_CONTENT_DETAIL_SUBTITLE_UNSELECTED + this->selected->state, TC_FROMSTRING, SA_CENTER); + DrawStringMultiLine(hr.left, hr.right, hr.top + GetCharacterHeight(FS_NORMAL), hr.bottom, STR_CONTENT_DETAIL_SUBTITLE_UNSELECTED + to_underlying(this->selected->state), TC_FROMSTRING, SA_CENTER); /* Also show the total download size, so keep some space from the bottom */ tr.bottom -= GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_wide; @@ -758,7 +758,7 @@ public: std::string buf; for (const ContentInfo *ci : tree) { - if (ci == this->selected || ci->state != ContentInfo::SELECTED) continue; + if (ci == this->selected || ci->state != ContentInfo::State::Selected) continue; if (!buf.empty()) buf += list_separator; buf += ci->name; @@ -772,7 +772,7 @@ public: void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override { if (widget >= WID_NCL_TEXTFILE && widget < WID_NCL_TEXTFILE + TFT_CONTENT_END) { - if (this->selected == nullptr || this->selected->state != ContentInfo::ALREADY_HERE) return; + if (this->selected == nullptr || this->selected->state != ContentInfo::State::AlreadyHere) return; ShowContentTextfileWindow(this, (TextfileType)(widget - WID_NCL_TEXTFILE), this->selected); return; @@ -957,12 +957,12 @@ public: bool show_select_upgrade = false; for (const ContentInfo *ci : this->content) { switch (ci->state) { - case ContentInfo::SELECTED: - case ContentInfo::AUTOSELECTED: + case ContentInfo::State::Selected: + case ContentInfo::State::Autoselected: this->filesize_sum += ci->filesize; break; - case ContentInfo::UNSELECTED: + case ContentInfo::State::Unselected: show_select_all = true; show_select_upgrade |= ci->upgrade; break; @@ -979,7 +979,7 @@ public: this->SetWidgetDisabledState(WID_NCL_SELECT_UPDATE, !show_select_upgrade || !this->filter_data.string_filter.IsEmpty()); this->SetWidgetDisabledState(WID_NCL_OPEN_URL, this->selected == nullptr || this->selected->url.empty()); for (TextfileType tft = TFT_CONTENT_BEGIN; tft < TFT_CONTENT_END; tft++) { - this->SetWidgetDisabledState(WID_NCL_TEXTFILE + tft, this->selected == nullptr || this->selected->state != ContentInfo::ALREADY_HERE || !this->selected->GetTextfile(tft).has_value()); + this->SetWidgetDisabledState(WID_NCL_TEXTFILE + tft, this->selected == nullptr || this->selected->state != ContentInfo::State::AlreadyHere || !this->selected->GetTextfile(tft).has_value()); } } }; diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 83d9f1fa6c..40192df904 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -1506,7 +1506,7 @@ void ShowMissingContentWindow(const GRFConfigList &list) auto ci = std::make_unique(); ci->type = CONTENT_TYPE_NEWGRF; - ci->state = ContentInfo::DOES_NOT_EXIST; + ci->state = ContentInfo::State::DoesNotExist; ci->name = c->GetName(); ci->unique_id = std::byteswap(c->ident.grfid); ci->md5sum = c->flags.Test(GRFConfigFlag::Compatible) ? c->original_md5sum : c->ident.md5sum;