summaryrefslogtreecommitdiff
path: root/src/settings.h
diff options
context:
space:
mode:
authorsfan5 <sfan5@live.de>2021-06-23 15:22:31 +0200
committerGitHub <noreply@github.com>2021-06-23 15:22:31 +0200
commitc60a146e2291f7a55a3e5fd0447bd393b063ab1c (patch)
tree7a0810ef78f43866eb899145d6afdb36d99f2720 /src/settings.h
parentcec0dfcbbda1e11e5ff2f45e58ea393d0437d7c6 (diff)
downloadminetest-c60a146e2291f7a55a3e5fd0447bd393b063ab1c.tar.gz
minetest-c60a146e2291f7a55a3e5fd0447bd393b063ab1c.tar.bz2
minetest-c60a146e2291f7a55a3e5fd0447bd393b063ab1c.zip
Rework Settings to support arbitrary hierarchies (#11352)
Diffstat (limited to 'src/settings.h')
-rw-r--r--src/settings.h44
1 files changed, 37 insertions, 7 deletions
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 <string>
#include <list>
#include <set>
@@ -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<Settings*> layers;
+};
+
struct ValueSpec {
ValueSpec(ValueType a_type, const char *a_help=NULL)
{
@@ -100,13 +123,15 @@ typedef std::unordered_map<std::string, SettingsEntry> 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<std::string, const FlagDesc *> s_flags;
};