src/config.h

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

mercurial