src/config.h

changeset 183
f1b8cb53d2a2
child 184
fae3bc9ce319
equal deleted inserted replaced
182:9374fea8f77f 183:f1b8cb53d2a2
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 Santeri Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 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 General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef CONFIG_H
20 #define CONFIG_H
21
22 #include "common.h"
23 #include "str.h"
24
25 // =============================================================================
26 #include <QString>
27 #include <qkeysequence.h>
28
29 #define MAX_INI_LINE 512
30 #define NUM_CONFIG (g_pConfigPointers.size ())
31
32 #define cfg(T, NAME, DEFAULT) \
33 T##config NAME (DEFAULT, #NAME, #T, #DEFAULT)
34
35 #define extern_cfg(T, NAME) \
36 extern T##config NAME
37
38 // =============================================================================
39 enum configtype_e {
40 CONFIG_none,
41 CONFIG_int,
42 CONFIG_str,
43 CONFIG_float,
44 CONFIG_bool,
45 CONFIG_keyseq,
46 };
47
48 // =========================================================
49 class config {
50 public:
51 const char* name, *typestring, *defaultstring;
52
53 virtual configtype_e getType () {
54 return CONFIG_none;
55 }
56
57 virtual void resetValue () {}
58
59 // ------------------------------------------
60 static bool load ();
61 static bool save ();
62 static void reset ();
63 static str dirpath ();
64 static str filepath ();
65 };
66
67 extern std::vector<config*> g_configPointers;
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 (T _defval, const char* _name, const char* _typestring, \
116 const char* _defaultstring) \
117 { \
118 value = defval = _defval; \
119 name = _name; \
120 typestring = _typestring; \
121 defaultstring = _defaultstring; \
122 g_pConfigPointers.push_back (this); \
123 } \
124 operator T () { \
125 return value; \
126 } \
127 configtype_e getType () { \
128 return CONFIG_##T; \
129 } \
130 virtual void resetValue () { \
131 value = defval; \
132 }
133
134 // =============================================================================
135 CONFIGTYPE (int) {
136 public:
137 IMPLEMENT_CONFIG (int)
138
139 // Int-specific operators
140 DEFINE_ALL_COMPARE_OPERATORS (int)
141 DEFINE_INCREMENT_OPERATORS (int)
142 DEFINE_BINARY_OPERATOR (int, +)
143 DEFINE_BINARY_OPERATOR (int, -)
144 DEFINE_BINARY_OPERATOR (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_UNARY_OPERATOR (int, !)
153 DEFINE_UNARY_OPERATOR (int, ~)
154 DEFINE_UNARY_OPERATOR (int, -)
155 DEFINE_UNARY_OPERATOR (int, +)
156 DEFINE_ASSIGN_OPERATOR (int, =)
157 DEFINE_ASSIGN_OPERATOR (int, +=)
158 DEFINE_ASSIGN_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 };
165
166 // =============================================================================
167 CONFIGTYPE (str) {
168 public:
169 IMPLEMENT_CONFIG (str)
170
171 DEFINE_ALL_COMPARE_OPERATORS (str)
172 DEFINE_BINARY_OPERATOR (str, -)
173 DEFINE_BINARY_OPERATOR (str, *)
174 DEFINE_UNARY_OPERATOR (str, !)
175 DEFINE_ASSIGN_OPERATOR (str, =)
176 DEFINE_ASSIGN_OPERATOR (str, +=)
177 DEFINE_ASSIGN_OPERATOR (str, -=)
178 DEFINE_ASSIGN_OPERATOR (str, *=)
179 DEFINE_CAST_OPERATOR (char*)
180
181 char operator[] (size_t n) {
182 return value[n];
183 }
184
185 #ifdef CONFIG_WITH_QT
186 operator QString () {
187 return QString (value.chars());
188 }
189 #endif // CONFIG_WITH_QT
190 };
191
192 // =============================================================================
193 CONFIGTYPE (float) {
194 public:
195 IMPLEMENT_CONFIG (float)
196
197 DEFINE_ALL_COMPARE_OPERATORS (float)
198 DEFINE_INCREMENT_OPERATORS (float)
199 DEFINE_BINARY_OPERATOR (float, +)
200 DEFINE_BINARY_OPERATOR (float, -)
201 DEFINE_BINARY_OPERATOR (float, *)
202 DEFINE_UNARY_OPERATOR (float, !)
203 DEFINE_ASSIGN_OPERATOR (float, =)
204 DEFINE_ASSIGN_OPERATOR (float, +=)
205 DEFINE_ASSIGN_OPERATOR (float, -=)
206 DEFINE_ASSIGN_OPERATOR (float, *=)
207 };
208
209 // =============================================================================
210 CONFIGTYPE (bool) {
211 public:
212 IMPLEMENT_CONFIG (bool)
213 DEFINE_ALL_COMPARE_OPERATORS (bool)
214 DEFINE_ASSIGN_OPERATOR (bool, =)
215 };
216
217 // =============================================================================
218 typedef QKeySequence keyseq;
219
220 CONFIGTYPE (keyseq) {
221 public:
222 IMPLEMENT_CONFIG (keyseq)
223 DEFINE_ALL_COMPARE_OPERATORS (keyseq)
224 DEFINE_ASSIGN_OPERATOR (keyseq, =)
225 };
226
227 #endif // CONFIG_H

mercurial