Fri, 15 Mar 2013 20:35:47 +0200
Recolored ends of the conditional line graphic purple instead of red for better distinguishment
0 | 1 | #ifndef __OPTIONS_H__ |
2 | #define __OPTIONS_H__ | |
3 | ||
4 | #include "common.h" | |
5 | #include "str.h" | |
6 | #include "color.h" | |
7 | ||
8 | // ============================================================================= | |
9 | // Determine configuration file. Use APPNAME if given. | |
10 | #ifdef APPNAME | |
11 | #define CONFIGFILE APPNAME ".ini" | |
12 | #else // APPNAME | |
13 | #define APPNAME "(unnamed application)" | |
14 | #define CONFIGFILE "config.ini" | |
15 | #endif // APPNAME | |
16 | ||
17 | #ifdef CONFIG_WITH_QT | |
18 | #include <QString> | |
19 | #endif // CONFIG_WITH_QT | |
20 | ||
21 | // ------------------------------- | |
22 | #define CFGSECTNAME(X) CFGSECT_##X | |
23 | ||
24 | #define MAX_INI_LINE 512 | |
25 | #define NUM_CONFIG (sizeof config::pointers / sizeof *config::pointers) | |
26 | ||
27 | // ============================================================================= | |
28 | enum configsection_e { | |
29 | #define CFG(...) | |
30 | #define SECT(A,B) CFGSECTNAME (A), | |
31 | #include "cfgdef.h" | |
32 | #undef CFG | |
33 | #undef SECT | |
34 | NUM_ConfigSections, | |
35 | NO_CONFIG_SECTION = -1 | |
36 | }; | |
37 | ||
38 | // ============================================================================= | |
39 | enum configtype_e { | |
40 | CONFIG_none, | |
41 | CONFIG_int, | |
42 | CONFIG_str, | |
43 | CONFIG_float, | |
44 | CONFIG_bool, | |
45 | CONFIG_color, | |
46 | }; | |
47 | ||
48 | // ========================================================= | |
49 | class config { | |
50 | public: | |
51 | configsection_e sect; | |
52 | const char* description, *name, *fullname, *typestring, *defaultstring; | |
53 | ||
54 | virtual configtype_e getType () { | |
55 | return CONFIG_none; | |
56 | } | |
57 | ||
58 | virtual void resetValue () {} | |
59 | ||
60 | // ------------------------------------------ | |
61 | static bool load (); | |
62 | static bool save (); | |
63 | static void reset (); | |
64 | static config* pointers[]; | |
65 | static const char* sections[]; | |
66 | static const char* sectionNames[]; | |
67 | static str dirpath (); | |
68 | static str filepath (); | |
69 | }; | |
70 | ||
71 | // ============================================================================= | |
72 | #define DEFINE_UNARY_OPERATOR(T, OP) \ | |
73 | T operator OP () { \ | |
74 | return (OP value); \ | |
75 | } \ | |
76 | ||
77 | #define DEFINE_BINARY_OPERATOR(T, OP) \ | |
78 | T operator OP (const T other) { \ | |
79 | return (value OP other); \ | |
80 | } \ | |
81 | ||
82 | #define DEFINE_ASSIGN_OPERATOR(T, OP) \ | |
83 | T& operator OP (const T other) { \ | |
84 | return (value OP other); \ | |
85 | } \ | |
86 | ||
87 | #define DEFINE_COMPARE_OPERATOR(T, OP) \ | |
88 | bool operator OP (const T other) { \ | |
89 | return (value OP other); \ | |
90 | } \ | |
91 | ||
92 | #define DEFINE_CAST_OPERATOR(T) \ | |
93 | operator T () { \ | |
94 | return (T) value; \ | |
95 | } \ | |
96 | ||
97 | #define DEFINE_ALL_COMPARE_OPERATORS(T) \ | |
98 | DEFINE_COMPARE_OPERATOR (T, ==) \ | |
99 | DEFINE_COMPARE_OPERATOR (T, !=) \ | |
100 | DEFINE_COMPARE_OPERATOR (T, >) \ | |
101 | DEFINE_COMPARE_OPERATOR (T, <) \ | |
102 | DEFINE_COMPARE_OPERATOR (T, >=) \ | |
103 | DEFINE_COMPARE_OPERATOR (T, <=) \ | |
104 | ||
105 | #define DEFINE_INCREMENT_OPERATORS(T) \ | |
106 | T operator++ () {return ++value;} \ | |
107 | T operator++ (int) {return value++;} \ | |
108 | T operator-- () {return --value;} \ | |
109 | T operator-- (int) {return value--;} | |
110 | ||
111 | #define CONFIGTYPE(T) \ | |
112 | class T##config : public config | |
113 | ||
114 | #define IMPLEMENT_CONFIG(T) \ | |
115 | T value, defval; \ | |
116 | \ | |
117 | T##config (const configsection_e _sect, const char* _description, \ | |
118 | T _defval, const char* _name, const char* _fullname, const char* _typestring, \ | |
119 | const char* _defaultstring) \ | |
120 | { \ | |
121 | sect = _sect; \ | |
122 | description = _description; \ | |
123 | value = defval = _defval; \ | |
124 | name = _name; \ | |
125 | fullname = _fullname; \ | |
126 | typestring = _typestring; \ | |
127 | defaultstring = _defaultstring; \ | |
128 | } \ | |
129 | operator T () { \ | |
130 | return value; \ | |
131 | } \ | |
132 | configtype_e getType () { \ | |
133 | return CONFIG_##T; \ | |
134 | } \ | |
135 | void resetValue () { \ | |
136 | value = defval; \ | |
137 | } | |
138 | ||
139 | // ============================================================================= | |
140 | CONFIGTYPE (int) { | |
141 | public: | |
142 | IMPLEMENT_CONFIG (int) | |
143 | ||
144 | // Int-specific operators | |
145 | DEFINE_ALL_COMPARE_OPERATORS (int) | |
146 | DEFINE_INCREMENT_OPERATORS (int) | |
147 | DEFINE_BINARY_OPERATOR (int, +) | |
148 | DEFINE_BINARY_OPERATOR (int, -) | |
149 | DEFINE_BINARY_OPERATOR (int, *) | |
150 | DEFINE_BINARY_OPERATOR (int, /) | |
151 | DEFINE_BINARY_OPERATOR (int, %) | |
152 | DEFINE_BINARY_OPERATOR (int, ^) | |
153 | DEFINE_BINARY_OPERATOR (int, |) | |
154 | DEFINE_BINARY_OPERATOR (int, &) | |
155 | DEFINE_BINARY_OPERATOR (int, >>) | |
156 | DEFINE_BINARY_OPERATOR (int, <<) | |
157 | DEFINE_UNARY_OPERATOR (int, !) | |
158 | DEFINE_UNARY_OPERATOR (int, ~) | |
159 | DEFINE_UNARY_OPERATOR (int, -) | |
160 | DEFINE_UNARY_OPERATOR (int, +) | |
161 | DEFINE_ASSIGN_OPERATOR (int, =) | |
162 | DEFINE_ASSIGN_OPERATOR (int, +=) | |
163 | DEFINE_ASSIGN_OPERATOR (int, -=) | |
164 | DEFINE_ASSIGN_OPERATOR (int, *=) | |
165 | DEFINE_ASSIGN_OPERATOR (int, /=) | |
166 | DEFINE_ASSIGN_OPERATOR (int, %=) | |
167 | DEFINE_ASSIGN_OPERATOR (int, >>=) | |
168 | DEFINE_ASSIGN_OPERATOR (int, <<=) | |
169 | }; | |
170 | ||
171 | // ============================================================================= | |
172 | CONFIGTYPE (str) { | |
173 | public: | |
174 | IMPLEMENT_CONFIG (str) | |
175 | ||
176 | DEFINE_ALL_COMPARE_OPERATORS (str) | |
177 | DEFINE_BINARY_OPERATOR (str, -) | |
178 | DEFINE_BINARY_OPERATOR (str, *) | |
179 | DEFINE_UNARY_OPERATOR (str, !) | |
180 | DEFINE_ASSIGN_OPERATOR (str, =) | |
181 | DEFINE_ASSIGN_OPERATOR (str, +=) | |
182 | DEFINE_ASSIGN_OPERATOR (str, -=) | |
183 | DEFINE_ASSIGN_OPERATOR (str, *=) | |
184 | DEFINE_CAST_OPERATOR (char*) | |
185 | ||
186 | char operator[] (size_t n) { | |
187 | return value[n]; | |
188 | } | |
189 | ||
190 | #ifdef CONFIG_WITH_QT | |
191 | operator QString () { | |
192 | return QString (value.chars()); | |
193 | } | |
194 | #endif // CONFIG_WITH_QT | |
195 | }; | |
196 | ||
197 | // ============================================================================= | |
198 | CONFIGTYPE (float) { | |
199 | public: | |
200 | IMPLEMENT_CONFIG (float) | |
201 | ||
202 | DEFINE_ALL_COMPARE_OPERATORS (float) | |
203 | DEFINE_INCREMENT_OPERATORS (float) | |
204 | DEFINE_BINARY_OPERATOR (float, +) | |
205 | DEFINE_BINARY_OPERATOR (float, -) | |
206 | DEFINE_BINARY_OPERATOR (float, *) | |
207 | DEFINE_UNARY_OPERATOR (float, !) | |
208 | DEFINE_ASSIGN_OPERATOR (float, =) | |
209 | DEFINE_ASSIGN_OPERATOR (float, +=) | |
210 | DEFINE_ASSIGN_OPERATOR (float, -=) | |
211 | DEFINE_ASSIGN_OPERATOR (float, *=) | |
212 | }; | |
213 | ||
214 | // ============================================================================= | |
215 | CONFIGTYPE (bool) { | |
216 | public: | |
217 | IMPLEMENT_CONFIG (bool) | |
218 | DEFINE_ALL_COMPARE_OPERATORS (bool) | |
219 | DEFINE_ASSIGN_OPERATOR (bool, =) | |
220 | }; | |
221 | ||
222 | // ============================================================================= | |
223 | CONFIGTYPE (color) { | |
224 | public: | |
225 | IMPLEMENT_CONFIG (color) | |
226 | // DEFINE_COMPARE_OPERATOR (color, ==) | |
227 | // DEFINE_COMPARE_OPERATOR (color, !=) | |
228 | }; | |
229 | ||
230 | // ============================================================================= | |
231 | // Extern the configurations now | |
232 | #define CFG(TYPE, SECT, NAME, DESCR, DEFAULT) extern TYPE##config SECT##_##NAME; | |
233 | #define SECT(...) | |
234 | #include "cfgdef.h" | |
235 | #undef CFG | |
236 | #undef SECT | |
237 | ||
238 | #endif // __OPTIONS_H__ |