From 6bca9e090dc6637fd7b6ed6a5a37d65f151849e9 Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Tue, 27 Apr 2021 20:58:17 +0200 Subject: [PATCH] Codechange: add SetDParamStr that accepts std::string& --- src/fios.cpp | 2 +- src/fios_gui.cpp | 2 +- src/industry_gui.cpp | 2 +- src/music_gui.cpp | 2 +- src/newgrf_gui.cpp | 12 ++++++------ src/settings.cpp | 2 +- src/settings_gui.cpp | 8 ++++---- src/strings.cpp | 11 +++++++++++ src/strings_func.h | 1 + src/town_gui.cpp | 4 ++-- src/widgets/dropdown.cpp | 2 +- 11 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/fios.cpp b/src/fios.cpp index 3b11bcbe02..c56e505fa0 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -392,7 +392,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c fios->mtime = 0; strecpy(fios->name, d_name, lastof(fios->name)); std::string dirname = std::string(d_name) + PATHSEP; - SetDParamStr(0, dirname.c_str()); + SetDParamStr(0, dirname); GetString(fios->title, STR_SAVELOAD_DIRECTORY, lastof(fios->title)); str_validate(fios->title, lastof(fios->title)); } diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 8de6235d62..e4de1acaaa 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -537,7 +537,7 @@ public: const CompanyProperties &c = *pair.second; if (!c.name.empty()) { SetDParam(1, STR_JUST_RAW_STRING); - SetDParamStr(2, c.name.c_str()); + SetDParamStr(2, c.name); } else { SetDParam(1, c.name_1); SetDParam(2, c.name_2); diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index aa4d86da6c..d0c916ef7c 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -972,7 +972,7 @@ public: } if (!i->text.empty()) { - SetDParamStr(0, i->text.c_str()); + SetDParamStr(0, i->text); y += WD_PAR_VSEP_WIDE; y = DrawStringMultiLine(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK); } diff --git a/src/music_gui.cpp b/src/music_gui.cpp index 28cb6ce4e1..16245a0b24 100644 --- a/src/music_gui.cpp +++ b/src/music_gui.cpp @@ -462,7 +462,7 @@ struct MusicTrackSelectionWindow : public Window { SetDParam(0, STR_MUSIC_PLAYLIST_ALL + _settings_client.music.playlist); break; case WID_MTS_CAPTION: - SetDParamStr(0, BaseMusic::GetUsedSet()->name.c_str()); + SetDParamStr(0, BaseMusic::GetUsedSet()->name); break; } } diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 6316a7c4bf..9e86a575f0 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -51,9 +51,9 @@ void ShowNewGRFError() if (c->error == nullptr || (c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL && c->error->severity != STR_NEWGRF_ERROR_MSG_ERROR)) continue; SetDParam (0, c->error->message != STR_NULL ? c->error->message : STR_JUST_RAW_STRING); - SetDParamStr(1, c->error->custom_message.c_str()); + SetDParamStr(1, c->error->custom_message); SetDParamStr(2, c->filename); - SetDParamStr(3, c->error->data.c_str()); + SetDParamStr(3, c->error->data); for (uint i = 0; i < lengthof(c->error->param_value); i++) { SetDParam(4 + i, c->error->param_value[i]); } @@ -70,9 +70,9 @@ static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint right, uint { if (c->error != nullptr) { char message[512]; - SetDParamStr(0, c->error->custom_message.c_str()); // is skipped by built-in messages + SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages SetDParamStr(1, c->filename); - SetDParamStr(2, c->error->data.c_str()); + SetDParamStr(2, c->error->data); for (uint i = 0; i < lengthof(c->error->param_value); i++) { SetDParam(3 + i, c->error->param_value[i]); } @@ -750,7 +750,7 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { case WID_NS_PRESET_LIST: { Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM); for (const auto &i : this->grf_presets) { - SetDParamStr(0, i.c_str()); + SetDParamStr(0, i); d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING)); } d.width += padding.width; @@ -783,7 +783,7 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { SetDParam(0, STR_NUM_CUSTOM); } else { SetDParam(0, STR_JUST_RAW_STRING); - SetDParamStr(1, this->grf_presets[this->preset].c_str()); + SetDParamStr(1, this->grf_presets[this->preset]); } break; } diff --git a/src/settings.cpp b/src/settings.cpp index c0b0dd8edd..855bedd18d 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1631,7 +1631,7 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNKNOWN); } - SetDParamStr(0, StrEmpty(filename) ? item->name.c_str() : filename); + SetDParamStr(0, StrEmpty(filename) ? item->name : filename); ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_GRF, WL_CRITICAL); delete c; continue; diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 34c073fe92..9935d596c0 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -123,7 +123,7 @@ struct BaseSetTextfileWindow : public TextfileWindow { { if (widget == WID_TF_CAPTION) { SetDParam(0, content_type); - SetDParamStr(1, this->baseset->name.c_str()); + SetDParamStr(1, this->baseset->name); } } }; @@ -304,10 +304,10 @@ struct GameOptionsWindow : Window { case WID_GO_LANG_DROPDOWN: SetDParamStr(0, _current_language->own_name); break; case WID_GO_GUI_ZOOM_DROPDOWN: SetDParam(0, _gui_zoom_dropdown[_gui_zoom_cfg != ZOOM_LVL_CFG_AUTO ? ZOOM_LVL_OUT_4X - _gui_zoom_cfg + 1 : 0]); break; case WID_GO_FONT_ZOOM_DROPDOWN: SetDParam(0, _font_zoom_dropdown[_font_zoom_cfg != ZOOM_LVL_CFG_AUTO ? ZOOM_LVL_OUT_4X - _font_zoom_cfg + 1 : 0]); break; - case WID_GO_BASE_GRF_DROPDOWN: SetDParamStr(0, BaseGraphics::GetUsedSet()->name.c_str()); break; + case WID_GO_BASE_GRF_DROPDOWN: SetDParamStr(0, BaseGraphics::GetUsedSet()->name); break; case WID_GO_BASE_GRF_STATUS: SetDParam(0, BaseGraphics::GetUsedSet()->GetNumInvalid()); break; - case WID_GO_BASE_SFX_DROPDOWN: SetDParamStr(0, BaseSounds::GetUsedSet()->name.c_str()); break; - case WID_GO_BASE_MUSIC_DROPDOWN: SetDParamStr(0, BaseMusic::GetUsedSet()->name.c_str()); break; + case WID_GO_BASE_SFX_DROPDOWN: SetDParamStr(0, BaseSounds::GetUsedSet()->name); break; + case WID_GO_BASE_MUSIC_DROPDOWN: SetDParamStr(0, BaseMusic::GetUsedSet()->name); break; case WID_GO_BASE_MUSIC_STATUS: SetDParam(0, BaseMusic::GetUsedSet()->GetNumInvalid()); break; case WID_GO_REFRESH_RATE_DROPDOWN: SetDParam(0, _settings_client.gui.refresh_rate); break; case WID_GO_RESOLUTION_DROPDOWN: { diff --git a/src/strings.cpp b/src/strings.cpp index 0489714590..2b6dbc3ef9 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -296,6 +296,17 @@ void SetDParamStr(uint n, const char *str) SetDParam(n, (uint64)(size_t)str); } +/** + * This function is used to "bind" the C string of a std::string to a OpenTTD dparam slot. + * The caller has to ensure that the std::string reference remains valid while the string is shown. + * @param n slot of the string + * @param str string to bind + */ +void SetDParamStr(uint n, const std::string &str) +{ + SetDParamStr(n, str.c_str()); +} + /** * Shift the string parameters in the global string parameter array by \a amount positions, making room at the beginning. * @param amount Number of positions to shift. diff --git a/src/strings_func.h b/src/strings_func.h index e4b7791052..be59b091a2 100644 --- a/src/strings_func.h +++ b/src/strings_func.h @@ -205,6 +205,7 @@ void SetDParamMaxValue(uint n, uint64 max_value, uint min_count = 0, FontSize si void SetDParamMaxDigits(uint n, uint count, FontSize size = FS_NORMAL); void SetDParamStr(uint n, const char *str); +void SetDParamStr(uint n, const std::string &str); void CopyInDParam(int offs, const uint64 *src, int num); void CopyOutDParam(uint64 *dst, int offs, int num); diff --git a/src/town_gui.cpp b/src/town_gui.cpp index a189c7ccb3..22f8b757f8 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -435,7 +435,7 @@ public: } if (!this->town->text.empty()) { - SetDParamStr(0, this->town->text.c_str()); + SetDParamStr(0, this->town->text); DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK); } } @@ -517,7 +517,7 @@ public: if (_settings_game.economy.station_noise_level) aimed_height += FONT_HEIGHT_NORMAL; if (!this->town->text.empty()) { - SetDParamStr(0, this->town->text.c_str()); + SetDParamStr(0, this->town->text); aimed_height += GetStringHeight(STR_JUST_RAW_STRING, width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT); } diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp index 8f4a8634ee..4b89b48092 100644 --- a/src/widgets/dropdown.cpp +++ b/src/widgets/dropdown.cpp @@ -65,7 +65,7 @@ StringID DropDownListParamStringItem::String() const StringID DropDownListCharStringItem::String() const { - SetDParamStr(0, this->raw_string.c_str()); + SetDParamStr(0, this->raw_string); return this->string; }