OpenTTD Source  20241121-master-g67a0fccfad
StringSettingDesc Struct Reference

String settings. More...

#include <settings_internal.h>

Inheritance diagram for StringSettingDesc:
SettingDesc

Public Types

typedef bool PreChangeCheck(std::string &value)
 A check to be performed before the setting gets changed. More...
 
typedef void PostChangeCallback(const std::string &value)
 A callback to denote that a setting has been changed. More...
 

Public Member Functions

 StringSettingDesc (const SaveLoad &save, SettingFlag flags, bool startup, const char *def, uint32_t max_length, PreChangeCheck pre_check, PostChangeCallback post_callback)
 
bool IsStringSetting () const override
 Check whether this setting is an string type setting. More...
 
void ChangeValue (const void *object, std::string &newval) const
 Handle changing a string value. More...
 
std::string FormatValue (const void *object) const override
 Format the value of the setting associated with this object. More...
 
void ParseValue (const IniItem *item, void *object) const override
 Parse/read the value from the Ini item into the setting associated with this object. More...
 
bool IsSameValue (const IniItem *item, void *object) const override
 Check whether the value in the Ini item is the same as is saved in this setting in the object. More...
 
bool IsDefaultValue (void *object) const override
 Check whether the value is the same as the default value. More...
 
void ResetToDefault (void *object) const override
 Reset the setting to its default value.
 
const std::string & Read (const void *object) const
 Read the string from the the actual setting. More...
 
- Public Member Functions inherited from SettingDesc
 SettingDesc (const SaveLoad &save, SettingFlag flags, bool startup)
 
bool IsEditable (bool do_command=false) const
 Check whether the setting is editable in the current gamemode. More...
 
SettingType GetType () const
 Return the type of the setting. More...
 
constexpr const std::string & GetName () const
 Get the name of this setting. More...
 
virtual bool IsIntSetting () const
 Check whether this setting is an integer type setting. More...
 
const struct IntSettingDescAsIntSetting () const
 Get the setting description of this setting as an integer setting. More...
 
const struct StringSettingDescAsStringSetting () const
 Get the setting description of this setting as a string setting. More...
 

Data Fields

std::string def
 Default value given when none is present.
 
uint32_t max_length
 Maximum length of the string, 0 means no maximum length.
 
PreChangeCheckpre_check
 Callback to check for the validity of the setting.
 
PostChangeCallbackpost_callback
 Callback when the setting has been changed.
 
- Data Fields inherited from SettingDesc
SettingFlag flags
 Handles how a setting would show up in the GUI (text/currency, etc.).
 
bool startup
 Setting has to be loaded directly at startup?.
 
SaveLoad save
 Internal structure (going to savegame, parts to config).
 

Private Member Functions

void MakeValueValid (std::string &str) const
 Make the value valid given the limitations of this setting. More...
 
void Write (const void *object, const std::string &str) const
 Write a string to the actual setting. More...
 

Detailed Description

String settings.

Definition at line 318 of file settings_internal.h.

Member Typedef Documentation

◆ PostChangeCallback

typedef void StringSettingDesc::PostChangeCallback(const std::string &value)

A callback to denote that a setting has been changed.

Parameters
Thenew value for the setting.

Definition at line 332 of file settings_internal.h.

◆ PreChangeCheck

typedef bool StringSettingDesc::PreChangeCheck(std::string &value)

A check to be performed before the setting gets changed.

The passed string may be changed by the check if that is important, for example to remove unwanted white space. The return value denotes whether the value, potentially after the changes, is allowed to be used/set in the configuration.

Parameters
valueThe prospective new value for the setting.
Returns
True when the setting is accepted.

Definition at line 327 of file settings_internal.h.

Member Function Documentation

◆ ChangeValue()

void StringSettingDesc::ChangeValue ( const void *  object,
std::string &  newval 
) const

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.

Parameters
objectThe object the setting is in.
newvalThe new value for the setting.

Definition at line 1838 of file settings.cpp.

References MakeValueValid(), post_callback, pre_check, and Write().

◆ FormatValue()

std::string StringSettingDesc::FormatValue ( const void *  object) const
overridevirtual

Format the value of the setting associated with this object.

Parameters
bufThe before of the buffer to format into.
lastThe end of the buffer to format into.
objectThe object the setting is in.

Implements SettingDesc.

Definition at line 760 of file settings.cpp.

References SaveLoad::conv, GetVarMemType(), Read(), SettingDesc::save, SLE_VAR_STR, and SLE_VAR_STRQ.

◆ IsDefaultValue()

bool StringSettingDesc::IsDefaultValue ( void *  object) const
overridevirtual

Check whether the value is the same as the default value.

Parameters
objectThe object the setting is in.
Returns
true iff the value is the default value.

Implements SettingDesc.

Definition at line 786 of file settings.cpp.

References def, and Read().

◆ IsSameValue()

bool StringSettingDesc::IsSameValue ( const IniItem item,
void *  object 
) const
overridevirtual

Check whether the value in the Ini item is the same as is saved in this setting in the object.

It might be that determining whether the value is the same is way more expensive than just writing the value. In those cases this function may unconditionally return false even though the value might be the same as in the Ini item.

Parameters
itemThe Ini item with the content of this setting.
objectThe object the setting is in.
Returns
True if the value is definitely the same (might be false when the same).

Implements SettingDesc.

Definition at line 776 of file settings.cpp.

References SaveLoad::conv, GetVarMemType(), Read(), SettingDesc::save, SLE_VAR_STRQ, and IniItem::value.

◆ IsStringSetting()

bool StringSettingDesc::IsStringSetting ( ) const
inlineoverridevirtual

Check whether this setting is an string type setting.

Returns
True when the underlying type is a string.

Reimplemented from SettingDesc.

Definition at line 344 of file settings_internal.h.

◆ MakeValueValid()

void StringSettingDesc::MakeValueValid ( std::string &  str) const
private

Make the value valid given the limitations of this setting.

In the case of string settings this is ensuring the string contains only accepted Utf8 characters and is at most the maximum length defined in this setting.

Parameters
strThe string to make valid.

Definition at line 574 of file settings.cpp.

References max_length, StrMakeValid(), and SVS_NONE.

Referenced by ChangeValue(), and ParseValue().

◆ ParseValue()

void StringSettingDesc::ParseValue ( const IniItem item,
void *  object 
) const
overridevirtual

Parse/read the value from the Ini item into the setting associated with this object.

Parameters
itemThe Ini item with the content of this setting.
objectThe object the setting is in.

Implements SettingDesc.

Definition at line 661 of file settings.cpp.

References def, MakeValueValid(), IniItem::value, and Write().

◆ Read()

const std::string & StringSettingDesc::Read ( const void *  object) const

Read the string from the the actual setting.

Parameters
objectThe object the setting is to be saved in.
Returns
The value of the saved string.

Definition at line 600 of file settings.cpp.

References GetVariableAddress(), and SettingDesc::save.

Referenced by FormatValue(), IsDefaultValue(), and IsSameValue().

◆ Write()

void StringSettingDesc::Write ( const void *  object,
const std::string &  str 
) const
private

Write a string to the actual setting.

Parameters
objectThe object the setting is to be saved in.
strThe string to save.

Definition at line 590 of file settings.cpp.

References GetVariableAddress(), and SettingDesc::save.

Referenced by ChangeValue(), ParseValue(), and ResetToDefault().


The documentation for this struct was generated from the following files: