src/config.h

changeset 461
fbcc91ae1dd2
parent 436
4268a5507725
child 462
55173a35eea5
equal deleted inserted replaced
460:b230ae09c8e5 461:fbcc91ae1dd2
30 typedef QString str; 30 typedef QString str;
31 31
32 #define MAX_INI_LINE 512 32 #define MAX_INI_LINE 512
33 #define MAX_CONFIG 512 33 #define MAX_CONFIG 512
34 34
35 #define cfg(T, NAME, DEFAULT) T##config NAME (DEFAULT, #NAME, #DEFAULT) 35 #define cfg(T, NAME, DEFAULT) T##Config NAME (DEFAULT, #NAME, #DEFAULT)
36 #define extern_cfg(T, NAME) extern T##config NAME 36 #define extern_cfg(T, NAME) extern T##Config NAME
37 37
38 // ========================================================= 38 // =========================================================
39 class config { 39 class Config {
40 public: 40 public:
41 enum Type { 41 enum Type {
42 Type_none, 42 None,
43 Type_int, 43 Int,
44 Type_str, 44 String,
45 Type_float, 45 Float,
46 Type_bool, 46 Bool,
47 Type_keyseq, 47 KeySequence,
48 }; 48 };
49 49
50 config (const char* defstring) : m_defstring (defstring) {} 50 Config (const char* name, const char* defstring) :
51 name (name), m_defstring (defstring) {}
51 const char* name; 52 const char* name;
52 53
53 virtual Type getType() const { 54 virtual Type getType() const {
54 return Type_none; 55 return None;
55 } 56 }
56 57
57 str toString() const; 58 str toString() const;
58 str defaultString() const; 59 str defaultString() const;
59 virtual void resetValue() {} 60 virtual void resetValue() {}
69 70
70 private: 71 private:
71 const char* m_defstring; 72 const char* m_defstring;
72 }; 73 };
73 74
74 void addConfig (config* ptr); 75 void addConfig (Config* ptr);
75 76
76 // ============================================================================= 77 // =============================================================================
77 #define DEFINE_UNARY_OPERATOR(T, OP) \ 78 #define DEFINE_UNARY_OPERATOR(T, OP) \
78 T operator OP() { \ 79 T operator OP() { \
79 return OP value; \ 80 return OP value; \
80 } \ 81 }
81 82
82 #define DEFINE_BINARY_OPERATOR(T, OP) \ 83 #define DEFINE_BINARY_OPERATOR(T, OP) \
83 T operator OP (const T other) { \ 84 T operator OP (const T other) { \
84 return value OP other; \ 85 return value OP other; \
85 } \ 86 }
86 87
87 #define DEFINE_ASSIGN_OPERATOR(T, OP) \ 88 #define DEFINE_ASSIGN_OPERATOR(T, OP) \
88 T& operator OP (const T other) { \ 89 T& operator OP (const T other) { \
89 return value OP other; \ 90 return value OP other; \
90 } \ 91 }
91 92
92 #define DEFINE_COMPARE_OPERATOR(T, OP) \ 93 #define DEFINE_COMPARE_OPERATOR(T, OP) \
93 bool operator OP (const T other) { \ 94 bool operator OP (const T other) { \
94 return value OP other; \ 95 return value OP other; \
95 } \ 96 }
96 97
97 #define DEFINE_CAST_OPERATOR(T) \ 98 #define DEFINE_CAST_OPERATOR(T) \
98 operator T() { \ 99 operator T() { \
99 return (T) value; \ 100 return (T) value; \
100 } \ 101 }
101 102
102 #define DEFINE_ALL_COMPARE_OPERATORS(T) \ 103 #define DEFINE_ALL_COMPARE_OPERATORS(T) \
103 DEFINE_COMPARE_OPERATOR (T, ==) \ 104 DEFINE_COMPARE_OPERATOR (T, ==) \
104 DEFINE_COMPARE_OPERATOR (T, !=) \ 105 DEFINE_COMPARE_OPERATOR (T, !=) \
105 DEFINE_COMPARE_OPERATOR (T, >) \ 106 DEFINE_COMPARE_OPERATOR (T, >) \
106 DEFINE_COMPARE_OPERATOR (T, <) \ 107 DEFINE_COMPARE_OPERATOR (T, <) \
112 T operator++(int) { return value++; } \ 113 T operator++(int) { return value++; } \
113 T operator--() { return --value; } \ 114 T operator--() { return --value; } \
114 T operator--(int) { return value--; } 115 T operator--(int) { return value--; }
115 116
116 #define CONFIGTYPE(T) \ 117 #define CONFIGTYPE(T) \
117 class T##config : public config 118 class T##Config : public Config
118 119
119 #define IMPLEMENT_CONFIG(T) \ 120 #define IMPLEMENT_CONFIG(NAME, T) \
120 T value, defval; \ 121 T value, defval; \
122 NAME##Config (T defval, const char* name, const char* defstring) : \
123 Config (name, defstring), value (defval), defval (defval) \
124 { addConfig (this); } \
121 \ 125 \
122 T##config (T _defval, const char* _name, const char* defstring) : config (defstring) { \ 126 operator const T&() const { return value; } \
123 value = defval = _defval; \ 127 Config::Type getType() const override { return Config::NAME; } \
124 name = _name; \ 128 virtual void resetValue() { value = defval; } \
125 addConfig (this); \ 129 virtual bool isDefault() const { return value == defval; } \
126 } \
127 operator const T&() const { \
128 return value; \
129 } \
130 config::Type getType() const override { \
131 return config::Type_##T; \
132 } \
133 virtual void resetValue() { \
134 value = defval; \
135 } \
136 virtual bool isDefault() const { \
137 return value == defval; \
138 } \
139 virtual void loadFromConfig (const QSettings* cfg) override; 130 virtual void loadFromConfig (const QSettings* cfg) override;
140 131
141 // ============================================================================= 132 // =============================================================================
142 CONFIGTYPE (int) { 133 CONFIGTYPE (Int) {
143 public: 134 public:
144 IMPLEMENT_CONFIG (int) 135 IMPLEMENT_CONFIG (Int, int)
145 DEFINE_ALL_COMPARE_OPERATORS (int) 136 DEFINE_ALL_COMPARE_OPERATORS (int)
146 DEFINE_INCREMENT_OPERATORS (int) 137 DEFINE_INCREMENT_OPERATORS (int)
147 DEFINE_BINARY_OPERATOR (int, +) 138 DEFINE_BINARY_OPERATOR (int, +)
148 DEFINE_BINARY_OPERATOR (int, -) 139 DEFINE_BINARY_OPERATOR (int, -)
149 DEFINE_BINARY_OPERATOR (int, *) 140 DEFINE_BINARY_OPERATOR (int, *)
167 DEFINE_ASSIGN_OPERATOR (int, >>=) 158 DEFINE_ASSIGN_OPERATOR (int, >>=)
168 DEFINE_ASSIGN_OPERATOR (int, <<=) 159 DEFINE_ASSIGN_OPERATOR (int, <<=)
169 }; 160 };
170 161
171 // ============================================================================= 162 // =============================================================================
172 CONFIGTYPE (str) { 163 CONFIGTYPE (String) {
173 public: 164 public:
174 IMPLEMENT_CONFIG (str) 165 IMPLEMENT_CONFIG (String, str)
175 166
176 DEFINE_COMPARE_OPERATOR (str, ==) 167 DEFINE_COMPARE_OPERATOR (str, ==)
177 DEFINE_COMPARE_OPERATOR (str, !=) 168 DEFINE_COMPARE_OPERATOR (str, !=)
178 DEFINE_ASSIGN_OPERATOR (str, =) 169 DEFINE_ASSIGN_OPERATOR (str, =)
179 DEFINE_ASSIGN_OPERATOR (str, +=) 170 DEFINE_ASSIGN_OPERATOR (str, +=)
182 return value[n]; 173 return value[n];
183 } 174 }
184 }; 175 };
185 176
186 // ============================================================================= 177 // =============================================================================
187 CONFIGTYPE (float) { 178 CONFIGTYPE (Float) {
188 public: 179 public:
189 IMPLEMENT_CONFIG (float) 180 IMPLEMENT_CONFIG (Float, float)
190 DEFINE_ALL_COMPARE_OPERATORS (float) 181 DEFINE_ALL_COMPARE_OPERATORS (float)
191 DEFINE_INCREMENT_OPERATORS (float) 182 DEFINE_INCREMENT_OPERATORS (float)
192 DEFINE_BINARY_OPERATOR (float, +) 183 DEFINE_BINARY_OPERATOR (float, +)
193 DEFINE_BINARY_OPERATOR (float, -) 184 DEFINE_BINARY_OPERATOR (float, -)
194 DEFINE_BINARY_OPERATOR (float, *) 185 DEFINE_BINARY_OPERATOR (float, *)
198 DEFINE_ASSIGN_OPERATOR (float, -=) 189 DEFINE_ASSIGN_OPERATOR (float, -=)
199 DEFINE_ASSIGN_OPERATOR (float, *=) 190 DEFINE_ASSIGN_OPERATOR (float, *=)
200 }; 191 };
201 192
202 // ============================================================================= 193 // =============================================================================
203 CONFIGTYPE (bool) { 194 CONFIGTYPE (Bool) {
204 public: 195 public:
205 IMPLEMENT_CONFIG (bool) 196 IMPLEMENT_CONFIG (Bool, bool)
206 DEFINE_ALL_COMPARE_OPERATORS (bool) 197 DEFINE_ALL_COMPARE_OPERATORS (bool)
207 DEFINE_ASSIGN_OPERATOR (bool, =) 198 DEFINE_ASSIGN_OPERATOR (bool, =)
208 }; 199 };
209 200
210 // ============================================================================= 201 // =============================================================================
211 typedef QKeySequence keyseq; 202 CONFIGTYPE (KeySequence) {
212 203 public:
213 CONFIGTYPE (keyseq) { 204 IMPLEMENT_CONFIG (KeySequence, QKeySequence)
214 public: 205 DEFINE_ALL_COMPARE_OPERATORS (QKeySequence)
215 IMPLEMENT_CONFIG (keyseq) 206 DEFINE_ASSIGN_OPERATOR (QKeySequence, =)
216 DEFINE_ALL_COMPARE_OPERATORS (keyseq)
217 DEFINE_ASSIGN_OPERATOR (keyseq, =)
218 }; 207 };
219 208
220 #endif // CONFIG_H 209 #endif // CONFIG_H

mercurial