From c60a146e2291f7a55a3e5fd0447bd393b063ab1c Mon Sep 17 00:00:00 2001 From: sfan5 Date: Wed, 23 Jun 2021 15:22:31 +0200 Subject: Rework Settings to support arbitrary hierarchies (#11352) --- src/settings.h | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'src/settings.h') diff --git a/src/settings.h b/src/settings.h index e22d949d3..7791413b9 100644 --- a/src/settings.h +++ b/src/settings.h @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irrlichttypes_bloated.h" #include "util/string.h" +#include "util/basic_macros.h" #include #include #include @@ -60,14 +61,36 @@ enum SettingsParseEvent { SPE_MULTILINE, }; +// Describes the global setting layers, SL_GLOBAL is where settings are read from enum SettingsLayer { SL_DEFAULTS, SL_GAME, SL_GLOBAL, - SL_MAP, SL_TOTAL_COUNT }; +// Implements the hierarchy a settings object may be part of +class SettingsHierarchy { +public: + /* + * A settings object that may be part of another hierarchy can + * occupy the index 0 as a fallback. If not set you can use 0 on your own. + */ + SettingsHierarchy(Settings *fallback = nullptr); + + DISABLE_CLASS_COPY(SettingsHierarchy) + + Settings *getLayer(int layer) const; + +private: + friend class Settings; + Settings *getParent(int layer) const; + void onLayerCreated(int layer, Settings *obj); + void onLayerRemoved(int layer); + + std::vector layers; +}; + struct ValueSpec { ValueSpec(ValueType a_type, const char *a_help=NULL) { @@ -100,13 +123,15 @@ typedef std::unordered_map SettingEntries; class Settings { public: + /* These functions operate on the global hierarchy! */ static Settings *createLayer(SettingsLayer sl, const std::string &end_tag = ""); static Settings *getLayer(SettingsLayer sl); - SettingsLayer getLayerType() const { return m_settingslayer; } + /**/ Settings(const std::string &end_tag = "") : m_end_tag(end_tag) {} + Settings(const std::string &end_tag, SettingsHierarchy *h, int settings_layer); ~Settings(); Settings & operator += (const Settings &other); @@ -200,9 +225,9 @@ public: // remove a setting bool remove(const std::string &name); - /************** - * Miscellany * - **************/ + /***************** + * Miscellaneous * + *****************/ void setDefault(const std::string &name, const FlagDesc *flagdesc, u32 flags); const FlagDesc *getFlagDescFallback(const std::string &name) const; @@ -214,6 +239,10 @@ public: void removeSecureSettings(); + // Returns the settings layer this object is. + // If within the global hierarchy you can cast this to enum SettingsLayer + inline int getLayer() const { return m_settingslayer; } + private: /*********************** * Reading and writing * @@ -257,7 +286,8 @@ private: // All methods that access m_settings/m_defaults directly should lock this. mutable std::mutex m_mutex; - static Settings *s_layers[SL_TOTAL_COUNT]; - SettingsLayer m_settingslayer = SL_TOTAL_COUNT; + SettingsHierarchy *m_hierarchy = nullptr; + int m_settingslayer = -1; + static std::unordered_map s_flags; }; -- cgit v1.2.3