From 667d01372648061a8b81d1def127f1c732d5f068 Mon Sep 17 00:00:00 2001 From: frosch Date: Sat, 22 Mar 2025 20:30:40 +0100 Subject: [PATCH] Codechange: Return name, description and URL as std::string from GRFConfig. (#13868) --- src/misc_gui.cpp | 4 ++-- src/newgrf_config.cpp | 16 ++++++++++------ src/newgrf_config.h | 6 +++--- src/newgrf_gui.cpp | 20 ++++++++++---------- src/tile_cmd.h | 2 +- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index c2aa51d39a..68c4e5c4ff 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -262,8 +262,8 @@ public: } /* NewGRF name */ - if (td.grf != nullptr) { - this->landinfo_data.push_back(GetString(STR_LAND_AREA_INFORMATION_NEWGRF_NAME, td.grf)); + if (td.grf.has_value()) { + this->landinfo_data.push_back(GetString(STR_LAND_AREA_INFORMATION_NEWGRF_NAME, std::move(*td.grf))); } /* Cargo acceptance is displayed in a extra multiline */ diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 62a73c3c80..4b4c979bfc 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -82,28 +82,32 @@ void GRFConfig::CopyParams(const GRFConfig &src) * the filename is returned. * @return The name of filename of this grf. */ -const char *GRFConfig::GetName() const +std::string GRFConfig::GetName() const { const char *name = GetGRFStringFromGRFText(this->name); - return StrEmpty(name) ? this->filename.c_str() : name; + return StrEmpty(name) ? this->filename : name; } /** * Get the grf info. * @return A string with a description of this grf. */ -const char *GRFConfig::GetDescription() const +std::optional GRFConfig::GetDescription() const { - return GetGRFStringFromGRFText(this->info); + const char *str = GetGRFStringFromGRFText(this->info); + if (StrEmpty(str)) return std::nullopt; + return std::string(str); } /** * Get the grf url. * @return A string with an url of this grf. */ -const char *GRFConfig::GetURL() const +std::optional GRFConfig::GetURL() const { - return GetGRFStringFromGRFText(this->url); + const char *str = GetGRFStringFromGRFText(this->url); + if (StrEmpty(str)) return std::nullopt; + return std::string(str); } /** Set the default value for all parameters as specified by action14. */ diff --git a/src/newgrf_config.h b/src/newgrf_config.h index 4b67eb2a0f..b8d50448af 100644 --- a/src/newgrf_config.h +++ b/src/newgrf_config.h @@ -190,9 +190,9 @@ struct GRFConfig { void SetValue(const GRFParameterInfo &info, uint32_t value); std::optional GetTextfile(TextfileType type) const; - const char *GetName() const; - const char *GetDescription() const; - const char *GetURL() const; + std::string GetName() const; + std::optional GetDescription() const; + std::optional GetURL() const; void SetParameterDefaults(); void SetSuitablePalette(); diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 7c8a283a8b..fdc55e6b24 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -128,8 +128,8 @@ static void ShowNewGRFInfo(const GRFConfig &c, const Rect &r, bool show_params) if (c.flags.Test(GRFConfigFlag::Compatible)) tr.top = DrawStringMultiLine(tr, STR_NEWGRF_COMPATIBLE_LOADED); /* Draw GRF info if it exists */ - if (!StrEmpty(c.GetDescription())) { - tr.top = DrawStringMultiLine(tr, GetString(STR_JUST_RAW_STRING, c.GetDescription()), TC_BLACK); + if (auto desc = c.GetDescription(); desc.has_value()) { + tr.top = DrawStringMultiLine(tr, GetString(STR_JUST_RAW_STRING, std::move(*desc)), TC_BLACK); } else { tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_NO_INFO); } @@ -854,7 +854,7 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { int i = 0; for (const auto &c : this->actives) { if (this->vscroll->IsVisible(i)) { - const char *text = c->GetName(); + std::string text = c->GetName(); bool h = (this->active_sel == c.get()); PaletteID pal = this->GetPalette(*c); @@ -871,7 +871,7 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { DrawSprite(SPR_SQUARE, pal, square_left, tr.top + square_offset_y); if (c->error.has_value()) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, tr.top + warning_offset_y); uint txtoffset = !c->error.has_value() ? 0 : warning.width; - DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), tr.top + offset_y, text, h ? TC_WHITE : TC_ORANGE); + DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), tr.top + offset_y, std::move(text), h ? TC_WHITE : TC_ORANGE); tr.top += step_height; } i++; @@ -894,10 +894,10 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { for (auto it = first; it != last; ++it) { const GRFConfig *c = *it; bool h = (c == this->avail_sel); - const char *text = c->GetName(); + std::string text = c->GetName(); if (h) GfxFillRect(br.left, tr.top, br.right, tr.top + step_height - 1, PC_DARK_BLUE); - DrawString(tr.left, tr.right, tr.top + offset_y, text, h ? TC_WHITE : TC_SILVER); + DrawString(tr.left, tr.right, tr.top + offset_y, std::move(text), h ? TC_WHITE : TC_SILVER); tr.top += step_height; } break; @@ -948,8 +948,8 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { case WID_NS_OPEN_URL: { const GRFConfig *c = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel; - - OpenBrowser(c->GetURL()); + auto url = c->GetURL(); + if (url.has_value()) OpenBrowser(std::move(*url)); break; } @@ -1263,7 +1263,7 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { for (TextfileType tft = TFT_CONTENT_BEGIN; tft < TFT_CONTENT_END; tft++) { this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, selected_config == nullptr || !selected_config->GetTextfile(tft).has_value()); } - this->SetWidgetDisabledState(WID_NS_OPEN_URL, selected_config == nullptr || StrEmpty(selected_config->GetURL())); + this->SetWidgetDisabledState(WID_NS_OPEN_URL, selected_config == nullptr || !selected_config->GetURL().has_value()); this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0); this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0); @@ -1413,7 +1413,7 @@ private: filter.ResetState(); filter.AddLine((*a)->GetName()); filter.AddLine((*a)->filename); - filter.AddLine((*a)->GetDescription()); + if (auto desc = (*a)->GetDescription(); desc.has_value()) filter.AddLine(*desc); return filter.GetState();; } diff --git a/src/tile_cmd.h b/src/tile_cmd.h index 562cc5d1dc..59c942a94b 100644 --- a/src/tile_cmd.h +++ b/src/tile_cmd.h @@ -60,7 +60,7 @@ struct TileDesc { StringID airport_class{}; ///< Name of the airport class StringID airport_name{}; ///< Name of the airport StringID airport_tile_name{}; ///< Name of the airport tile - const char *grf = nullptr; ///< newGRF used for the tile contents + std::optional grf = std::nullopt; ///< newGRF used for the tile contents StringID railtype{}; ///< Type of rail on the tile. uint16_t rail_speed = 0; ///< Speed limit of rail (bridges and track) StringID roadtype{}; ///< Type of road on the tile.