1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

Rework Settings to support arbitrary hierarchies (#11352)

This commit is contained in:
sfan5 2021-06-23 15:22:31 +02:00 committed by GitHub
parent cec0dfcbbd
commit c60a146e22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 164 additions and 71 deletions

View file

@ -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;
};