summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-05-22 20:52:24 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-05-27 18:49:43 +0200
commit1f8ff0e4f9895538a98f2a22b2f6b6e62310aae9 (patch)
treebb153f443726184fe787b404a37d2bbc22bfcda4 /src
parentbe28c95b30eb433de5e7b9831657af551ef236f9 (diff)
downloadopenttd-1f8ff0e4f9895538a98f2a22b2f6b6e62310aae9.tar.xz
Codechange: make Write_ValidateSetting a function of StringSettingDesc
Diffstat (limited to 'src')
-rw-r--r--src/settings.cpp60
-rw-r--r--src/settings_internal.h5
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<std::string *>(ptr);
+ std::string *dst = reinterpret_cast<std::string *>(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: {
@@ -844,6 +843,14 @@ bool SettingDesc::IsIntSetting() const {
}
/**
+ * 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<const IntSettingDesc *>(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<const StringSettingDesc *>(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;
};