config.h

changeset 69
6790dea720a8
parent 30
31ff9aabd506
child 78
c190fe218506
equal deleted inserted replaced
68:c637b172d565 69:6790dea720a8
21 21
22 #include "common.h" 22 #include "common.h"
23 #include "str.h" 23 #include "str.h"
24 24
25 // ============================================================================= 25 // =============================================================================
26 // Determine configuration file. Use APPNAME if given. 26 #define CONFIGFILE APPNAME ".cfg"
27 #ifdef APPNAME 27 #include <QString>
28 #define CONFIGFILE APPNAME ".ini"
29 #else // APPNAME
30 #define APPNAME "(unnamed application)"
31 #define CONFIGFILE "config.ini"
32 #endif // APPNAME
33
34 #ifdef CONFIG_WITH_QT
35 #include <QString>
36 #endif // CONFIG_WITH_QT
37
38 // -------------------------------
39 #define CFGSECTNAME(X) CFGSECT_##X
40 28
41 #define MAX_INI_LINE 512 29 #define MAX_INI_LINE 512
42 #define NUM_CONFIG (sizeof config::pointers / sizeof *config::pointers) 30 #define NUM_CONFIG (g_pConfigPointers.size ())
43 31
44 // ============================================================================= 32 #define cfg(T, NAME, DEFAULT) \
45 enum configsection_e { 33 T##config NAME (DEFAULT, #NAME, #T, #DEFAULT)
46 #define CFG(...) 34
47 #define SECT(A,B) CFGSECTNAME (A), 35 #define extern_cfg(T, NAME) \
48 #include "cfgdef.h" 36 extern T##config NAME
49 #undef CFG
50 #undef SECT
51 NUM_ConfigSections,
52 NO_CONFIG_SECTION = -1
53 };
54 37
55 // ============================================================================= 38 // =============================================================================
56 enum configtype_e { 39 enum configtype_e {
57 CONFIG_none, 40 CONFIG_none,
58 CONFIG_int, 41 CONFIG_int,
62 }; 45 };
63 46
64 // ========================================================= 47 // =========================================================
65 class config { 48 class config {
66 public: 49 public:
67 configsection_e sect; 50 const char* name, *typestring, *defaultstring;
68 const char* description, *name, *fullname, *typestring, *defaultstring;
69 51
70 virtual configtype_e getType () { 52 virtual configtype_e getType () {
71 return CONFIG_none; 53 return CONFIG_none;
72 } 54 }
73 55
75 57
76 // ------------------------------------------ 58 // ------------------------------------------
77 static bool load (); 59 static bool load ();
78 static bool save (); 60 static bool save ();
79 static void reset (); 61 static void reset ();
80 static config* pointers[];
81 static const char* sections[];
82 static const char* sectionNames[];
83 static str dirpath (); 62 static str dirpath ();
84 static str filepath (); 63 static str filepath ();
85 }; 64 };
65
66 extern std::vector<config*> g_pConfigPointers;
86 67
87 // ============================================================================= 68 // =============================================================================
88 #define DEFINE_UNARY_OPERATOR(T, OP) \ 69 #define DEFINE_UNARY_OPERATOR(T, OP) \
89 T operator OP () { \ 70 T operator OP () { \
90 return (OP value); \ 71 return (OP value); \
128 class T##config : public config 109 class T##config : public config
129 110
130 #define IMPLEMENT_CONFIG(T) \ 111 #define IMPLEMENT_CONFIG(T) \
131 T value, defval; \ 112 T value, defval; \
132 \ 113 \
133 T##config (const configsection_e _sect, const char* _description, \ 114 T##config (T _defval, const char* _name, const char* _typestring, \
134 T _defval, const char* _name, const char* _fullname, const char* _typestring, \
135 const char* _defaultstring) \ 115 const char* _defaultstring) \
136 { \ 116 { \
137 sect = _sect; \
138 description = _description; \
139 value = defval = _defval; \ 117 value = defval = _defval; \
140 name = _name; \ 118 name = _name; \
141 fullname = _fullname; \
142 typestring = _typestring; \ 119 typestring = _typestring; \
143 defaultstring = _defaultstring; \ 120 defaultstring = _defaultstring; \
121 g_pConfigPointers.push_back (this); \
144 } \ 122 } \
145 operator T () { \ 123 operator T () { \
146 return value; \ 124 return value; \
147 } \ 125 } \
148 configtype_e getType () { \ 126 configtype_e getType () { \
149 return CONFIG_##T; \ 127 return CONFIG_##T; \
150 } \ 128 } \
151 void resetValue () { \ 129 virtual void resetValue () { \
152 value = defval; \ 130 value = defval; \
153 } 131 }
154 132
155 // ============================================================================= 133 // =============================================================================
156 CONFIGTYPE (int) { 134 CONFIGTYPE (int) {
233 IMPLEMENT_CONFIG (bool) 211 IMPLEMENT_CONFIG (bool)
234 DEFINE_ALL_COMPARE_OPERATORS (bool) 212 DEFINE_ALL_COMPARE_OPERATORS (bool)
235 DEFINE_ASSIGN_OPERATOR (bool, =) 213 DEFINE_ASSIGN_OPERATOR (bool, =)
236 }; 214 };
237 215
238 // =============================================================================
239 // Extern the configurations now
240 #define CFG(TYPE, SECT, NAME, DESCR, DEFAULT) extern TYPE##config SECT##_##NAME;
241 #define SECT(...)
242 #include "cfgdef.h"
243 #undef CFG
244 #undef SECT
245
246 #endif // __OPTIONS_H__ 216 #endif // __OPTIONS_H__

mercurial