From 1f8ff0e4f9895538a98f2a22b2f6b6e62310aae9 Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Sat, 22 May 2021 20:52:24 +0200 Subject: Codechange: make Write_ValidateSetting a function of StringSettingDesc --- src/settings.cpp | 60 ++++++++++++++++++++++++++++++++++++------------- src/settings_internal.h | 5 +++++ 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/settings.cpp b/src/settings.cpp index 8bdfced96..cedfdf5af 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -507,26 +507,25 @@ void IntSettingDesc::Write_ValidateSetting(const void *object, int32 val) const /** * Set the string value of a setting. - * @param ptr Pointer to the std::string. - * @param sd Pointer to the information for the conversions and limitations to apply. - * @param p The string to save. + * @param object The object the setting is to be saved in. + * @param str The string to save. */ -static void Write_ValidateStdString(void *ptr, const SettingDesc *sd, const char *p) +void StringSettingDesc::Write_ValidateSetting(const void *object, const char *str) const { - std::string *dst = reinterpret_cast(ptr); + std::string *dst = reinterpret_cast(GetVariableAddress(object, &this->save)); - switch (GetVarMemType(sd->save.conv)) { + switch (GetVarMemType(this->save.conv)) { case SLE_VAR_STR: case SLE_VAR_STRQ: - if (p != nullptr) { - if (sd->max != 0 && strlen(p) >= sd->max) { + if (str != nullptr) { + if (this->max != 0 && strlen(str) >= this->max) { /* In case a maximum length is imposed by the setting, the length * includes the '\0' termination for network transfer purposes. * Also ensure the string is valid after chopping of some bytes. */ - std::string str(p, sd->max - 1); - dst->assign(str_validate(str, SVS_NONE)); + std::string stdstr(str, this->max - 1); + dst->assign(str_validate(stdstr, SVS_NONE)); } else { - dst->assign(p); + dst->assign(str); } } else { dst->clear(); @@ -603,7 +602,7 @@ static void IniLoadSettings(IniFile *ini, const SettingTable &settings_table, co break; case SDT_STDSTRING: - Write_ValidateStdString(ptr, sd.get(), (const char *)p); + sd->AsStringSetting()->Write_ValidateSetting(object, (const char *)p); break; case SDT_INTLIST: { @@ -843,6 +842,14 @@ bool SettingDesc::IsIntSetting() const { return this->cmd == SDT_BOOLX || this->cmd == SDT_NUMX || this->cmd == SDT_ONEOFMANY || this->cmd == SDT_MANYOFMANY; } +/** + * Check whether this setting is an string type setting. + * @return True when the underlying type is a string. + */ +bool SettingDesc::IsStringSetting() const { + return this->cmd == SDT_STDSTRING; +} + /** * Get the setting description of this setting as an integer setting. * @return The integer setting description. @@ -853,6 +860,16 @@ const IntSettingDesc *SettingDesc::AsIntSetting() const return static_cast(this); } +/** + * Get the setting description of this setting as a string setting. + * @return The string setting description. + */ +const StringSettingDesc *SettingDesc::AsStringSetting() const +{ + assert(this->IsStringSetting()); + return static_cast(this); +} + /* Begin - Callback Functions for the various settings. */ /** Reposition the main toolbar as the setting changed. */ @@ -2105,12 +2122,23 @@ bool SetSettingValue(const SettingDesc *sd, const char *value, bool force_newgam value = nullptr; } - void *ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save); - Write_ValidateStdString(ptr, sd, value); - if (sd->proc != nullptr) sd->proc(0); + const void *object = (_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game; + sd->AsStringSetting()->ChangeValue(object, value); + return true; +} + +/** + * Handle changing a string value. This performs validation of the input value + * and calls the appropriate callbacks, and saves it when the value is changed. + * @param object The object the setting is in. + * @param newval The new value for the setting. + */ +void StringSettingDesc::ChangeValue(const void *object, const char *newval) const +{ + this->Write_ValidateSetting(object, newval); + if (this->proc != nullptr) this->proc(0); if (_save_config) SaveToConfig(); - return true; } /** diff --git a/src/settings_internal.h b/src/settings_internal.h index 7b37ac267..33910e066 100644 --- a/src/settings_internal.h +++ b/src/settings_internal.h @@ -111,7 +111,9 @@ struct SettingDesc { bool IsEditable(bool do_command = false) const; SettingType GetType() const; bool IsIntSetting() const; + bool IsStringSetting() const; const struct IntSettingDesc *AsIntSetting() const; + const struct StringSettingDesc *AsStringSetting() const; /** * Format the value of the setting associated with this object. @@ -144,6 +146,9 @@ struct StringSettingDesc : SettingDesc { SettingDesc(save, name, def, cmd, flags, 0, max_length, 0, nullptr, 0, 0, 0, proc, nullptr, SC_NONE, startup) {} virtual ~StringSettingDesc() {} + void ChangeValue(const void *object, const char *newval) const; + void Write_ValidateSetting(const void *object, const char *str) const; + void FormatValue(char *buf, const char *last, const void *object) const override; const std::string &Read(const void *object) const; }; -- cgit v1.2.3-54-g00ecf