src/config.h

changeset 590
7aec744ce97b
parent 587
507e5d5c348c
child 600
209e3f1f7b2c
equal deleted inserted replaced
589:1417828920e6 590:7aec744ce97b
17 */ 17 */
18 18
19 #ifndef LDFORGE_CONFIG_H 19 #ifndef LDFORGE_CONFIG_H
20 #define LDFORGE_CONFIG_H 20 #define LDFORGE_CONFIG_H
21 21
22 #include "property.h"
23
22 // ============================================================================= 24 // =============================================================================
23 #include <QString> 25 #include <QString>
24 #include <QVariant> 26 #include <QVariant>
25 #include <QKeySequence> 27 #include <QKeySequence>
26 class QSettings; 28 class QSettings;
29 typedef QString str; 31 typedef QString str;
30 32
31 #define MAX_INI_LINE 512 33 #define MAX_INI_LINE 512
32 #define MAX_CONFIG 512 34 #define MAX_CONFIG 512
33 35
34 #define cfg(T, NAME, DEFAULT) T##Config NAME (DEFAULT, #NAME, #DEFAULT) 36 #define cfg(T, NAME, DEFAULT) \
35 #define extern_cfg(T, NAME) extern T##Config NAME 37 Config::T##Type NAME; \
38 T##Config config_##NAME (&NAME, #NAME, DEFAULT);
39
40 #define extern_cfg(T, NAME) extern Config::T##Type NAME;
36 41
37 // ========================================================= 42 // =========================================================
38 class Config 43 class Config
39 { public: 44 { PROPERTY (private, str, Name, STR_OPS, STOCK_WRITE)
45
46 public:
40 enum Type 47 enum Type
41 { Int, 48 { Int,
42 String, 49 String,
43 Float, 50 Float,
44 Bool, 51 Bool,
45 KeySequence, 52 KeySequence,
46 List, 53 List,
47 }; 54 };
48 55
49 Config (const char* name, const char* defstring); 56 using IntType = int;
50 const char* name; 57 using StringType = QString;
58 using FloatType = float;
59 using BoolType = bool;
60 using KeySequenceType = QKeySequence;
61 using ListType = QList<QVariant>;
51 62
52 virtual Type getType() const 63 Config (str name);
53 { return (Type) 0;
54 }
55 64
56 virtual void resetValue() {} 65 virtual QVariant getDefaultAsVariant() const = 0;
57 virtual void loadFromVariant (const QVariant& val) 66 virtual Type getType() const = 0;
58 { (void) val; 67 virtual bool isDefault() const = 0;
59 } 68 virtual void loadFromVariant (const QVariant& val) = 0;
60 69 virtual void resetValue() = 0;
61 virtual bool isDefault() const 70 virtual QVariant toVariant() const = 0;
62 { return false;
63 }
64
65 virtual QVariant toVariant() const
66 { return QVariant();
67 }
68
69 virtual QVariant defaultVariant() const
70 { return QVariant();
71 }
72 71
73 // ------------------------------------------ 72 // ------------------------------------------
74 static bool load(); 73 static bool load();
75 static bool save(); 74 static bool save();
76 static void reset(); 75 static void reset();
77 static str dirpath(); 76 static str dirpath();
78 static str filepath (str file); 77 static str filepath (str file);
79 78
80 protected: 79 protected:
81 static void addToArray (Config* ptr); 80 static void addToArray (Config* ptr);
82
83 private:
84 const char* m_defstring;
85 }; 81 };
86 82
87 // ============================================================================= 83 // =============================================================================
88 #define IMPLEMENT_CONFIG(NAME, T) \ 84 #define IMPLEMENT_CONFIG(NAME) \
89 T value, defval; \ 85 public: \
90 NAME##Config (T defval, const char* name, const char* defstring) : \ 86 using ValueType = Config::NAME##Type; \
91 Config (name, defstring), value (defval), defval (defval) \ 87 \
92 { Config::addToArray (this); } \ 88 NAME##Config (ValueType* valueptr, str name, ValueType def) : \
93 \ 89 Config (name), \
94 operator const T&() const { return value; } \ 90 m_valueptr (valueptr), \
95 Config::Type getType() const override { return Config::NAME; } \ 91 m_default (def) \
96 virtual void resetValue() override { value = defval; } \ 92 { Config::addToArray (this); \
97 virtual bool isDefault() const override { return value == defval; } \ 93 *m_valueptr = def; \
98 virtual QVariant toVariant() const override { return QVariant::fromValue<T> (value); } \ 94 } \
99 virtual QVariant defaultVariant() const override { return QVariant::fromValue<T> (defval); } \ 95 \
100 virtual void loadFromVariant (const QVariant& val) override { value = val.value<T>(); } \ 96 inline ValueType getValue() const \
101 static NAME##Config& getByName (str name); 97 { return *m_valueptr; \
102 98 } \
103 #define DEFINE_UNARY_OPERATOR(T, OP) \ 99 \
104 T operator OP() { \ 100 inline void setValue (ValueType val) \
105 return OP value; \ 101 { *m_valueptr = val; \
106 } 102 } \
107 103 \
108 #define DEFINE_BINARY_OPERATOR(T, OP) \ 104 virtual Config::Type getType() const \
109 T operator OP (const T other) { \ 105 { return Config::NAME; \
110 return value OP other; \ 106 } \
111 } 107 \
112 108 virtual void resetValue() \
113 #define DEFINE_ASSIGN_OPERATOR(T, OP) \ 109 { *m_valueptr = m_default; \
114 T& operator OP (const T other) { \ 110 } \
115 return value OP other; \ 111 \
116 } 112 virtual const ValueType& getDefault() const \
117 113 { return m_default; \
118 #define DEFINE_COMPARE_OPERATOR(T, OP) \ 114 } \
119 bool operator OP (const T other) { \ 115 \
120 return value OP other; \ 116 virtual bool isDefault() const \
121 } 117 { return *m_valueptr == m_default; \
122 118 } \
123 #define DEFINE_CAST_OPERATOR(T) \ 119 \
124 operator T() { \ 120 virtual void loadFromVariant (const QVariant& val) \
125 return (T) value; \ 121 { *m_valueptr = val.value<ValueType>(); \
126 } 122 } \
127 123 \
128 #define DEFINE_ALL_COMPARE_OPERATORS(T) \ 124 virtual QVariant toVariant() const \
129 DEFINE_COMPARE_OPERATOR (T, ==) \ 125 { return QVariant::fromValue<ValueType> (*m_valueptr); \
130 DEFINE_COMPARE_OPERATOR (T, !=) \ 126 } \
131 DEFINE_COMPARE_OPERATOR (T, >) \ 127 \
132 DEFINE_COMPARE_OPERATOR (T, <) \ 128 virtual QVariant getDefaultAsVariant() const \
133 DEFINE_COMPARE_OPERATOR (T, >=) \ 129 { return QVariant::fromValue<ValueType> (m_default); \
134 DEFINE_COMPARE_OPERATOR (T, <=) \ 130 } \
135 131 \
136 #define DEFINE_INCREMENT_OPERATORS(T) \ 132 static NAME##Config* getByName (str name); \
137 T operator++() { return ++value; } \ 133 \
138 T operator++(int) { return value++; } \ 134 private: \
139 T operator--() { return --value; } \ 135 ValueType* m_valueptr; \
140 T operator--(int) { return value--; } 136 ValueType m_default;
141 137
142 // ============================================================================= 138 // =============================================================================
143 class IntConfig : public Config 139 class IntConfig : public Config
144 { public: 140 { IMPLEMENT_CONFIG (Int)
145 IMPLEMENT_CONFIG (Int, int)
146 DEFINE_ALL_COMPARE_OPERATORS (int)
147 DEFINE_INCREMENT_OPERATORS (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_BINARY_OPERATOR (int, <<)
158 DEFINE_UNARY_OPERATOR (int, !)
159 DEFINE_UNARY_OPERATOR (int, ~)
160 DEFINE_UNARY_OPERATOR (int, -)
161 DEFINE_UNARY_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 DEFINE_ASSIGN_OPERATOR (int, <<=)
170 }; 141 };
171 142
172 // ============================================================================= 143 // =============================================================================
173 class StringConfig : public Config 144 class StringConfig : public Config
174 { public: 145 { IMPLEMENT_CONFIG (String)
175 IMPLEMENT_CONFIG (String, str)
176
177 DEFINE_COMPARE_OPERATOR (str, ==)
178 DEFINE_COMPARE_OPERATOR (str, !=)
179 DEFINE_ASSIGN_OPERATOR (str, =)
180 DEFINE_ASSIGN_OPERATOR (str, +=)
181
182 QChar operator[] (int n)
183 { return value[n];
184 }
185 }; 146 };
186 147
187 // ============================================================================= 148 // =============================================================================
188 class FloatConfig : public Config 149 class FloatConfig : public Config
189 { public: 150 { IMPLEMENT_CONFIG (Float)
190 IMPLEMENT_CONFIG (Float, float)
191 DEFINE_ALL_COMPARE_OPERATORS (float)
192 DEFINE_INCREMENT_OPERATORS (float)
193 DEFINE_BINARY_OPERATOR (float, +)
194 DEFINE_BINARY_OPERATOR (float, -)
195 DEFINE_BINARY_OPERATOR (float, *)
196 DEFINE_UNARY_OPERATOR (float, !)
197 DEFINE_ASSIGN_OPERATOR (float, =)
198 DEFINE_ASSIGN_OPERATOR (float, +=)
199 DEFINE_ASSIGN_OPERATOR (float, -=)
200 DEFINE_ASSIGN_OPERATOR (float, *=)
201 }; 151 };
202 152
203 // ============================================================================= 153 // =============================================================================
204 class BoolConfig : public Config 154 class BoolConfig : public Config
205 { public: 155 { IMPLEMENT_CONFIG (Bool)
206 IMPLEMENT_CONFIG (Bool, bool)
207 DEFINE_ALL_COMPARE_OPERATORS (bool)
208 DEFINE_ASSIGN_OPERATOR (bool, =)
209 }; 156 };
210 157
211 // ============================================================================= 158 // =============================================================================
212 class KeySequenceConfig : public Config 159 class KeySequenceConfig : public Config
213 { public: 160 { IMPLEMENT_CONFIG (KeySequence)
214 IMPLEMENT_CONFIG (KeySequence, QKeySequence)
215 DEFINE_ALL_COMPARE_OPERATORS (QKeySequence)
216 DEFINE_ASSIGN_OPERATOR (QKeySequence, =)
217 }; 161 };
218 162
219 // ============================================================================= 163 // =============================================================================
220 class ListConfig : public Config 164 class ListConfig : public Config
221 { public: 165 { IMPLEMENT_CONFIG (List)
222 IMPLEMENT_CONFIG (List, QList<QVariant>)
223 DEFINE_ASSIGN_OPERATOR (QList<QVariant>, =)
224
225 typedef QList<QVariant>::iterator it;
226 typedef QList<QVariant>::const_iterator c_it;
227
228 it begin()
229 { return value.begin();
230 }
231
232 c_it begin() const
233 { return value.constBegin();
234 }
235
236 it end()
237 { return value.end();
238 }
239
240 c_it end() const
241 { return value.constEnd();
242 }
243 }; 166 };
244 167
245 #endif // LDFORGE_CONFIG_H 168 #endif // LDFORGE_CONFIG_H

mercurial