Changeset View
Changeset View
Standalone View
Standalone View
src/kconfig_compiler/KConfigHeaderGenerator.cpp
Show All 22 Lines | 1 | /* This file is part of the KDE libraries | |||
---|---|---|---|---|---|
23 | Boston, MA 02110-1301, USA. | 23 | Boston, MA 02110-1301, USA. | ||
24 | */ | 24 | */ | ||
25 | 25 | | |||
26 | #include "KConfigHeaderGenerator.h" | 26 | #include "KConfigHeaderGenerator.h" | ||
27 | #include "KConfigCommonStructs.h" | 27 | #include "KConfigCommonStructs.h" | ||
28 | 28 | | |||
29 | #include <QTextStream> | 29 | #include <QTextStream> | ||
30 | #include <QDebug> | 30 | #include <QDebug> | ||
31 | 31 | #include <iostream> | |||
32 | namespace | | |||
33 | { | | |||
34 | QTextStream cout(stdout); | | |||
35 | QTextStream cerr(stderr); | | |||
36 | } | | |||
37 | 32 | | |||
38 | KConfigHeaderGenerator::KConfigHeaderGenerator( | 33 | KConfigHeaderGenerator::KConfigHeaderGenerator( | ||
39 | const QString &inputFile, | 34 | const QString &inputFile, | ||
40 | const QString &baseDir, | 35 | const QString &baseDir, | ||
41 | const KConfigParameters &cfg, | 36 | const KConfigParameters &cfg, | ||
42 | ParseResult &result) | 37 | ParseResult &result) | ||
43 | : KConfigCodeGeneratorBase(inputFile, baseDir, baseDir + cfg.baseName + QLatin1Char('.') + cfg.headerExtension, cfg, result) | 38 | : KConfigCodeGeneratorBase(inputFile, baseDir, baseDir + cfg.baseName + QLatin1Char('.') + cfg.headerExtension, cfg, result) | ||
44 | { | 39 | { | ||
Show All 12 Lines | 43 | { | |||
57 | doClassDefinition(); | 52 | doClassDefinition(); | ||
58 | 53 | | |||
59 | endNamespaces(); | 54 | endNamespaces(); | ||
60 | endHeaderGuards(); | 55 | endHeaderGuards(); | ||
61 | } | 56 | } | ||
62 | 57 | | |||
63 | void KConfigHeaderGenerator::doClassDefinition() | 58 | void KConfigHeaderGenerator::doClassDefinition() | ||
64 | { | 59 | { | ||
65 | stream() << "class " << cfg().visibility << cfg().className << " : public " << cfg().inherits << endl; | 60 | stream() << "class " << cfg().visibility << cfg().className << " : public " << cfg().inherits << '\n'; | ||
66 | startScope(); | 61 | startScope(); | ||
67 | 62 | | |||
68 | // Add Q_OBJECT macro if the config need signals. | 63 | // Add Q_OBJECT macro if the config need signals. | ||
69 | if (!parseResult.signalList.isEmpty() || cfg().generateProperties) { | 64 | if (!parseResult.signalList.isEmpty() || cfg().generateProperties) { | ||
70 | stream() << " Q_OBJECT" << endl; | 65 | stream() << " Q_OBJECT\n"; | ||
71 | } | 66 | } | ||
72 | stream() << " public:" << endl; | 67 | stream() << " public:\n"; | ||
73 | implementEnums(); | 68 | implementEnums(); | ||
74 | createConstructor(); | 69 | createConstructor(); | ||
75 | createDestructor(); | 70 | createDestructor(); | ||
76 | 71 | | |||
77 | for (auto *entry : parseResult.entries) { | 72 | for (auto *entry : parseResult.entries) { | ||
78 | const QString n = entry->name; | 73 | const QString n = entry->name; | ||
79 | const QString t = entry->type; | 74 | const QString t = entry->type; | ||
80 | 75 | | |||
81 | const QString returnType = (cfg().useEnumTypes && t == QLatin1String("Enum")) | 76 | const QString returnType = (cfg().useEnumTypes && t == QLatin1String("Enum")) | ||
82 | ? enumType(entry, cfg().globalEnums) | 77 | ? enumType(entry, cfg().globalEnums) | ||
83 | : cppType(t); | 78 | : cppType(t); | ||
84 | 79 | | |||
85 | createSetters(entry); | 80 | createSetters(entry); | ||
86 | createProperties(entry, returnType); | 81 | createProperties(entry, returnType); | ||
87 | createGetters(entry, returnType); | 82 | createGetters(entry, returnType); | ||
88 | createDefaultValueMember(entry); | 83 | createDefaultValueMember(entry); | ||
89 | createItemAcessors(entry, returnType); | 84 | createItemAcessors(entry, returnType); | ||
90 | } | 85 | } | ||
91 | 86 | | |||
92 | createSignals(); | 87 | createSignals(); | ||
93 | stream() << " protected:" << endl; | 88 | stream() << " protected:\n"; | ||
94 | createSingleton(); | 89 | createSingleton(); | ||
95 | 90 | | |||
96 | // TODO: Move those to functions too. | 91 | // TODO: Move those to functions too. | ||
97 | if (parseResult.hasNonModifySignals) { | 92 | if (parseResult.hasNonModifySignals) { | ||
98 | stream() << whitespace() << "bool usrSave() override;" << endl; | 93 | stream() << whitespace() << "bool usrSave() override;\n"; | ||
99 | } | 94 | } | ||
100 | 95 | | |||
101 | // Member variables | 96 | // Member variables | ||
102 | if (!cfg().memberVariables.isEmpty() | 97 | if (!cfg().memberVariables.isEmpty() | ||
103 | && cfg().memberVariables != QLatin1String("private") | 98 | && cfg().memberVariables != QLatin1String("private") | ||
104 | && cfg().memberVariables != QLatin1String("dpointer")) { | 99 | && cfg().memberVariables != QLatin1String("dpointer")) { | ||
105 | stream() << " " << cfg().memberVariables << ":" << endl; | 100 | stream() << " " << cfg().memberVariables << ":\n"; | ||
106 | } | 101 | } | ||
107 | 102 | | |||
108 | // Class Parameters | 103 | // Class Parameters | ||
109 | for (const auto ¶meter : parseResult.parameters) { | 104 | for (const auto ¶meter : parseResult.parameters) { | ||
110 | stream() << whitespace() << "" << cppType(parameter.type) << " mParam" << parameter.name << ";" << endl; | 105 | stream() << whitespace() << "" << cppType(parameter.type) << " mParam" << parameter.name << ";\n"; | ||
111 | } | 106 | } | ||
112 | 107 | | |||
113 | createNonDPointerHelpers(); | 108 | createNonDPointerHelpers(); | ||
114 | createDPointer(); | 109 | createDPointer(); | ||
115 | 110 | | |||
116 | if (cfg().customAddons) { | 111 | if (cfg().customAddons) { | ||
117 | stream() << whitespace() << "// Include custom additions" << endl; | 112 | stream() << whitespace() << "// Include custom additions\n"; | ||
118 | stream() << whitespace() << "#include \"" << cfg().baseName << "_addons." << cfg().headerExtension << '"' << endl; | 113 | stream() << whitespace() << "#include \"" << cfg().baseName << "_addons." << cfg().headerExtension << "\"\n"; | ||
119 | } | 114 | } | ||
120 | 115 | | |||
121 | endScope(ScopeFinalizer::Semicolon); | 116 | endScope(ScopeFinalizer::Semicolon); | ||
122 | } | 117 | } | ||
123 | 118 | | |||
124 | void KConfigHeaderGenerator::createHeaders() | 119 | void KConfigHeaderGenerator::createHeaders() | ||
125 | { | 120 | { | ||
126 | addHeaders(cfg().headerIncludes); | 121 | addHeaders(cfg().headerIncludes); | ||
127 | if (cfg().headerIncludes.size()) { | 122 | if (cfg().headerIncludes.size()) { | ||
128 | stream() << endl; | 123 | stream() << '\n'; | ||
129 | } | 124 | } | ||
130 | 125 | | |||
131 | if (!cfg().singleton && parseResult.parameters.isEmpty()) { | 126 | if (!cfg().singleton && parseResult.parameters.isEmpty()) { | ||
132 | addHeaders({QStringLiteral("qglobal.h")}); | 127 | addHeaders({QStringLiteral("qglobal.h")}); | ||
133 | } | 128 | } | ||
134 | 129 | | |||
135 | if (cfg().inherits == QLatin1String("KCoreConfigSkeleton")) { | 130 | if (cfg().inherits == QLatin1String("KCoreConfigSkeleton")) { | ||
136 | addHeaders({QStringLiteral("kcoreconfigskeleton.h")}); | 131 | addHeaders({QStringLiteral("kcoreconfigskeleton.h")}); | ||
137 | } else { | 132 | } else { | ||
138 | addHeaders({QStringLiteral("kconfigskeleton.h")}); | 133 | addHeaders({QStringLiteral("kconfigskeleton.h")}); | ||
139 | } | 134 | } | ||
140 | 135 | | |||
141 | addHeaders({QStringLiteral("QCoreApplication"), QStringLiteral("QDebug")}); | 136 | addHeaders({QStringLiteral("QCoreApplication"), QStringLiteral("QDebug")}); | ||
142 | stream() << endl; | 137 | stream() << '\n'; | ||
143 | 138 | | |||
144 | addHeaders(parseResult.includes); | 139 | addHeaders(parseResult.includes); | ||
145 | if (parseResult.includes.size()) { | 140 | if (parseResult.includes.size()) { | ||
146 | stream() << endl; | 141 | stream() << '\n'; | ||
147 | } | 142 | } | ||
148 | } | 143 | } | ||
149 | 144 | | |||
150 | void KConfigHeaderGenerator::startHeaderGuards() | 145 | void KConfigHeaderGenerator::startHeaderGuards() | ||
151 | { | 146 | { | ||
152 | const bool hasNamespace = !cfg().nameSpace.isEmpty(); | 147 | const bool hasNamespace = !cfg().nameSpace.isEmpty(); | ||
153 | const QString namespaceName = QString(QString(cfg().nameSpace).replace(QLatin1String("::"), QLatin1String("_"))).toUpper(); | 148 | const QString namespaceName = QString(QString(cfg().nameSpace).replace(QLatin1String("::"), QLatin1String("_"))).toUpper(); | ||
154 | const QString namespaceStr = hasNamespace ? namespaceName + QLatin1Char('_') : QStringLiteral(""); | 149 | const QString namespaceStr = hasNamespace ? namespaceName + QLatin1Char('_') : QStringLiteral(""); | ||
155 | const QString defineName = namespaceStr + cfg().className.toUpper() + QStringLiteral("_H"); | 150 | const QString defineName = namespaceStr + cfg().className.toUpper() + QStringLiteral("_H"); | ||
156 | 151 | | |||
157 | stream() << "#ifndef " << defineName << endl; | 152 | stream() << "#ifndef " << defineName << '\n'; | ||
158 | stream() << "#define " << defineName << endl; | 153 | stream() << "#define " << defineName << '\n'; | ||
159 | stream() << endl; | 154 | stream() << '\n'; | ||
160 | } | 155 | } | ||
161 | 156 | | |||
162 | void KConfigHeaderGenerator::endHeaderGuards() | 157 | void KConfigHeaderGenerator::endHeaderGuards() | ||
163 | { | 158 | { | ||
164 | stream() << endl; | 159 | stream() << '\n'; | ||
165 | stream() << "#endif"; | 160 | stream() << "#endif"; | ||
166 | stream() << endl; | 161 | stream() << '\n'; | ||
167 | // HACK: Original files ended with two last newlines, add them. | 162 | // HACK: Original files ended with two last newlines, add them. | ||
168 | stream() << endl; | 163 | stream() << '\n'; | ||
169 | } | 164 | } | ||
170 | 165 | | |||
171 | void KConfigHeaderGenerator::implementChoiceEnums(const CfgEntry *entry, const CfgEntry::Choices &choices) | 166 | void KConfigHeaderGenerator::implementChoiceEnums(const CfgEntry *entry, const CfgEntry::Choices &choices) | ||
172 | { | 167 | { | ||
173 | const QList<CfgEntry::Choice> chlist = choices.choices; | 168 | const QList<CfgEntry::Choice> chlist = choices.choices; | ||
174 | 169 | | |||
175 | if (chlist.isEmpty()) { | 170 | if (chlist.isEmpty()) { | ||
176 | return; | 171 | return; | ||
177 | } | 172 | } | ||
178 | 173 | | |||
179 | QStringList values; | 174 | QStringList values; | ||
180 | for (const auto choice : qAsConst(chlist)) { | 175 | for (const auto choice : qAsConst(chlist)) { | ||
181 | values.append(choices.prefix + choice.name); | 176 | values.append(choices.prefix + choice.name); | ||
182 | } | 177 | } | ||
183 | 178 | | |||
184 | if (choices.name().isEmpty()) { | 179 | if (choices.name().isEmpty()) { | ||
185 | if (cfg().globalEnums) { | 180 | if (cfg().globalEnums) { | ||
186 | stream() << whitespace() << "enum " << enumName(entry->name, entry->choices) << " { " << values.join(QStringLiteral(", ")) << " };" << endl; | 181 | stream() << whitespace() << "enum " << enumName(entry->name, entry->choices) << " { " << values.join(QStringLiteral(", ")) << " };\n"; | ||
187 | } else { | 182 | } else { | ||
188 | // Create an automatically named enum | 183 | // Create an automatically named enum | ||
189 | stream() << whitespace() << "class " << enumName(entry->name, entry->choices) << endl; | 184 | stream() << whitespace() << "class " << enumName(entry->name, entry->choices) << '\n'; | ||
190 | stream() << whitespace() << "{" << endl; | 185 | stream() << whitespace() << "{\n"; | ||
191 | stream() << whitespace() << " public:" << endl; | 186 | stream() << whitespace() << " public:\n"; | ||
192 | stream() << whitespace() << " enum type { " << values.join(QStringLiteral(", ")) << ", COUNT };" << endl; | 187 | stream() << whitespace() << " enum type { " << values.join(QStringLiteral(", ")) << ", COUNT };\n"; | ||
193 | stream() << whitespace() << "};" << endl; | 188 | stream() << whitespace() << "};\n"; | ||
194 | } | 189 | } | ||
195 | } else if (!choices.external()) { | 190 | } else if (!choices.external()) { | ||
196 | // Create a named enum | 191 | // Create a named enum | ||
197 | stream() << whitespace() << "enum " << enumName(entry->name, entry->choices) << " { " << values.join(QStringLiteral(", ")) << " };" << endl; | 192 | stream() << whitespace() << "enum " << enumName(entry->name, entry->choices) << " { " << values.join(QStringLiteral(", ")) << " };\n"; | ||
198 | } | 193 | } | ||
199 | } | 194 | } | ||
200 | 195 | | |||
201 | void KConfigHeaderGenerator::implementValueEnums(const CfgEntry *entry, const QStringList &values) | 196 | void KConfigHeaderGenerator::implementValueEnums(const CfgEntry *entry, const QStringList &values) | ||
202 | { | 197 | { | ||
203 | if (values.isEmpty()) { | 198 | if (values.isEmpty()) { | ||
204 | return; | 199 | return; | ||
205 | } | 200 | } | ||
206 | 201 | | |||
207 | if (cfg().globalEnums) { | 202 | if (cfg().globalEnums) { | ||
208 | // ### FIXME!! | 203 | // ### FIXME!! | ||
209 | // make the following string table an index-based string search! | 204 | // make the following string table an index-based string search! | ||
210 | // ### | 205 | // ### | ||
211 | stream() << whitespace() << "enum " << enumName(entry->param) << " { " << values.join(QStringLiteral(", ")) << " };" << endl; | 206 | stream() << whitespace() << "enum " << enumName(entry->param) << " { " << values.join(QStringLiteral(", ")) << " };\n"; | ||
212 | stream() << whitespace() << "static const char* const " << enumName(entry->param) << "ToString[];" << endl; | 207 | stream() << whitespace() << "static const char* const " << enumName(entry->param) << "ToString[];\n"; | ||
213 | } else { | 208 | } else { | ||
214 | stream() << whitespace() << "class " << enumName(entry->param) << endl; | 209 | stream() << whitespace() << "class " << enumName(entry->param) << '\n'; | ||
215 | stream() << whitespace() << "{" << endl; | 210 | stream() << whitespace() << "{\n"; | ||
216 | stream() << whitespace() << " public:" << endl; | 211 | stream() << whitespace() << " public:\n"; | ||
217 | stream() << whitespace() << " enum type { " << values.join(QStringLiteral(", ")) << ", COUNT };" << endl; | 212 | stream() << whitespace() << " enum type { " << values.join(QStringLiteral(", ")) << ", COUNT };\n"; | ||
218 | stream() << whitespace() << " static const char* const enumToString[];" << endl; | 213 | stream() << whitespace() << " static const char* const enumToString[];\n"; | ||
219 | stream() << whitespace() << "};" << endl; | 214 | stream() << whitespace() << "};\n"; | ||
220 | } | 215 | } | ||
221 | } | 216 | } | ||
222 | 217 | | |||
223 | void KConfigHeaderGenerator::implementEnums() | 218 | void KConfigHeaderGenerator::implementEnums() | ||
224 | { | 219 | { | ||
225 | if (!parseResult.entries.size()) { | 220 | if (!parseResult.entries.size()) { | ||
226 | return; | 221 | return; | ||
227 | } | 222 | } | ||
228 | 223 | | |||
229 | for (const auto entry : parseResult.entries) { | 224 | for (const auto entry : parseResult.entries) { | ||
230 | const CfgEntry::Choices &choices = entry->choices; | 225 | const CfgEntry::Choices &choices = entry->choices; | ||
231 | const QStringList values = entry->paramValues; | 226 | const QStringList values = entry->paramValues; | ||
232 | 227 | | |||
233 | implementChoiceEnums(entry, choices); | 228 | implementChoiceEnums(entry, choices); | ||
234 | implementValueEnums(entry, values); | 229 | implementValueEnums(entry, values); | ||
235 | } | 230 | } | ||
236 | stream() << endl; | 231 | stream() << '\n'; | ||
237 | } | 232 | } | ||
238 | 233 | | |||
239 | void KConfigHeaderGenerator::createSignals() | 234 | void KConfigHeaderGenerator::createSignals() | ||
240 | { | 235 | { | ||
241 | // Signal definition. | 236 | // Signal definition. | ||
242 | const bool hasSignals = !parseResult.signalList.isEmpty(); | 237 | const bool hasSignals = !parseResult.signalList.isEmpty(); | ||
243 | 238 | | |||
244 | unsigned val = 1 << parseResult.signalList.size(); | 239 | unsigned val = 1 << parseResult.signalList.size(); | ||
245 | if (!val) { | 240 | if (!val) { | ||
246 | cerr << "Too many signals to create unique bit masks" << endl; | 241 | std::cerr << "Too many signals to create unique bit masks" << std::endl; | ||
247 | exit(1); | 242 | exit(1); | ||
248 | } | 243 | } | ||
249 | 244 | | |||
250 | if (!hasSignals) { | 245 | if (!hasSignals) { | ||
251 | return; | 246 | return; | ||
252 | } | 247 | } | ||
253 | 248 | | |||
254 | stream() << "\n enum {" << endl; | 249 | stream() << "\n enum {\n"; | ||
255 | val = 1; | 250 | val = 1; | ||
256 | 251 | | |||
257 | // HACK: Use C-Style for add a comma in all but the last element, | 252 | // HACK: Use C-Style for add a comma in all but the last element, | ||
258 | // just to make the source generated code equal to the old one. | 253 | // just to make the source generated code equal to the old one. | ||
259 | // When we are sure, revert this to a range-based-for and just add | 254 | // When we are sure, revert this to a range-based-for and just add | ||
260 | // a last comma, as it's valid c++. | 255 | // a last comma, as it's valid c++. | ||
261 | for (int i = 0, end = parseResult.signalList.size(); i < end; i++) { | 256 | for (int i = 0, end = parseResult.signalList.size(); i < end; i++) { | ||
262 | auto signal = parseResult.signalList.at(i); | 257 | auto signal = parseResult.signalList.at(i); | ||
263 | stream() << whitespace() << " " << signalEnumName(signal.name) << " = 0x" << hex << val; | 258 | stream() << whitespace() << " " << signalEnumName(signal.name) << " = 0x" << hex << val; | ||
264 | if (i != end-1) { | 259 | if (i != end-1) { | ||
265 | stream() << "," << endl; | 260 | stream() << ",\n"; | ||
266 | } | 261 | } | ||
267 | 262 | | |||
268 | val <<= 1; | 263 | val <<= 1; | ||
269 | } | 264 | } | ||
270 | stream() << endl; | 265 | stream() << '\n'; | ||
271 | stream() << whitespace() << "};" << dec << endl << endl; | 266 | stream() << whitespace() << "};" << dec << "\n\n"; | ||
272 | 267 | | |||
273 | stream() << " Q_SIGNALS:"; | 268 | stream() << " Q_SIGNALS:"; | ||
274 | for (const Signal &signal : parseResult.signalList) { | 269 | for (const Signal &signal : parseResult.signalList) { | ||
275 | stream() << endl; | 270 | stream() << '\n'; | ||
276 | if (!signal.label.isEmpty()) { | 271 | if (!signal.label.isEmpty()) { | ||
277 | stream() << whitespace() << "/**" << endl; | 272 | stream() << whitespace() << "/**\n"; | ||
278 | stream() << whitespace() << " " << signal.label << endl; | 273 | stream() << whitespace() << " " << signal.label << '\n'; | ||
279 | stream() << whitespace() << "*/" << endl; | 274 | stream() << whitespace() << "*/\n"; | ||
280 | } | 275 | } | ||
281 | stream() << whitespace() << "void " << signal.name << "("; | 276 | stream() << whitespace() << "void " << signal.name << "("; | ||
282 | QList<Param>::ConstIterator it, itEnd = signal.arguments.constEnd(); | 277 | QList<Param>::ConstIterator it, itEnd = signal.arguments.constEnd(); | ||
283 | for (it = signal.arguments.constBegin(); it != itEnd;) { | 278 | for (it = signal.arguments.constBegin(); it != itEnd;) { | ||
284 | Param argument = *it; | 279 | Param argument = *it; | ||
285 | QString type = param(argument.type); | 280 | QString type = param(argument.type); | ||
286 | if (cfg().useEnumTypes && argument.type == QLatin1String("Enum")) { | 281 | if (cfg().useEnumTypes && argument.type == QLatin1String("Enum")) { | ||
287 | for (auto *entry : parseResult.entries) { | 282 | for (auto *entry : parseResult.entries) { | ||
288 | if (entry->name == argument.name) { | 283 | if (entry->name == argument.name) { | ||
289 | type = enumType(entry, cfg().globalEnums); | 284 | type = enumType(entry, cfg().globalEnums); | ||
290 | break; | 285 | break; | ||
291 | } | 286 | } | ||
292 | } | 287 | } | ||
293 | } | 288 | } | ||
294 | stream() << type << " " << argument.name; | 289 | stream() << type << " " << argument.name; | ||
295 | if (++it != itEnd) { | 290 | if (++it != itEnd) { | ||
296 | stream() << ", "; | 291 | stream() << ", "; | ||
297 | } | 292 | } | ||
298 | } | 293 | } | ||
299 | stream() << ");" << endl; | 294 | stream() << ");\n"; | ||
300 | } | 295 | } | ||
301 | stream() << endl; | 296 | stream() << '\n'; | ||
302 | 297 | | |||
303 | stream() << " private:" << endl; | 298 | stream() << " private:\n"; | ||
304 | stream() << whitespace() << "void itemChanged(quint64 flags);" << endl; | 299 | stream() << whitespace() << "void itemChanged(quint64 flags);\n"; | ||
305 | stream() << endl; | 300 | stream() << '\n'; | ||
306 | } | 301 | } | ||
307 | 302 | | |||
308 | void KConfigHeaderGenerator::createDPointer() | 303 | void KConfigHeaderGenerator::createDPointer() | ||
309 | { | 304 | { | ||
310 | if (!cfg().dpointer) { | 305 | if (!cfg().dpointer) { | ||
311 | return; | 306 | return; | ||
312 | } | 307 | } | ||
313 | 308 | | |||
314 | // use a private class for both member variables and items | 309 | // use a private class for both member variables and items | ||
315 | stream() << " private:" << endl; | 310 | stream() << " private:\n"; | ||
316 | for (const auto &entry : parseResult.entries) { | 311 | for (const auto &entry : parseResult.entries) { | ||
317 | if (cfg().allDefaultGetters || cfg().defaultGetters.contains(entry->name)) { | 312 | if (cfg().allDefaultGetters || cfg().defaultGetters.contains(entry->name)) { | ||
318 | stream() << whitespace() << ""; | 313 | stream() << whitespace() << ""; | ||
319 | if (cfg().staticAccessors) { | 314 | if (cfg().staticAccessors) { | ||
320 | stream() << "static "; | 315 | stream() << "static "; | ||
321 | } | 316 | } | ||
322 | stream() << cppType(entry->type) << " " << getDefaultFunction(entry->name) << "_helper("; | 317 | stream() << cppType(entry->type) << " " << getDefaultFunction(entry->name) << "_helper("; | ||
323 | if (!entry->param.isEmpty()) { | 318 | if (!entry->param.isEmpty()) { | ||
324 | stream() << " " << cppType(entry->paramType) << " i "; | 319 | stream() << " " << cppType(entry->paramType) << " i "; | ||
325 | } | 320 | } | ||
326 | stream() << ")" << Const() << ";" << endl; | 321 | stream() << ")" << Const() << ";\n"; | ||
327 | } | 322 | } | ||
328 | } | 323 | } | ||
329 | stream() << whitespace() << "" << cfg().className << "Private *d;" << endl; | 324 | stream() << whitespace() << "" << cfg().className << "Private *d;\n"; | ||
330 | } | 325 | } | ||
331 | 326 | | |||
332 | void KConfigHeaderGenerator::createConstructor() | 327 | void KConfigHeaderGenerator::createConstructor() | ||
333 | { | 328 | { | ||
334 | if (cfg().singleton) { | 329 | if (cfg().singleton) { | ||
335 | stream() << whitespace() << "static " << cfg().className << " *self();" << endl; | 330 | stream() << whitespace() << "static " << cfg().className << " *self();\n"; | ||
336 | if (parseResult.cfgFileNameArg) { | 331 | if (parseResult.cfgFileNameArg) { | ||
337 | stream() << whitespace() << "static void instance(const QString& cfgfilename);" << endl; | 332 | stream() << whitespace() << "static void instance(const QString& cfgfilename);\n"; | ||
338 | stream() << whitespace() << "static void instance(KSharedConfig::Ptr config);" << endl; | 333 | stream() << whitespace() << "static void instance(KSharedConfig::Ptr config);\n"; | ||
339 | } | 334 | } | ||
340 | return; | 335 | return; | ||
341 | } | 336 | } | ||
342 | 337 | | |||
343 | stream() << whitespace() << "" << cfg().className << "("; | 338 | stream() << whitespace() << "" << cfg().className << "("; | ||
344 | if (parseResult.cfgFileNameArg) { | 339 | if (parseResult.cfgFileNameArg) { | ||
345 | if (cfg().forceStringFilename) | 340 | if (cfg().forceStringFilename) | ||
346 | stream() << " const QString &cfgfilename" << (parseResult.parameters.isEmpty() ? " = QString()" : ", "); | 341 | stream() << " const QString &cfgfilename" << (parseResult.parameters.isEmpty() ? " = QString()" : ", "); | ||
Show All 13 Lines | |||||
360 | } | 355 | } | ||
361 | 356 | | |||
362 | if (cfg().parentInConstructor) { | 357 | if (cfg().parentInConstructor) { | ||
363 | if (parseResult.cfgFileNameArg || !parseResult.parameters.isEmpty()) { | 358 | if (parseResult.cfgFileNameArg || !parseResult.parameters.isEmpty()) { | ||
364 | stream() << ","; | 359 | stream() << ","; | ||
365 | } | 360 | } | ||
366 | stream() << " QObject *parent = nullptr"; | 361 | stream() << " QObject *parent = nullptr"; | ||
367 | } | 362 | } | ||
368 | stream() << " );" << endl; | 363 | stream() << " );\n"; | ||
369 | } | 364 | } | ||
370 | 365 | | |||
371 | void KConfigHeaderGenerator::createDestructor() | 366 | void KConfigHeaderGenerator::createDestructor() | ||
372 | { | 367 | { | ||
373 | stream() << whitespace() << "~" << cfg().className << "();" << endl << endl; | 368 | stream() << whitespace() << "~" << cfg().className << "();\n\n"; | ||
374 | } | 369 | } | ||
375 | 370 | | |||
376 | void KConfigHeaderGenerator::createForwardDeclarations() | 371 | void KConfigHeaderGenerator::createForwardDeclarations() | ||
377 | { | 372 | { | ||
378 | // Private class declaration | 373 | // Private class declaration | ||
379 | if (cfg().dpointer) { | 374 | if (cfg().dpointer) { | ||
380 | stream() << "class " << cfg().className << "Private;" << endl << endl; | 375 | stream() << "class " << cfg().className << "Private;\n\n"; | ||
381 | } | 376 | } | ||
382 | } | 377 | } | ||
383 | 378 | | |||
384 | void KConfigHeaderGenerator::createProperties(const CfgEntry *entry, const QString& returnType) | 379 | void KConfigHeaderGenerator::createProperties(const CfgEntry *entry, const QString& returnType) | ||
385 | { | 380 | { | ||
386 | if (!cfg().generateProperties) { | 381 | if (!cfg().generateProperties) { | ||
387 | return; | 382 | return; | ||
388 | } | 383 | } | ||
Show All 9 Lines | 387 | if (cfg().allMutators || cfg().mutators.contains(entry->name)) { | |||
398 | //the changed signal as well | 393 | //the changed signal as well | ||
399 | Signal s; | 394 | Signal s; | ||
400 | s.name = signal; | 395 | s.name = signal; | ||
401 | s.modify = true; | 396 | s.modify = true; | ||
402 | parseResult.signalList.append(s); | 397 | parseResult.signalList.append(s); | ||
403 | } else { | 398 | } else { | ||
404 | stream() << " CONSTANT"; | 399 | stream() << " CONSTANT"; | ||
405 | } | 400 | } | ||
406 | stream() << ")" << endl; | 401 | stream() << ")\n"; | ||
407 | } | 402 | } | ||
408 | 403 | | |||
409 | void KConfigHeaderGenerator::createSetters(const CfgEntry *entry) | 404 | void KConfigHeaderGenerator::createSetters(const CfgEntry *entry) | ||
410 | { | 405 | { | ||
411 | // Manipulator | 406 | // Manipulator | ||
412 | if (!cfg().allMutators && !cfg().mutators.contains(entry->name)) { | 407 | if (!cfg().allMutators && !cfg().mutators.contains(entry->name)) { | ||
413 | return; | 408 | return; | ||
414 | } | 409 | } | ||
415 | 410 | | |||
416 | stream() << whitespace() << "/**" << endl; | 411 | stream() << whitespace() << "/**\n"; | ||
417 | stream() << whitespace() << " Set " << entry->label << endl; | 412 | stream() << whitespace() << " Set " << entry->label << '\n'; | ||
418 | stream() << whitespace() << "*/" << endl; | 413 | stream() << whitespace() << "*/\n"; | ||
419 | 414 | | |||
420 | if (cfg().staticAccessors) { | 415 | if (cfg().staticAccessors) { | ||
421 | stream() << whitespace() << "static" << endl; | 416 | stream() << whitespace() << "static\n"; | ||
422 | } | 417 | } | ||
423 | 418 | | |||
424 | stream() << whitespace() << "void " << setFunction(entry->name) << "( "; | 419 | stream() << whitespace() << "void " << setFunction(entry->name) << "( "; | ||
425 | if (!entry->param.isEmpty()) { | 420 | if (!entry->param.isEmpty()) { | ||
426 | stream() <<cppType(entry->paramType) << " i, "; | 421 | stream() <<cppType(entry->paramType) << " i, "; | ||
427 | } | 422 | } | ||
428 | 423 | | |||
429 | stream() << (cfg().useEnumTypes && entry->type == QLatin1String("Enum") | 424 | stream() << (cfg().useEnumTypes && entry->type == QLatin1String("Enum") | ||
430 | ? enumType(entry, cfg().globalEnums) | 425 | ? enumType(entry, cfg().globalEnums) | ||
431 | : param(entry->type)); | 426 | : param(entry->type)); | ||
432 | 427 | | |||
433 | stream() << " v )"; | 428 | stream() << " v )"; | ||
434 | 429 | | |||
435 | // function body inline only if not using dpointer | 430 | // function body inline only if not using dpointer | ||
436 | // for BC mode | 431 | // for BC mode | ||
437 | if (!cfg().dpointer) { | 432 | if (!cfg().dpointer) { | ||
438 | stream() << endl; | 433 | stream() << '\n'; | ||
439 | startScope(); | 434 | startScope(); | ||
440 | memberMutatorBody(entry); | 435 | memberMutatorBody(entry); | ||
441 | endScope(); | 436 | endScope(); | ||
442 | stream() << endl; | 437 | stream() << '\n'; | ||
443 | } else { | 438 | } else { | ||
444 | stream() << ";" << endl << endl; | 439 | stream() << ";\n\n"; | ||
445 | } | 440 | } | ||
446 | } | 441 | } | ||
447 | 442 | | |||
448 | void KConfigHeaderGenerator::createGetters(const CfgEntry *entry, const QString &returnType) | 443 | void KConfigHeaderGenerator::createGetters(const CfgEntry *entry, const QString &returnType) | ||
449 | { | 444 | { | ||
450 | // Accessor | 445 | // Accessor | ||
451 | stream() << whitespace() << "/**" << endl; | 446 | stream() << whitespace() << "/**\n"; | ||
452 | stream() << whitespace() << " Get " << entry->label << endl; | 447 | stream() << whitespace() << " Get " << entry->label << '\n'; | ||
453 | stream() << whitespace() << "*/" << endl; | 448 | stream() << whitespace() << "*/\n"; | ||
454 | if (cfg().staticAccessors) { | 449 | if (cfg().staticAccessors) { | ||
455 | stream() << whitespace() << "static" << endl; | 450 | stream() << whitespace() << "static\n"; | ||
456 | } | 451 | } | ||
457 | stream() << whitespace() << ""; | 452 | stream() << whitespace() << ""; | ||
458 | stream() << returnType; | 453 | stream() << returnType; | ||
459 | stream() << " " << getFunction(entry->name) << "("; | 454 | stream() << " " << getFunction(entry->name) << "("; | ||
460 | if (!entry->param.isEmpty()) { | 455 | if (!entry->param.isEmpty()) { | ||
461 | stream() << " " << cppType(entry->paramType) << " i "; | 456 | stream() << " " << cppType(entry->paramType) << " i "; | ||
462 | } | 457 | } | ||
463 | stream() << ")" << Const(); | 458 | stream() << ")" << Const(); | ||
464 | 459 | | |||
465 | // function body inline only if not using dpointer | 460 | // function body inline only if not using dpointer | ||
466 | // for BC mode | 461 | // for BC mode | ||
467 | if (!cfg().dpointer) { | 462 | if (!cfg().dpointer) { | ||
468 | stream() << endl; | 463 | stream() << '\n'; | ||
469 | startScope(); | 464 | startScope(); | ||
470 | stream() << whitespace() << memberAccessorBody(entry, cfg().globalEnums); | 465 | stream() << whitespace() << memberAccessorBody(entry, cfg().globalEnums); | ||
471 | endScope(); | 466 | endScope(); | ||
472 | stream() << endl; | 467 | stream() << '\n'; | ||
473 | } else { | 468 | } else { | ||
474 | stream() << ";" << endl << endl; | 469 | stream() << ";\n\n"; | ||
475 | } | 470 | } | ||
476 | } | 471 | } | ||
477 | 472 | | |||
478 | void KConfigHeaderGenerator::createItemAcessors(const CfgEntry *entry, const QString &returnType) | 473 | void KConfigHeaderGenerator::createItemAcessors(const CfgEntry *entry, const QString &returnType) | ||
479 | { | 474 | { | ||
480 | // Item accessor | 475 | // Item accessor | ||
481 | if (!cfg().itemAccessors) { | 476 | if (!cfg().itemAccessors) { | ||
482 | return; | 477 | return; | ||
483 | } | 478 | } | ||
484 | stream() << whitespace() << "/**" << endl; | 479 | stream() << whitespace() << "/**\n"; | ||
485 | stream() << whitespace() << " Get Item object corresponding to " << entry->name << "()" | 480 | stream() << whitespace() << " Get Item object corresponding to " << entry->name << "()" | ||
486 | << endl; | 481 | << '\n'; | ||
487 | stream() << whitespace() << "*/" << endl; | 482 | stream() << whitespace() << "*/\n"; | ||
488 | stream() << whitespace() << "Item" << itemType(entry->type) << " *" | 483 | stream() << whitespace() << "Item" << itemType(entry->type) << " *" | ||
489 | << getFunction(entry->name) << "Item("; | 484 | << getFunction(entry->name) << "Item("; | ||
490 | if (!entry->param.isEmpty()) { | 485 | if (!entry->param.isEmpty()) { | ||
491 | stream() << " " << cppType(entry->paramType) << " i "; | 486 | stream() << " " << cppType(entry->paramType) << " i "; | ||
492 | } | 487 | } | ||
493 | stream() << ")"; | 488 | stream() << ")"; | ||
494 | if (!cfg().dpointer) { | 489 | if (!cfg().dpointer) { | ||
495 | stream() << endl; | 490 | stream() << '\n'; | ||
496 | startScope(); | 491 | startScope(); | ||
497 | stream() << whitespace() << itemAccessorBody(entry, cfg()); | 492 | stream() << whitespace() << itemAccessorBody(entry, cfg()); | ||
498 | endScope(); | 493 | endScope(); | ||
499 | } else { | 494 | } else { | ||
500 | stream() << ";" << endl; | 495 | stream() << ";\n"; | ||
501 | } | 496 | } | ||
502 | 497 | | |||
503 | stream() <<endl; | 498 | stream() <<endl; | ||
504 | } | 499 | } | ||
505 | 500 | | |||
506 | void KConfigHeaderGenerator::createDefaultValueMember(const CfgEntry *entry) | 501 | void KConfigHeaderGenerator::createDefaultValueMember(const CfgEntry *entry) | ||
507 | { | 502 | { | ||
508 | // Default value Accessor | 503 | // Default value Accessor | ||
509 | if (! ((cfg().allDefaultGetters || cfg().defaultGetters.contains(entry->name)) && !entry->defaultValue.isEmpty())) { | 504 | if (! ((cfg().allDefaultGetters || cfg().defaultGetters.contains(entry->name)) && !entry->defaultValue.isEmpty())) { | ||
510 | return; | 505 | return; | ||
511 | } | 506 | } | ||
512 | stream() << whitespace() << "/**" << endl; | 507 | stream() << whitespace() << "/**\n"; | ||
513 | stream() << whitespace() << " Get " << entry->label << " default value" << endl; | 508 | stream() << whitespace() << " Get " << entry->label << " default value\n"; | ||
514 | stream() << whitespace() << "*/" << endl; | 509 | stream() << whitespace() << "*/\n"; | ||
515 | if (cfg().staticAccessors) { | 510 | if (cfg().staticAccessors) { | ||
516 | stream() << whitespace() << "static" << endl; | 511 | stream() << whitespace() << "static\n"; | ||
517 | } | 512 | } | ||
518 | stream() << whitespace() << ""; | 513 | stream() << whitespace() << ""; | ||
519 | if (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) { | 514 | if (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) { | ||
520 | stream() << enumType(entry, cfg().globalEnums); | 515 | stream() << enumType(entry, cfg().globalEnums); | ||
521 | } else { | 516 | } else { | ||
522 | stream() << cppType(entry->type); | 517 | stream() << cppType(entry->type); | ||
523 | } | 518 | } | ||
524 | stream() << " " << getDefaultFunction(entry->name) << "("; | 519 | stream() << " " << getDefaultFunction(entry->name) << "("; | ||
525 | if (!entry->param.isEmpty()) { | 520 | if (!entry->param.isEmpty()) { | ||
526 | stream() << " " << cppType(entry->paramType) << " i "; | 521 | stream() << " " << cppType(entry->paramType) << " i "; | ||
527 | } | 522 | } | ||
528 | stream() << ")" << Const() << endl; | 523 | stream() << ")" << Const() << '\n'; | ||
529 | stream() << whitespace() << "{" << endl; | 524 | stream() << whitespace() << "{\n"; | ||
530 | stream() << whitespace() << " return "; | 525 | stream() << whitespace() << " return "; | ||
531 | if (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) { | 526 | if (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) { | ||
532 | stream() << "static_cast<" << enumType(entry, cfg().globalEnums) << ">("; | 527 | stream() << "static_cast<" << enumType(entry, cfg().globalEnums) << ">("; | ||
533 | } | 528 | } | ||
534 | stream() << getDefaultFunction(entry->name) << "_helper("; | 529 | stream() << getDefaultFunction(entry->name) << "_helper("; | ||
535 | if (!entry->param.isEmpty()) { | 530 | if (!entry->param.isEmpty()) { | ||
536 | stream() << " i "; | 531 | stream() << " i "; | ||
537 | } | 532 | } | ||
538 | stream() << ")"; | 533 | stream() << ")"; | ||
539 | if (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) { | 534 | if (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) { | ||
540 | stream() << ")"; | 535 | stream() << ")"; | ||
541 | } | 536 | } | ||
542 | stream() << ";" << endl; | 537 | stream() << ";\n"; | ||
543 | stream() << whitespace() << "}" << endl; | 538 | stream() << whitespace() << "}\n"; | ||
544 | stream() << endl; | 539 | stream() << '\n'; | ||
545 | } | 540 | } | ||
546 | 541 | | |||
547 | void KConfigHeaderGenerator::createSingleton() | 542 | void KConfigHeaderGenerator::createSingleton() | ||
548 | { | 543 | { | ||
549 | // Private constructor for singleton | 544 | // Private constructor for singleton | ||
550 | if (!cfg().singleton) { | 545 | if (!cfg().singleton) { | ||
551 | return; | 546 | return; | ||
552 | } | 547 | } | ||
553 | 548 | | |||
554 | stream() << whitespace() << "" << cfg().className << "("; | 549 | stream() << whitespace() << "" << cfg().className << "("; | ||
555 | if (parseResult.cfgFileNameArg) { | 550 | if (parseResult.cfgFileNameArg) { | ||
556 | stream() << "KSharedConfig::Ptr config"; | 551 | stream() << "KSharedConfig::Ptr config"; | ||
557 | } | 552 | } | ||
558 | if (cfg().parentInConstructor) { | 553 | if (cfg().parentInConstructor) { | ||
559 | if (parseResult.cfgFileNameArg) { | 554 | if (parseResult.cfgFileNameArg) { | ||
560 | stream() << ", "; | 555 | stream() << ", "; | ||
561 | } | 556 | } | ||
562 | stream() << "QObject *parent = nullptr"; | 557 | stream() << "QObject *parent = nullptr"; | ||
563 | } | 558 | } | ||
564 | stream() << ");" << endl; | 559 | stream() << ");\n"; | ||
565 | stream() << whitespace() << "friend class " << cfg().className << "Helper;" << endl << endl; | 560 | stream() << whitespace() << "friend class " << cfg().className << "Helper;\n\n"; | ||
566 | } | 561 | } | ||
567 | 562 | | |||
568 | void KConfigHeaderGenerator::createNonDPointerHelpers() | 563 | void KConfigHeaderGenerator::createNonDPointerHelpers() | ||
569 | { | 564 | { | ||
570 | if (cfg().memberVariables == QLatin1String("dpointer")) { | 565 | if (cfg().memberVariables == QLatin1String("dpointer")) { | ||
571 | return; | 566 | return; | ||
572 | } | 567 | } | ||
573 | 568 | | |||
574 | QString group; | 569 | QString group; | ||
575 | for (auto *entry : parseResult.entries) { | 570 | for (auto *entry : parseResult.entries) { | ||
576 | if (entry->group != group) { | 571 | if (entry->group != group) { | ||
577 | group = entry->group; | 572 | group = entry->group; | ||
578 | stream() << endl; | 573 | stream() << '\n'; | ||
579 | stream() << whitespace() << "// " << group << endl; | 574 | stream() << whitespace() << "// " << group << '\n'; | ||
580 | } | 575 | } | ||
581 | stream() << whitespace() << "" << cppType(entry->type) << " " << varName(entry->name, cfg()); | 576 | stream() << whitespace() << "" << cppType(entry->type) << " " << varName(entry->name, cfg()); | ||
582 | if (!entry->param.isEmpty()) { | 577 | if (!entry->param.isEmpty()) { | ||
583 | stream() << QStringLiteral("[%1]").arg(entry->paramMax + 1); | 578 | stream() << QStringLiteral("[%1]").arg(entry->paramMax + 1); | ||
584 | } | 579 | } | ||
585 | stream() << ";" << endl; | 580 | stream() << ";\n"; | ||
586 | 581 | | |||
587 | if (cfg().allDefaultGetters || cfg().defaultGetters.contains(entry->name)) { | 582 | if (cfg().allDefaultGetters || cfg().defaultGetters.contains(entry->name)) { | ||
588 | stream() << whitespace() << ""; | 583 | stream() << whitespace() << ""; | ||
589 | if (cfg().staticAccessors) { | 584 | if (cfg().staticAccessors) { | ||
590 | stream() << "static "; | 585 | stream() << "static "; | ||
591 | } | 586 | } | ||
592 | stream() << cppType(entry->type) << " " << getDefaultFunction(entry->name) << "_helper("; | 587 | stream() << cppType(entry->type) << " " << getDefaultFunction(entry->name) << "_helper("; | ||
593 | if (!entry->param.isEmpty()) { | 588 | if (!entry->param.isEmpty()) { | ||
594 | stream() << " " << cppType(entry->paramType) << " i "; | 589 | stream() << " " << cppType(entry->paramType) << " i "; | ||
595 | } | 590 | } | ||
596 | stream() << ")" << Const() << ";" << endl; | 591 | stream() << ")" << Const() << ";\n"; | ||
597 | } | 592 | } | ||
598 | } | 593 | } | ||
599 | 594 | | |||
600 | stream() << endl << " private:" << endl; | 595 | stream() << "\n private:\n"; | ||
601 | if (cfg().itemAccessors) { | 596 | if (cfg().itemAccessors) { | ||
602 | for (auto *entry : parseResult.entries) { | 597 | for (auto *entry : parseResult.entries) { | ||
603 | stream() << whitespace() << "Item" << itemType(entry->type) << " *" << itemVar(entry, cfg()); | 598 | stream() << whitespace() << "Item" << itemType(entry->type) << " *" << itemVar(entry, cfg()); | ||
604 | if (!entry->param.isEmpty()) { | 599 | if (!entry->param.isEmpty()) { | ||
605 | stream() << QStringLiteral("[%1]").arg(entry->paramMax + 1); | 600 | stream() << QStringLiteral("[%1]").arg(entry->paramMax + 1); | ||
606 | } | 601 | } | ||
607 | stream() << ";" << endl; | 602 | stream() << ";\n"; | ||
608 | } | 603 | } | ||
609 | } | 604 | } | ||
610 | 605 | | |||
611 | if (parseResult.hasNonModifySignals) { | 606 | if (parseResult.hasNonModifySignals) { | ||
612 | stream() << whitespace() << "uint " << varName(QStringLiteral("settingsChanged"), cfg()) << ";" << endl; | 607 | stream() << whitespace() << "uint " << varName(QStringLiteral("settingsChanged"), cfg()) << ";\n"; | ||
613 | } | 608 | } | ||
614 | } | 609 | } |