mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use std::string_view for the string setting defaults
parent
5c7b04a0c2
commit
5ca686261e
|
@ -592,7 +592,7 @@ void StringSettingDesc::MakeValueValid(std::string &str) const
|
||||||
* @param object The object the setting is to be saved in.
|
* @param object The object the setting is to be saved in.
|
||||||
* @param str The string to save.
|
* @param str The string to save.
|
||||||
*/
|
*/
|
||||||
void StringSettingDesc::Write(const void *object, const std::string &str) const
|
void StringSettingDesc::Write(const void *object, std::string_view str) const
|
||||||
{
|
{
|
||||||
reinterpret_cast<std::string *>(GetVariableAddress(object, this->save))->assign(str);
|
reinterpret_cast<std::string *>(GetVariableAddress(object, this->save))->assign(str);
|
||||||
}
|
}
|
||||||
|
@ -665,7 +665,7 @@ void IntSettingDesc::ParseValue(const IniItem *item, void *object) const
|
||||||
|
|
||||||
void StringSettingDesc::ParseValue(const IniItem *item, void *object) const
|
void StringSettingDesc::ParseValue(const IniItem *item, void *object) const
|
||||||
{
|
{
|
||||||
std::string str = (item == nullptr) ? this->def : item->value.value_or("");
|
std::string str{(item == nullptr) ? this->def : item->value.value_or("")};
|
||||||
this->MakeValueValid(str);
|
this->MakeValueValid(str);
|
||||||
this->Write(object, str);
|
this->Write(object, str);
|
||||||
}
|
}
|
||||||
|
@ -675,7 +675,7 @@ void ListSettingDesc::ParseValue(const IniItem *item, void *object) const
|
||||||
std::optional<std::string_view> str;
|
std::optional<std::string_view> str;
|
||||||
if (item != nullptr) {
|
if (item != nullptr) {
|
||||||
str = item->value;
|
str = item->value;
|
||||||
} else if (this->def != nullptr) {
|
} else if (!this->def.empty()) {
|
||||||
str = this->def;
|
str = this->def;
|
||||||
}
|
}
|
||||||
void *ptr = GetVariableAddress(object, this->save);
|
void *ptr = GetVariableAddress(object, this->save);
|
||||||
|
|
|
@ -323,12 +323,12 @@ struct StringSettingDesc : SettingDesc {
|
||||||
*/
|
*/
|
||||||
typedef void PostChangeCallback(const std::string &value);
|
typedef void PostChangeCallback(const std::string &value);
|
||||||
|
|
||||||
StringSettingDesc(const SaveLoad &save, SettingFlags flags, bool startup, const char *def,
|
StringSettingDesc(const SaveLoad &save, SettingFlags flags, bool startup, std::string_view def,
|
||||||
uint32_t max_length, PreChangeCheck pre_check, PostChangeCallback post_callback) :
|
uint32_t max_length, PreChangeCheck pre_check, PostChangeCallback post_callback) :
|
||||||
SettingDesc(save, flags, startup), def(def == nullptr ? "" : def), max_length(max_length),
|
SettingDesc(save, flags, startup), def(def), max_length(max_length),
|
||||||
pre_check(pre_check), post_callback(post_callback) {}
|
pre_check(pre_check), post_callback(post_callback) {}
|
||||||
|
|
||||||
std::string def; ///< Default value given when none is present
|
std::string_view def; ///< Default value given when none is present
|
||||||
uint32_t max_length; ///< Maximum length of the string, 0 means no maximum length
|
uint32_t max_length; ///< Maximum length of the string, 0 means no maximum length
|
||||||
PreChangeCheck *pre_check; ///< Callback to check for the validity of the setting.
|
PreChangeCheck *pre_check; ///< Callback to check for the validity of the setting.
|
||||||
PostChangeCallback *post_callback; ///< Callback when the setting has been changed.
|
PostChangeCallback *post_callback; ///< Callback when the setting has been changed.
|
||||||
|
@ -345,15 +345,15 @@ struct StringSettingDesc : SettingDesc {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void MakeValueValid(std::string &str) const;
|
void MakeValueValid(std::string &str) const;
|
||||||
void Write(const void *object, const std::string &str) const;
|
void Write(const void *object, std::string_view str) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** List/array settings. */
|
/** List/array settings. */
|
||||||
struct ListSettingDesc : SettingDesc {
|
struct ListSettingDesc : SettingDesc {
|
||||||
ListSettingDesc(const SaveLoad &save, SettingFlags flags, bool startup, const char *def) :
|
ListSettingDesc(const SaveLoad &save, SettingFlags flags, bool startup, std::string_view def) :
|
||||||
SettingDesc(save, flags, startup), def(def) {}
|
SettingDesc(save, flags, startup), def(def) {}
|
||||||
|
|
||||||
const char *def; ///< default value given when none is present
|
std::string_view def; ///< default value given when none is present
|
||||||
|
|
||||||
std::string FormatValue(const void *object) const override;
|
std::string FormatValue(const void *object) const override;
|
||||||
void ParseValue(const IniItem *item, void *object) const override;
|
void ParseValue(const IniItem *item, void *object) const override;
|
||||||
|
|
|
@ -61,7 +61,7 @@ max = CalendarTime::MAX_YEAR
|
||||||
[SDT_SSTR]
|
[SDT_SSTR]
|
||||||
var = prefix
|
var = prefix
|
||||||
type = SLE_STRQ
|
type = SLE_STRQ
|
||||||
def = nullptr
|
def = """"
|
||||||
|
|
||||||
[SDT_SSTR]
|
[SDT_SSTR]
|
||||||
var = suffix
|
var = suffix
|
||||||
|
|
|
@ -176,7 +176,7 @@ var = locale.digit_group_separator
|
||||||
type = SLE_STRQ
|
type = SLE_STRQ
|
||||||
from = SLV_118
|
from = SLV_118
|
||||||
flags = SettingFlag::NoNetworkSync
|
flags = SettingFlag::NoNetworkSync
|
||||||
def = nullptr
|
def = """"
|
||||||
post_cb = [](auto) { MarkWholeScreenDirty(); }
|
post_cb = [](auto) { MarkWholeScreenDirty(); }
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
|
||||||
|
@ -185,7 +185,7 @@ var = locale.digit_group_separator_currency
|
||||||
type = SLE_STRQ
|
type = SLE_STRQ
|
||||||
from = SLV_118
|
from = SLV_118
|
||||||
flags = SettingFlag::NoNetworkSync
|
flags = SettingFlag::NoNetworkSync
|
||||||
def = nullptr
|
def = """"
|
||||||
post_cb = [](auto) { MarkWholeScreenDirty(); }
|
post_cb = [](auto) { MarkWholeScreenDirty(); }
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
|
||||||
|
@ -194,6 +194,6 @@ var = locale.digit_decimal_separator
|
||||||
type = SLE_STRQ
|
type = SLE_STRQ
|
||||||
from = SLV_126
|
from = SLV_126
|
||||||
flags = SettingFlag::NoNetworkSync
|
flags = SettingFlag::NoNetworkSync
|
||||||
def = nullptr
|
def = """"
|
||||||
post_cb = [](auto) { MarkWholeScreenDirty(); }
|
post_cb = [](auto) { MarkWholeScreenDirty(); }
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
|
|
@ -111,48 +111,48 @@ cat = SC_BASIC
|
||||||
name = ""soundsset""
|
name = ""soundsset""
|
||||||
type = SLE_STRQ
|
type = SLE_STRQ
|
||||||
var = BaseSounds::ini_set
|
var = BaseSounds::ini_set
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
|
||||||
[SDTG_SSTR]
|
[SDTG_SSTR]
|
||||||
name = ""musicset""
|
name = ""musicset""
|
||||||
type = SLE_STRQ
|
type = SLE_STRQ
|
||||||
var = BaseMusic::ini_set
|
var = BaseMusic::ini_set
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
|
||||||
[SDTG_SSTR]
|
[SDTG_SSTR]
|
||||||
name = ""videodriver""
|
name = ""videodriver""
|
||||||
type = SLE_STRQ
|
type = SLE_STRQ
|
||||||
var = _ini_videodriver
|
var = _ini_videodriver
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_EXPERT
|
cat = SC_EXPERT
|
||||||
|
|
||||||
[SDTG_SSTR]
|
[SDTG_SSTR]
|
||||||
name = ""musicdriver""
|
name = ""musicdriver""
|
||||||
type = SLE_STRQ
|
type = SLE_STRQ
|
||||||
var = _ini_musicdriver
|
var = _ini_musicdriver
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_EXPERT
|
cat = SC_EXPERT
|
||||||
|
|
||||||
[SDTG_SSTR]
|
[SDTG_SSTR]
|
||||||
name = ""sounddriver""
|
name = ""sounddriver""
|
||||||
type = SLE_STRQ
|
type = SLE_STRQ
|
||||||
var = _ini_sounddriver
|
var = _ini_sounddriver
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_EXPERT
|
cat = SC_EXPERT
|
||||||
|
|
||||||
[SDTG_SSTR]
|
[SDTG_SSTR]
|
||||||
name = ""blitter""
|
name = ""blitter""
|
||||||
type = SLE_STRQ
|
type = SLE_STRQ
|
||||||
var = _ini_blitter
|
var = _ini_blitter
|
||||||
def = nullptr
|
def = """"
|
||||||
|
|
||||||
[SDTG_SSTR]
|
[SDTG_SSTR]
|
||||||
name = ""language""
|
name = ""language""
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
var = _config_language_file
|
var = _config_language_file
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
|
||||||
; workaround for implicit lengthof() in SDTG_LIST
|
; workaround for implicit lengthof() in SDTG_LIST
|
||||||
|
@ -168,14 +168,14 @@ cat = SC_BASIC
|
||||||
name = ""screenshot_format""
|
name = ""screenshot_format""
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
var = _screenshot_format_name
|
var = _screenshot_format_name
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_EXPERT
|
cat = SC_EXPERT
|
||||||
|
|
||||||
[SDTG_SSTR]
|
[SDTG_SSTR]
|
||||||
name = ""savegame_format""
|
name = ""savegame_format""
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
var = _savegame_format
|
var = _savegame_format
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_EXPERT
|
cat = SC_EXPERT
|
||||||
|
|
||||||
[SDTG_BOOL]
|
[SDTG_BOOL]
|
||||||
|
@ -188,28 +188,28 @@ ifdef = HAS_TRUETYPE_FONT
|
||||||
name = ""small_font""
|
name = ""small_font""
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
var = _fcsettings.small.font
|
var = _fcsettings.small.font
|
||||||
def = nullptr
|
def = """"
|
||||||
|
|
||||||
[SDTG_SSTR]
|
[SDTG_SSTR]
|
||||||
ifdef = HAS_TRUETYPE_FONT
|
ifdef = HAS_TRUETYPE_FONT
|
||||||
name = ""medium_font""
|
name = ""medium_font""
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
var = _fcsettings.medium.font
|
var = _fcsettings.medium.font
|
||||||
def = nullptr
|
def = """"
|
||||||
|
|
||||||
[SDTG_SSTR]
|
[SDTG_SSTR]
|
||||||
ifdef = HAS_TRUETYPE_FONT
|
ifdef = HAS_TRUETYPE_FONT
|
||||||
name = ""large_font""
|
name = ""large_font""
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
var = _fcsettings.large.font
|
var = _fcsettings.large.font
|
||||||
def = nullptr
|
def = """"
|
||||||
|
|
||||||
[SDTG_SSTR]
|
[SDTG_SSTR]
|
||||||
ifdef = HAS_TRUETYPE_FONT
|
ifdef = HAS_TRUETYPE_FONT
|
||||||
name = ""mono_font""
|
name = ""mono_font""
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
var = _fcsettings.mono.font
|
var = _fcsettings.mono.font
|
||||||
def = nullptr
|
def = """"
|
||||||
|
|
||||||
[SDTG_VAR]
|
[SDTG_VAR]
|
||||||
ifdef = HAS_TRUETYPE_FONT
|
ifdef = HAS_TRUETYPE_FONT
|
||||||
|
@ -308,14 +308,14 @@ cat = SC_BASIC
|
||||||
name = ""keyboard""
|
name = ""keyboard""
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
var = _keyboard_opt[0]
|
var = _keyboard_opt[0]
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_EXPERT
|
cat = SC_EXPERT
|
||||||
|
|
||||||
[SDTG_SSTR]
|
[SDTG_SSTR]
|
||||||
name = ""keyboard_caps""
|
name = ""keyboard_caps""
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
var = _keyboard_opt[1]
|
var = _keyboard_opt[1]
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_EXPERT
|
cat = SC_EXPERT
|
||||||
|
|
||||||
[SDTG_VAR]
|
[SDTG_VAR]
|
||||||
|
|
|
@ -132,14 +132,14 @@ cat = SC_BASIC
|
||||||
var = music.custom_1
|
var = music.custom_1
|
||||||
type = SLE_UINT8
|
type = SLE_UINT8
|
||||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
|
||||||
[SDTC_LIST]
|
[SDTC_LIST]
|
||||||
var = music.custom_2
|
var = music.custom_2
|
||||||
type = SLE_UINT8
|
type = SLE_UINT8
|
||||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
|
||||||
[SDTC_BOOL]
|
[SDTC_BOOL]
|
||||||
|
|
|
@ -47,7 +47,7 @@ var = network.client_name
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
length = NETWORK_CLIENT_NAME_LENGTH
|
length = NETWORK_CLIENT_NAME_LENGTH
|
||||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
||||||
def = nullptr
|
def = """"
|
||||||
pre_cb = NetworkValidateClientName
|
pre_cb = NetworkValidateClientName
|
||||||
post_cb = NetworkUpdateClientName
|
post_cb = NetworkUpdateClientName
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
@ -57,7 +57,7 @@ var = network.server_name
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
length = NETWORK_NAME_LENGTH
|
length = NETWORK_NAME_LENGTH
|
||||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync, SettingFlag::NetworkOnly
|
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync, SettingFlag::NetworkOnly
|
||||||
def = nullptr
|
def = """"
|
||||||
pre_cb = NetworkValidateServerName
|
pre_cb = NetworkValidateServerName
|
||||||
post_cb = [](auto) { UpdateClientConfigValues(); }
|
post_cb = [](auto) { UpdateClientConfigValues(); }
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
@ -67,7 +67,7 @@ var = network.connect_to_ip
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
length = 0
|
length = 0
|
||||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
||||||
def = nullptr
|
def = """"
|
||||||
|
|
||||||
[SDTC_SSTR]
|
[SDTC_SSTR]
|
||||||
var = network.last_joined
|
var = network.last_joined
|
||||||
|
|
|
@ -39,7 +39,7 @@ var = network.server_password
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
length = NETWORK_PASSWORD_LENGTH
|
length = NETWORK_PASSWORD_LENGTH
|
||||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync, SettingFlag::NetworkOnly
|
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync, SettingFlag::NetworkOnly
|
||||||
def = nullptr
|
def = """"
|
||||||
pre_cb = ReplaceAsteriskWithEmptyPassword
|
pre_cb = ReplaceAsteriskWithEmptyPassword
|
||||||
post_cb = [](auto) { NetworkServerUpdateGameInfo(); }
|
post_cb = [](auto) { NetworkServerUpdateGameInfo(); }
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
@ -49,7 +49,7 @@ var = network.rcon_password
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
length = NETWORK_PASSWORD_LENGTH
|
length = NETWORK_PASSWORD_LENGTH
|
||||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync, SettingFlag::NetworkOnly
|
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync, SettingFlag::NetworkOnly
|
||||||
def = nullptr
|
def = """"
|
||||||
pre_cb = ReplaceAsteriskWithEmptyPassword
|
pre_cb = ReplaceAsteriskWithEmptyPassword
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ var = network.admin_password
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
length = NETWORK_PASSWORD_LENGTH
|
length = NETWORK_PASSWORD_LENGTH
|
||||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync, SettingFlag::NetworkOnly
|
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync, SettingFlag::NetworkOnly
|
||||||
def = nullptr
|
def = """"
|
||||||
cat = SC_BASIC
|
cat = SC_BASIC
|
||||||
|
|
||||||
[SDTC_SSTR]
|
[SDTC_SSTR]
|
||||||
|
@ -66,7 +66,7 @@ var = network.client_secret_key
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
length = NETWORK_SECRET_KEY_LENGTH
|
length = NETWORK_SECRET_KEY_LENGTH
|
||||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
||||||
def = nullptr
|
def = """"
|
||||||
; Prevent the user from setting the secret key from the console using 'setting'
|
; Prevent the user from setting the secret key from the console using 'setting'
|
||||||
pre_cb = [](auto) { return false; }
|
pre_cb = [](auto) { return false; }
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ var = network.client_public_key
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
length = NETWORK_SECRET_KEY_LENGTH
|
length = NETWORK_SECRET_KEY_LENGTH
|
||||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync
|
||||||
def = nullptr
|
def = """"
|
||||||
; Prevent the user from setting the public key from the console using 'setting'
|
; Prevent the user from setting the public key from the console using 'setting'
|
||||||
pre_cb = [](auto) { return false; }
|
pre_cb = [](auto) { return false; }
|
||||||
|
|
||||||
|
@ -84,11 +84,11 @@ var = network.server_invite_code
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
length = NETWORK_INVITE_CODE_LENGTH
|
length = NETWORK_INVITE_CODE_LENGTH
|
||||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync, SettingFlag::NetworkOnly
|
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync, SettingFlag::NetworkOnly
|
||||||
def = nullptr
|
def = """"
|
||||||
|
|
||||||
[SDTC_SSTR]
|
[SDTC_SSTR]
|
||||||
var = network.server_invite_code_secret
|
var = network.server_invite_code_secret
|
||||||
type = SLE_STR
|
type = SLE_STR
|
||||||
length = NETWORK_INVITE_CODE_SECRET_LENGTH
|
length = NETWORK_INVITE_CODE_SECRET_LENGTH
|
||||||
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync, SettingFlag::NetworkOnly
|
flags = SettingFlag::NotInSave, SettingFlag::NoNetworkSync, SettingFlag::NetworkOnly
|
||||||
def = nullptr
|
def = """"
|
||||||
|
|
|
@ -75,7 +75,7 @@ type = SLE_FILE_I16 | SLE_VAR_U16
|
||||||
flags = SettingFlag::NotInConfig
|
flags = SettingFlag::NotInConfig
|
||||||
var = _old_diff_custom
|
var = _old_diff_custom
|
||||||
length = 17
|
length = 17
|
||||||
def = nullptr
|
def = """"
|
||||||
to = SLV_4
|
to = SLV_4
|
||||||
|
|
||||||
[SDTG_LIST]
|
[SDTG_LIST]
|
||||||
|
@ -86,7 +86,7 @@ type = SLE_UINT16
|
||||||
flags = SettingFlag::NotInConfig
|
flags = SettingFlag::NotInConfig
|
||||||
var = _old_diff_custom
|
var = _old_diff_custom
|
||||||
length = 18
|
length = 18
|
||||||
def = nullptr
|
def = """"
|
||||||
full = nullptr
|
full = nullptr
|
||||||
from = SLV_4
|
from = SLV_4
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue