#include "irrlichttypes_bloated.h"
#include "util/string.h"
-#include "jthread/jmutex.h"
+#include "threading/mutex.h"
#include <string>
-#include <map>
+#include "util/cpp11_container.h"
#include <list>
#include <set>
class Settings;
struct NoiseParams;
-/** function type to register a changed callback */
-typedef void (*setting_changed_callback)(const std::string, void*);
+// Global objects
+extern Settings *g_settings;
+extern std::string g_settings_path;
+
+// Type for a settings changed callback function
+typedef void (*SettingsChangedCallback)(const std::string &name, void *data);
+
+typedef std::vector<
+ std::pair<
+ SettingsChangedCallback,
+ void *
+ >
+> SettingsCallbackList;
+
+typedef UNORDERED_MAP<std::string, SettingsCallbackList> SettingsCallbackMap;
enum ValueType {
VALUETYPE_STRING,
};
struct SettingsEntry {
- SettingsEntry()
- {
- group = NULL;
- is_group = false;
- }
-
- SettingsEntry(const std::string &value_)
- {
- value = value_;
- group = NULL;
- is_group = false;
- }
-
- SettingsEntry(Settings *group_)
- {
- group = group_;
- is_group = true;
- }
+ SettingsEntry() :
+ group(NULL),
+ is_group(false)
+ {}
+
+ SettingsEntry(const std::string &value_) :
+ value(value_),
+ group(NULL),
+ is_group(false)
+ {}
+
+ SettingsEntry(Settings *group_) :
+ group(group_),
+ is_group(true)
+ {}
std::string value;
Settings *group;
bool is_group;
};
+typedef UNORDERED_MAP<std::string, SettingsEntry> SettingEntries;
+
class Settings {
public:
Settings() {}
static bool checkNameValid(const std::string &name);
static bool checkValueValid(const std::string &value);
- static std::string sanitizeName(const std::string &name);
- static std::string sanitizeValue(const std::string &value);
static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
static void printEntry(std::ostream &os, const std::string &name,
const SettingsEntry &entry, u32 tab_depth=0);
const SettingsEntry &getEntry(const std::string &name) const;
Settings *getGroup(const std::string &name) const;
- std::string get(const std::string &name) const;
+ const std::string &get(const std::string &name) const;
bool getBool(const std::string &name) const;
u16 getU16(const std::string &name) const;
s16 getS16(const std::string &name) const;
// remove a setting
bool remove(const std::string &name);
void clear();
+ void clearDefaults();
void updateValue(const Settings &other, const std::string &name);
void update(const Settings &other);
- void registerChangedCallback(std::string name, setting_changed_callback cbf, void *userdata = NULL);
- void deregisterChangedCallback(std::string name, setting_changed_callback cbf, void *userdata = NULL);
-private:
+ void registerChangedCallback(const std::string &name,
+ SettingsChangedCallback cbf, void *userdata = NULL);
+ void deregisterChangedCallback(const std::string &name,
+ SettingsChangedCallback cbf, void *userdata = NULL);
+private:
void updateNoLock(const Settings &other);
void clearNoLock();
+ void clearDefaultsNoLock();
+
+ void doCallbacks(const std::string &name) const;
- void doCallbacks(std::string name);
+ SettingEntries m_settings;
+ SettingEntries m_defaults;
- std::map<std::string, SettingsEntry> m_settings;
- std::map<std::string, SettingsEntry> m_defaults;
+ SettingsCallbackMap m_callbacks;
- std::map<std::string, std::vector<std::pair<setting_changed_callback,void*> > > m_callbacks;
+ mutable Mutex m_callback_mutex;
- mutable JMutex m_callbackMutex;
- mutable JMutex m_mutex; // All methods that access m_settings/m_defaults directly should lock this.
+ // All methods that access m_settings/m_defaults directly should lock this.
+ mutable Mutex m_mutex;
};