Make some maps unordered to improve performance
[oweals/minetest.git] / src / settings.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef SETTINGS_HEADER
21 #define SETTINGS_HEADER
22
23 #include "irrlichttypes_bloated.h"
24 #include "util/string.h"
25 #include "threading/mutex.h"
26 #include <string>
27 #include "util/cpp11_container.h"
28 #include <list>
29 #include <set>
30
31 class Settings;
32 struct NoiseParams;
33
34 // Global objects
35 extern Settings *g_settings;
36 extern std::string g_settings_path;
37
38 // Type for a settings changed callback function
39 typedef void (*SettingsChangedCallback)(const std::string &name, void *data);
40
41 typedef std::vector<
42         std::pair<
43                 SettingsChangedCallback,
44                 void *
45         >
46 > SettingsCallbackList;
47
48 typedef UNORDERED_MAP<std::string, SettingsCallbackList> SettingsCallbackMap;
49
50 enum ValueType {
51         VALUETYPE_STRING,
52         VALUETYPE_FLAG // Doesn't take any arguments
53 };
54
55 enum SettingsParseEvent {
56         SPE_NONE,
57         SPE_INVALID,
58         SPE_COMMENT,
59         SPE_KVPAIR,
60         SPE_END,
61         SPE_GROUP,
62         SPE_MULTILINE,
63 };
64
65 struct ValueSpec {
66         ValueSpec(ValueType a_type, const char *a_help=NULL)
67         {
68                 type = a_type;
69                 help = a_help;
70         }
71
72         ValueType type;
73         const char *help;
74 };
75
76 struct SettingsEntry {
77         SettingsEntry()
78         {
79                 group    = NULL;
80                 is_group = false;
81         }
82
83         SettingsEntry(const std::string &value_)
84         {
85                 value    = value_;
86                 group    = NULL;
87                 is_group = false;
88         }
89
90         SettingsEntry(Settings *group_)
91         {
92                 group    = group_;
93                 is_group = true;
94         }
95
96         std::string value;
97         Settings *group;
98         bool is_group;
99 };
100
101 class Settings {
102 public:
103         Settings() {}
104         ~Settings();
105
106         Settings & operator += (const Settings &other);
107         Settings & operator = (const Settings &other);
108
109         /***********************
110          * Reading and writing *
111          ***********************/
112
113         // Read configuration file.  Returns success.
114         bool readConfigFile(const char *filename);
115         //Updates configuration file.  Returns success.
116         bool updateConfigFile(const char *filename);
117         // NOTE: Types of allowed_options are ignored.  Returns success.
118         bool parseCommandLine(int argc, char *argv[],
119                         std::map<std::string, ValueSpec> &allowed_options);
120         bool parseConfigLines(std::istream &is, const std::string &end = "");
121         void writeLines(std::ostream &os, u32 tab_depth=0) const;
122
123         SettingsParseEvent parseConfigObject(const std::string &line,
124                 const std::string &end, std::string &name, std::string &value);
125         bool updateConfigObject(std::istream &is, std::ostream &os,
126                 const std::string &end, u32 tab_depth=0);
127
128         static bool checkNameValid(const std::string &name);
129         static bool checkValueValid(const std::string &value);
130         static std::string sanitizeName(const std::string &name);
131         static std::string sanitizeValue(const std::string &value);
132         static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
133         static void printEntry(std::ostream &os, const std::string &name,
134                 const SettingsEntry &entry, u32 tab_depth=0);
135
136         /***********
137          * Getters *
138          ***********/
139
140         const SettingsEntry &getEntry(const std::string &name) const;
141         Settings *getGroup(const std::string &name) const;
142         std::string get(const std::string &name) const;
143         bool getBool(const std::string &name) const;
144         u16 getU16(const std::string &name) const;
145         s16 getS16(const std::string &name) const;
146         s32 getS32(const std::string &name) const;
147         u64 getU64(const std::string &name) const;
148         float getFloat(const std::string &name) const;
149         v2f getV2F(const std::string &name) const;
150         v3f getV3F(const std::string &name) const;
151         u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
152                         u32 *flagmask) const;
153         // N.B. if getStruct() is used to read a non-POD aggregate type,
154         // the behavior is undefined.
155         bool getStruct(const std::string &name, const std::string &format,
156                         void *out, size_t olen) const;
157         bool getNoiseParams(const std::string &name, NoiseParams &np) const;
158         bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
159         bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
160
161         // return all keys used
162         std::vector<std::string> getNames() const;
163         bool exists(const std::string &name) const;
164
165
166         /***************************************
167          * Getters that don't throw exceptions *
168          ***************************************/
169
170         bool getEntryNoEx(const std::string &name, SettingsEntry &val) const;
171         bool getGroupNoEx(const std::string &name, Settings *&val) const;
172         bool getNoEx(const std::string &name, std::string &val) const;
173         bool getFlag(const std::string &name) const;
174         bool getU16NoEx(const std::string &name, u16 &val) const;
175         bool getS16NoEx(const std::string &name, s16 &val) const;
176         bool getS32NoEx(const std::string &name, s32 &val) const;
177         bool getU64NoEx(const std::string &name, u64 &val) const;
178         bool getFloatNoEx(const std::string &name, float &val) const;
179         bool getV2FNoEx(const std::string &name, v2f &val) const;
180         bool getV3FNoEx(const std::string &name, v3f &val) const;
181         // N.B. getFlagStrNoEx() does not set val, but merely modifies it.  Thus,
182         // val must be initialized before using getFlagStrNoEx().  The intention of
183         // this is to simplify modifying a flags field from a default value.
184         bool getFlagStrNoEx(const std::string &name, u32 &val, FlagDesc *flagdesc) const;
185
186
187         /***********
188          * Setters *
189          ***********/
190
191         // N.B. Groups not allocated with new must be set to NULL in the settings
192         // tree before object destruction.
193         bool setEntry(const std::string &name, const void *entry,
194                 bool set_group, bool set_default);
195         bool set(const std::string &name, const std::string &value);
196         bool setDefault(const std::string &name, const std::string &value);
197         bool setGroup(const std::string &name, Settings *group);
198         bool setGroupDefault(const std::string &name, Settings *group);
199         bool setBool(const std::string &name, bool value);
200         bool setS16(const std::string &name, s16 value);
201         bool setU16(const std::string &name, u16 value);
202         bool setS32(const std::string &name, s32 value);
203         bool setU64(const std::string &name, u64 value);
204         bool setFloat(const std::string &name, float value);
205         bool setV2F(const std::string &name, v2f value);
206         bool setV3F(const std::string &name, v3f value);
207         bool setFlagStr(const std::string &name, u32 flags,
208                 const FlagDesc *flagdesc, u32 flagmask);
209         bool setNoiseParams(const std::string &name, const NoiseParams &np,
210                 bool set_default=false);
211         // N.B. if setStruct() is used to write a non-POD aggregate type,
212         // the behavior is undefined.
213         bool setStruct(const std::string &name, const std::string &format, void *value);
214
215         // remove a setting
216         bool remove(const std::string &name);
217         void clear();
218         void clearDefaults();
219         void updateValue(const Settings &other, const std::string &name);
220         void update(const Settings &other);
221
222         void registerChangedCallback(const std::string &name,
223                 SettingsChangedCallback cbf, void *userdata = NULL);
224         void deregisterChangedCallback(const std::string &name,
225                 SettingsChangedCallback cbf, void *userdata = NULL);
226
227 private:
228         void updateNoLock(const Settings &other);
229         void clearNoLock();
230         void clearDefaultsNoLock();
231
232         void doCallbacks(const std::string &name) const;
233
234         std::map<std::string, SettingsEntry> m_settings;
235         std::map<std::string, SettingsEntry> m_defaults;
236
237         SettingsCallbackMap m_callbacks;
238
239         mutable Mutex m_callback_mutex;
240
241         // All methods that access m_settings/m_defaults directly should lock this.
242         mutable Mutex m_mutex;
243
244 };
245
246 #endif
247