Refactor settings to library/
[lsnes.git] / include / library / settings.hpp
blob2b19fac45ff096117f3ad47179c545a79f42f107
1 #ifndef _library__settings__hpp__included__
2 #define _library__settings__hpp__included__
4 #include <string>
5 #include <set>
6 #include <stdexcept>
7 #include <iostream>
8 #include <map>
9 #include <list>
10 #include "library/workthread.hpp"
12 class setting;
13 class setting_group;
15 /**
16 * A settings listener.
18 struct setting_listener
20 /**
21 * Destructor.
23 virtual ~setting_listener() throw();
24 /**
25 * Listen for setting being blanked.
27 virtual void blanked(setting_group& group, const std::string& sname) = 0;
28 /**
29 * Listen for setting changing value.
31 virtual void changed(setting_group& group, const std::string& sname, const std::string& newval) = 0;
34 /**
35 * A group of settings.
37 class setting_group
39 public:
40 /**
41 * Create a new group of settings.
43 setting_group() throw(std::bad_alloc);
44 /**
45 * Destroy a group of settings.
47 ~setting_group() throw();
48 /**
49 * Can the setting be blanked?
51 bool blankable(const std::string& name) throw(std::bad_alloc, std::runtime_error);
52 /**
53 * Look up setting and try to blank it.
55 * parameter name: Name of setting to blank.
56 * throws std::bad_alloc: Not enough memory.
57 * throws std::runtime_error: Blanking this setting is not allowed (currently). Or setting does not exist.
59 void blank(const std::string& name) throw(std::bad_alloc, std::runtime_error);
60 /**
61 * Look up a setting and see if it is set (not blanked)?
63 * parameter name: Name of setting to check.
64 * returns: True if setting is not blanked, false if it is blanked.
65 * throws std::bad_alloc: Not enough memory.
66 * throws std::runtime_error: Setting does not exist.
68 bool is_set(const std::string& name) throw(std::bad_alloc, std::runtime_error);
69 /**
70 * Look up setting and set it.
72 * parameter name: Name of the setting.
73 * parameter value: New value for setting.
74 * throws std::bad_alloc: Not enough memory.
75 * throws std::runtime_error: Setting the setting to this value is not allowed (currently). Or setting does not exist.
77 void set(const std::string& name, const std::string& value) throw(std::bad_alloc, std::runtime_error);
78 /**
79 * Look up setting an get value of it.
81 * returns: The setting value.
82 * throws std::bad_alloc: Not enough memory.
83 * throws std::runtime_error: Setting does not exist.
85 std::string get(const std::string& name) throw(std::bad_alloc, std::runtime_error);
86 /**
87 * Get set of all settings.
89 std::set<std::string> get_settings_set() throw(std::bad_alloc);
90 /**
91 * Enable/Disable storage mode.
93 * In storage mode, invalid values are stored in addition to being rejected.
95 void set_storage_mode(bool enable) throw();
96 /**
97 * Get invalid settings cache.
99 std::map<std::string, std::string> get_invalid_values() throw(std::bad_alloc);
101 * Add a listener.
103 void add_listener(struct setting_listener& listener) throw(std::bad_alloc);
105 * Remove a listener.
107 void remove_listener(struct setting_listener& listener) throw(std::bad_alloc);
109 * Register a setting.
111 void register_setting(const std::string& name, setting& _setting) throw(std::bad_alloc);
113 * Unregister a setting.
115 void unregister_setting(const std::string& name) throw(std::bad_alloc);
116 private:
117 setting* get_by_name(const std::string& name);
118 std::map<std::string, class setting*> settings;
119 std::map<std::string, std::string> invalid_values;
120 std::set<struct setting_listener*> listeners;
121 mutex_class lock;
122 bool storage_mode;
126 * A setting.
128 class setting
130 public:
132 * Create new setting.
134 * parameter group: The group setting is in.
135 * parameter name: Name of the setting.
136 * throws std::bad_alloc: Not enough memory.
138 setting(setting_group& group, const std::string& name) throw(std::bad_alloc);
141 * Remove the setting.
143 ~setting() throw();
145 * Set the setting to special blank state. Not all settings can be blanked.
147 * Parameter really: Do dummy clear if false, otherwise try it for real.
148 * Returns: True on success, false on failure.
149 * throws std::bad_alloc: Not enough memory.
150 * throws std::runtime_error: Blanking this setting is not allowed (currently).
152 virtual bool blank(bool really) throw(std::bad_alloc, std::runtime_error);
155 * Is this setting set (not blanked)?
157 * returns: True if setting is not blanked, false if it is blanked.
159 virtual bool is_set() throw() = 0;
162 * Set value of setting.
164 * parameter value: New value for setting.
165 * throws std::bad_alloc: Not enough memory.
166 * throws std::runtime_error: Setting the setting to this value is not allowed (currently).
168 virtual void set(const std::string& value) throw(std::bad_alloc, std::runtime_error) = 0;
170 * Get the value of setting.
172 * returns: The setting value.
173 * throws std::bad_alloc: Not enough memory.
175 virtual std::string get() throw(std::bad_alloc) = 0;
177 * Lock holder
179 struct lock_holder
181 lock_holder(setting* t) { (targ = t)->mut.lock(); }
182 ~lock_holder() { targ->mut.unlock(); }
183 private:
184 setting* targ;
186 friend struct lock_holder;
187 protected:
188 std::string settingname;
189 private:
190 mutex_class mut;
191 setting_group& in_group;
195 * Setting having numeric value.
197 class numeric_setting : public setting
199 public:
201 * Create a new numeric setting.
203 * parameter group: Group setting is in.
204 * parameter sname: Name of the setting.
205 * parameter minv: Minimum value for the setting.
206 * parameter maxv: Maximum value for the setting.
207 * parameter dflt: Default (initial) value for the setting.
208 * throws std::bad_alloc: Not enough memory.
210 numeric_setting(setting_group& group, const std::string& sname, int32_t minv, int32_t maxv, int32_t dflt)
211 throw(std::bad_alloc);
213 * Returns true (these settings are always set).
215 bool is_set() throw();
217 * Set the value of setting. Accepts only numeric values.
219 * parameter value: New value.
220 * throws std::bad_alloc: Not enough memory.
221 * throws std::runtime_error: Invalid value.
223 void set(const std::string& value) throw(std::bad_alloc, std::runtime_error);
225 * Gets the value of the setting.
227 * returns: Value of setting as string.
228 * throws std::bad_alloc: Not enough memory.
230 std::string get() throw(std::bad_alloc);
232 * Get the value of setting as numeric.
234 * returns: Value of the setting as numeric.
236 operator int32_t() throw();
237 private:
238 int32_t value;
239 int32_t minimum;
240 int32_t maximum;
244 * Setting having boolean value.
246 class boolean_setting : public setting
248 public:
250 * Create a new boolean setting.
252 * parameter group: Group setting is in.
253 * parameter sname: Name of the setting.
254 * parameter dflt: Default (initial) value for the setting.
255 * throws std::bad_alloc: Not enough memory.
257 boolean_setting(setting_group& group, const std::string& sname, bool dflt) throw(std::bad_alloc);
259 * Returns true (these settings are always set).
261 bool is_set() throw();
263 * Set the value of setting.
265 * The following values are accepted as true: true, yes, on, 1, enable and enabled.
266 * The following values are accepted as false: false, no, off, 0, disable and disabled.
268 * parameter value: New value.
269 * throws std::bad_alloc: Not enough memory.
270 * throws std::runtime_error: Invalid value.
272 void set(const std::string& value) throw(std::bad_alloc, std::runtime_error);
274 * Gets the value of the setting.
276 * returns: Value of setting as string.
277 * throws std::bad_alloc: Not enough memory.
279 std::string get() throw(std::bad_alloc);
281 * Get the value of setting as boolean.
283 * returns: Value of the setting as boolean.
285 operator bool() throw();
286 private:
287 bool value;
291 * Setting having path value.
293 class path_setting : public setting
295 public:
297 * Create a new path setting.
299 * Parameter group: The group setting is in.
300 * Parameter sname: Name of the setting.
302 path_setting(setting_group& group, const std::string& sname) throw(std::bad_alloc);
303 bool blank(bool really) throw(std::bad_alloc, std::runtime_error);
304 bool is_set() throw();
305 void set(const std::string& value) throw(std::bad_alloc, std::runtime_error);
306 std::string get() throw(std::bad_alloc);
308 * Read the value of the setting.
310 operator std::string();
311 private:
312 bool _default;
313 std::string path;
316 #endif