mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-06-25 22:49:46 +01:00
Codechange: make Write_ValidateSetting a function of StringSettingDesc
This commit is contained in:
parent
be28c95b30
commit
1f8ff0e4f9
@ -507,26 +507,25 @@ void IntSettingDesc::Write_ValidateSetting(const void *object, int32 val) const
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the string value of a setting.
|
* Set the string value of a setting.
|
||||||
* @param ptr Pointer to the std::string.
|
* @param object The object the setting is to be saved in.
|
||||||
* @param sd Pointer to the information for the conversions and limitations to apply.
|
* @param str The string to save.
|
||||||
* @param p 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_STR:
|
||||||
case SLE_VAR_STRQ:
|
case SLE_VAR_STRQ:
|
||||||
if (p != nullptr) {
|
if (str != nullptr) {
|
||||||
if (sd->max != 0 && strlen(p) >= sd->max) {
|
if (this->max != 0 && strlen(str) >= this->max) {
|
||||||
/* In case a maximum length is imposed by the setting, the length
|
/* In case a maximum length is imposed by the setting, the length
|
||||||
* includes the '\0' termination for network transfer purposes.
|
* includes the '\0' termination for network transfer purposes.
|
||||||
* Also ensure the string is valid after chopping of some bytes. */
|
* Also ensure the string is valid after chopping of some bytes. */
|
||||||
std::string str(p, sd->max - 1);
|
std::string stdstr(str, this->max - 1);
|
||||||
dst->assign(str_validate(str, SVS_NONE));
|
dst->assign(str_validate(stdstr, SVS_NONE));
|
||||||
} else {
|
} else {
|
||||||
dst->assign(p);
|
dst->assign(str);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dst->clear();
|
dst->clear();
|
||||||
@ -603,7 +602,7 @@ static void IniLoadSettings(IniFile *ini, const SettingTable &settings_table, co
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case SDT_STDSTRING:
|
case SDT_STDSTRING:
|
||||||
Write_ValidateStdString(ptr, sd.get(), (const char *)p);
|
sd->AsStringSetting()->Write_ValidateSetting(object, (const char *)p);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDT_INTLIST: {
|
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;
|
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.
|
* Get the setting description of this setting as an integer setting.
|
||||||
* @return The integer setting description.
|
* @return The integer setting description.
|
||||||
@ -853,6 +860,16 @@ const IntSettingDesc *SettingDesc::AsIntSetting() const
|
|||||||
return static_cast<const IntSettingDesc *>(this);
|
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. */
|
/* Begin - Callback Functions for the various settings. */
|
||||||
|
|
||||||
/** Reposition the main toolbar as the setting changed. */
|
/** 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;
|
value = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
|
const void *object = (_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game;
|
||||||
Write_ValidateStdString(ptr, sd, value);
|
sd->AsStringSetting()->ChangeValue(object, value);
|
||||||
if (sd->proc != nullptr) sd->proc(0);
|
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();
|
if (_save_config) SaveToConfig();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -111,7 +111,9 @@ struct SettingDesc {
|
|||||||
bool IsEditable(bool do_command = false) const;
|
bool IsEditable(bool do_command = false) const;
|
||||||
SettingType GetType() const;
|
SettingType GetType() const;
|
||||||
bool IsIntSetting() const;
|
bool IsIntSetting() const;
|
||||||
|
bool IsStringSetting() const;
|
||||||
const struct IntSettingDesc *AsIntSetting() const;
|
const struct IntSettingDesc *AsIntSetting() const;
|
||||||
|
const struct StringSettingDesc *AsStringSetting() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format the value of the setting associated with this object.
|
* 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) {}
|
SettingDesc(save, name, def, cmd, flags, 0, max_length, 0, nullptr, 0, 0, 0, proc, nullptr, SC_NONE, startup) {}
|
||||||
virtual ~StringSettingDesc() {}
|
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;
|
void FormatValue(char *buf, const char *last, const void *object) const override;
|
||||||
const std::string &Read(const void *object) const;
|
const std::string &Read(const void *object) const;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user