diff --git a/src/settings.cpp b/src/settings.cpp index dc02c7a66c..4d6d9648c2 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -831,6 +831,12 @@ bool IntSettingDesc::IsSameValue(const IniItem *item, void *object) const return item_value == object_value; } +bool IntSettingDesc::IsDefaultValue(void *object) const +{ + int32_t object_value = this->Read(object); + return this->def == object_value; +} + void StringSettingDesc::FormatValue(char *buf, const char *last, const void *object) const { const std::string &str = this->Read(object); @@ -859,12 +865,24 @@ bool StringSettingDesc::IsSameValue(const IniItem *item, void *object) const return item->value->compare(str) == 0; } +bool StringSettingDesc::IsDefaultValue(void *object) const +{ + const std::string &str = this->Read(object); + return this->def == str; +} + bool ListSettingDesc::IsSameValue(const IniItem *item, void *object) const { /* Checking for equality is way more expensive than just writing the value. */ return false; } +bool ListSettingDesc::IsDefaultValue(void *) const +{ + /* Defaults of lists are often complicated, and hard to compare. */ + return false; +} + /** * Loads all items from a 'grpname' section into a list * The list parameter can be a nullptr pointer, in this case nothing will be diff --git a/src/settings_internal.h b/src/settings_internal.h index 7975f437a2..8ed380eabe 100644 --- a/src/settings_internal.h +++ b/src/settings_internal.h @@ -165,6 +165,14 @@ struct SettingDesc { * @return True if the value is definitely the same (might be false when the same). */ virtual bool IsSameValue(const IniItem *item, void *object) const = 0; + + /** + * Check whether the value is the same as the default value. + * + * @param object The object the setting is in. + * @return true iff the value is the default value. + */ + virtual bool IsDefaultValue(void *object) const = 0; }; /** Base integer type, including boolean, settings. Only these are shown in the settings UI. */ @@ -219,6 +227,7 @@ struct IntSettingDesc : SettingDesc { virtual void FormatIntValue(char *buf, const char *last, uint32 value) const; void ParseValue(const IniItem *item, void *object) const override; bool IsSameValue(const IniItem *item, void *object) const override; + bool IsDefaultValue(void *object) const override; int32 Read(const void *object) const; private: @@ -309,6 +318,7 @@ struct StringSettingDesc : SettingDesc { void FormatValue(char *buf, const char *last, const void *object) const override; void ParseValue(const IniItem *item, void *object) const override; bool IsSameValue(const IniItem *item, void *object) const override; + bool IsDefaultValue(void *object) const override; const std::string &Read(const void *object) const; private: @@ -326,6 +336,7 @@ struct ListSettingDesc : SettingDesc { void FormatValue(char *buf, const char *last, const void *object) const override; void ParseValue(const IniItem *item, void *object) const override; bool IsSameValue(const IniItem *item, void *object) const override; + bool IsDefaultValue(void *object) const override; }; /** Placeholder for settings that have been removed, but might still linger in the savegame. */ @@ -338,6 +349,7 @@ struct NullSettingDesc : SettingDesc { void FormatValue(char *buf, const char *last, const void *object) const override { NOT_REACHED(); } void ParseValue(const IniItem *item, void *object) const override { NOT_REACHED(); } bool IsSameValue(const IniItem *item, void *object) const override { NOT_REACHED(); } + bool IsDefaultValue(void *object) const override { NOT_REACHED(); } }; typedef std::initializer_list> SettingTable;