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