C++11 patchset 2: remove util/cpp11.h and util/cpp11_container.h (#5821)
[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 <list>
28 #include <set>
29
30 class Settings;
31 struct NoiseParams;
32
33 // Global objects
34 extern Settings *g_settings;
35 extern std::string g_settings_path;
36
37 // Type for a settings changed callback function
38 typedef void (*SettingsChangedCallback)(const std::string &name, void *data);
39
40 typedef std::vector<
41         std::pair<
42                 SettingsChangedCallback,
43                 void *
44         >
45 > SettingsCallbackList;
46
47 typedef std::unordered_map<std::string, SettingsCallbackList> SettingsCallbackMap;
48
49 enum ValueType {
50         VALUETYPE_STRING,
51         VALUETYPE_FLAG // Doesn't take any arguments
52 };
53
54 enum SettingsParseEvent {
55         SPE_NONE,
56         SPE_INVALID,
57         SPE_COMMENT,
58         SPE_KVPAIR,
59         SPE_END,
60         SPE_GROUP,
61         SPE_MULTILINE,
62 };
63
64 struct ValueSpec {
65         ValueSpec(ValueType a_type, const char *a_help=NULL)
66         {
67                 type = a_type;
68                 help = a_help;
69         }
70
71         ValueType type;
72         const char *help;
73 };
74
75 struct SettingsEntry {
76         SettingsEntry() :
77                 group(NULL),
78                 is_group(false)
79         {}
80
81         SettingsEntry(const std::string &value_) :
82                 value(value_),
83                 group(NULL),
84                 is_group(false)
85         {}
86
87         SettingsEntry(Settings *group_) :
88                 group(group_),
89                 is_group(true)
90         {}
91
92         std::string value;
93         Settings *group;
94         bool is_group;
95 };
96
97 typedef std::unordered_map<std::string, SettingsEntry> SettingEntries;
98
99 class Settings {
100 public:
101         Settings() {}
102         ~Settings();
103
104         Settings & operator += (const Settings &other);
105         Settings & operator = (const Settings &other);
106
107         /***********************
108          * Reading and writing *
109          ***********************/
110
111         // Read configuration file.  Returns success.
112         bool readConfigFile(const char *filename);
113         //Updates configuration file.  Returns success.
114         bool updateConfigFile(const char *filename);
115         // NOTE: Types of allowed_options are ignored.  Returns success.
116         bool parseCommandLine(int argc, char *argv[],
117                         std::map<std::string, ValueSpec> &allowed_options);
118         bool parseConfigLines(std::istream &is, const std::string &end = "");
119         void writeLines(std::ostream &os, u32 tab_depth=0) const;
120
121         SettingsParseEvent parseConfigObject(const std::string &line,
122                 const std::string &end, std::string &name, std::string &value);
123         bool updateConfigObject(std::istream &is, std::ostream &os,
124                 const std::string &end, u32 tab_depth=0);
125
126         static bool checkNameValid(const std::string &name);
127         static bool checkValueValid(const std::string &value);
128         static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
129         static void printEntry(std::ostream &os, const std::string &name,
130                 const SettingsEntry &entry, u32 tab_depth=0);
131
132         /***********
133          * Getters *
134          ***********/
135
136         const SettingsEntry &getEntry(const std::string &name) const;
137         Settings *getGroup(const std::string &name) const;
138         const std::string &get(const std::string &name) const;
139         bool getBool(const std::string &name) const;
140         u16 getU16(const std::string &name) const;
141         s16 getS16(const std::string &name) const;
142         s32 getS32(const std::string &name) const;
143         u64 getU64(const std::string &name) const;
144         float getFloat(const std::string &name) const;
145         v2f getV2F(const std::string &name) const;
146         v3f getV3F(const std::string &name) const;
147         u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
148                         u32 *flagmask) const;
149         // N.B. if getStruct() is used to read a non-POD aggregate type,
150         // the behavior is undefined.
151         bool getStruct(const std::string &name, const std::string &format,
152                         void *out, size_t olen) const;
153         bool getNoiseParams(const std::string &name, NoiseParams &np) const;
154         bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
155         bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
156
157         // return all keys used
158         std::vector<std::string> getNames() const;
159         bool exists(const std::string &name) const;
160
161
162         /***************************************
163          * Getters that don't throw exceptions *
164          ***************************************/
165
166         bool getEntryNoEx(const std::string &name, SettingsEntry &val) const;
167         bool getGroupNoEx(const std::string &name, Settings *&val) const;
168         bool getNoEx(const std::string &name, std::string &val) const;
169         bool getFlag(const std::string &name) const;
170         bool getU16NoEx(const std::string &name, u16 &val) const;
171         bool getS16NoEx(const std::string &name, s16 &val) const;
172         bool getS32NoEx(const std::string &name, s32 &val) const;
173         bool getU64NoEx(const std::string &name, u64 &val) const;
174         bool getFloatNoEx(const std::string &name, float &val) const;
175         bool getV2FNoEx(const std::string &name, v2f &val) const;
176         bool getV3FNoEx(const std::string &name, v3f &val) const;
177         // N.B. getFlagStrNoEx() does not set val, but merely modifies it.  Thus,
178         // val must be initialized before using getFlagStrNoEx().  The intention of
179         // this is to simplify modifying a flags field from a default value.
180         bool getFlagStrNoEx(const std::string &name, u32 &val, FlagDesc *flagdesc) const;
181
182
183         /***********
184          * Setters *
185          ***********/
186
187         // N.B. Groups not allocated with new must be set to NULL in the settings
188         // tree before object destruction.
189         bool setEntry(const std::string &name, const void *entry,
190                 bool set_group, bool set_default);
191         bool set(const std::string &name, const std::string &value);
192         bool setDefault(const std::string &name, const std::string &value);
193         bool setGroup(const std::string &name, Settings *group);
194         bool setGroupDefault(const std::string &name, Settings *group);
195         bool setBool(const std::string &name, bool value);
196         bool setS16(const std::string &name, s16 value);
197         bool setU16(const std::string &name, u16 value);
198         bool setS32(const std::string &name, s32 value);
199         bool setU64(const std::string &name, u64 value);
200         bool setFloat(const std::string &name, float value);
201         bool setV2F(const std::string &name, v2f value);
202         bool setV3F(const std::string &name, v3f value);
203         bool setFlagStr(const std::string &name, u32 flags,
204                 const FlagDesc *flagdesc, u32 flagmask);
205         bool setNoiseParams(const std::string &name, const NoiseParams &np,
206                 bool set_default=false);
207         // N.B. if setStruct() is used to write a non-POD aggregate type,
208         // the behavior is undefined.
209         bool setStruct(const std::string &name, const std::string &format, void *value);
210
211         // remove a setting
212         bool remove(const std::string &name);
213         void clear();
214         void clearDefaults();
215         void updateValue(const Settings &other, const std::string &name);
216         void update(const Settings &other);
217
218         void registerChangedCallback(const std::string &name,
219                 SettingsChangedCallback cbf, void *userdata = NULL);
220         void deregisterChangedCallback(const std::string &name,
221                 SettingsChangedCallback cbf, void *userdata = NULL);
222
223 private:
224         void updateNoLock(const Settings &other);
225         void clearNoLock();
226         void clearDefaultsNoLock();
227
228         void doCallbacks(const std::string &name) const;
229
230         SettingEntries m_settings;
231         SettingEntries m_defaults;
232
233         SettingsCallbackMap m_callbacks;
234
235         mutable Mutex m_callback_mutex;
236
237         // All methods that access m_settings/m_defaults directly should lock this.
238         mutable Mutex m_mutex;
239
240 };
241
242 #endif
243