KConfig

KConfigHeaderGenerator.cpp
1/*
2 This file is part of the KDE libraries.
3
4 SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
5 SPDX-FileCopyrightText: 2003 Waldo Bastian <bastian@kde.org>
6 SPDX-FileCopyrightText: 2003 Zack Rusin <zack@kde.org>
7 SPDX-FileCopyrightText: 2006 Michaƫl Larouche <michael.larouche@kdemail.net>
8 SPDX-FileCopyrightText: 2008 Allen Winter <winter@kde.org>
9 SPDX-FileCopyrightText: 2020 Tomaz Cananbrava <tcanabrava@kde.org>
10
11 SPDX-License-Identifier: LGPL-2.0-or-later
12*/
13
14#include "KConfigHeaderGenerator.h"
15
16#include <QDebug>
17#include <QTextStream>
18#include <iostream>
19
20KConfigHeaderGenerator::KConfigHeaderGenerator(const QString &inputFile, const QString &baseDir, const KConfigParameters &cfg, ParseResult &result)
21 : KConfigCodeGeneratorBase(inputFile, baseDir, baseDir + cfg.baseName + QLatin1Char('.') + cfg.headerExtension, cfg, result)
22{
23}
24
25void KConfigHeaderGenerator::start()
26{
27 KConfigCodeGeneratorBase::start();
28 startHeaderGuards();
29 createHeaders();
30
31 beginNamespaces();
32
33 createForwardDeclarations();
34
35 doClassDefinition();
36
37 endNamespaces();
38 endHeaderGuards();
39}
40
41void KConfigHeaderGenerator::doClassDefinition()
42{
43 stream() << "class " << cfg().visibility << cfg().className << " : public " << cfg().inherits << '\n';
44 startScope();
45
46 // Add Q_OBJECT macro if the config need signals.
47 if (!parseResult.signalList.isEmpty() || cfg().generateProperties) {
48 stream() << " Q_OBJECT\n";
49 }
50
51 if (cfg().qmlRegistration) {
52 stream() << " QML_ELEMENT\n";
53
54 if (cfg().singleton) {
55 stream() << " QML_SINGLETON\n";
56 }
57
58 if (cfg().qmlUncreatable) {
59 stream() << " QML_UNCREATABLE(\"\")\n";
60 }
61 }
62 stream() << " public:\n";
63 implementEnums();
64 createConstructor();
65 createDestructor();
66
67 for (const auto *entry : std::as_const(parseResult.entries)) {
68 const QString returnType = (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) ? enumType(entry, cfg().globalEnums) : cppType(entry->type);
69
70 createSetters(entry);
71 createProperties(entry, returnType);
72 createImmutableProperty(entry);
73 createGetters(entry, returnType);
74 createImmutableGetters(entry);
75 createDefaultValueMember(entry);
76 createItemAcessors(entry, returnType);
77 }
78
79 createSignals();
80 stream() << " protected:\n";
81 createSingleton();
82
83 // TODO: Move those to functions too.
84 if (parseResult.hasNonModifySignals) {
85 stream() << whitespace() << "bool usrSave() override;\n";
86 }
87
88 // Member variables
89 if (!cfg().memberVariables.isEmpty() //
90 && cfg().memberVariables != QLatin1String("private") //
91 && cfg().memberVariables != QLatin1String("dpointer")) {
92 stream() << " " << cfg().memberVariables << ":\n";
93 }
94
95 // Class Parameters
96 for (const auto &parameter : std::as_const(parseResult.parameters)) {
97 stream() << whitespace() << "" << cppType(parameter.type) << " mParam" << parameter.name << ";\n";
98 }
99
100 createNonDPointerHelpers();
101 createDPointer();
102
103 if (cfg().customAddons) {
104 stream() << whitespace() << "// Include custom additions\n";
105 stream() << whitespace() << "#include \"" << cfg().baseName << "_addons." << cfg().headerExtension << "\"\n";
106 }
107
108 endScope(ScopeFinalizer::Semicolon);
109}
110
111void KConfigHeaderGenerator::createHeaders()
112{
113 addHeaders(cfg().headerIncludes);
114 if (cfg().headerIncludes.size()) {
115 stream() << '\n';
116 }
117
118 if (!cfg().singleton && parseResult.parameters.isEmpty()) {
119 addHeaders({QStringLiteral("qglobal.h")});
120 }
121
122 if (cfg().inherits == QLatin1String("KCoreConfigSkeleton")) {
123 addHeaders({QStringLiteral("kcoreconfigskeleton.h")});
124 } else {
125 addHeaders({QStringLiteral("kconfigskeleton.h")});
126 }
127
128 addHeaders({QStringLiteral("QCoreApplication"), QStringLiteral("QDebug")});
129 if (!cfg().dpointer && parseResult.hasNonModifySignals) {
130 addHeaders({QStringLiteral("QSet")});
131 }
132
133 if (cfg().qmlRegistration) {
134 addHeaders({QStringLiteral("qqmlintegration.h")});
135 }
136
137 stream() << '\n';
138
139 addHeaders(parseResult.includes);
140 if (parseResult.includes.size()) {
141 stream() << '\n';
142 }
143}
144
145void KConfigHeaderGenerator::startHeaderGuards()
146{
147 const bool hasNamespace = !cfg().nameSpace.isEmpty();
148 const QString namespaceName = QString(QString(cfg().nameSpace).replace(QLatin1String("::"), QLatin1String("_"))).toUpper();
149 const QString namespaceStr = hasNamespace ? namespaceName + QLatin1Char('_') : QString{};
150 const QString defineName = namespaceStr + cfg().className.toUpper() + QStringLiteral("_H");
151
152 stream() << "#ifndef " << defineName << '\n';
153 stream() << "#define " << defineName << '\n';
154 stream() << '\n';
155}
156
157void KConfigHeaderGenerator::endHeaderGuards()
158{
159 stream() << '\n';
160 stream() << "#endif";
161 stream() << '\n';
162 // HACK: Original files ended with two last newlines, add them.
163 stream() << '\n';
164}
165
166void KConfigHeaderGenerator::implementChoiceEnums(const CfgEntry *entry, const CfgEntry::Choices &choices)
167{
168 const QList<CfgEntry::Choice> chlist = choices.choices;
169
170 if (chlist.isEmpty()) {
171 return;
172 }
173
174 QStringList values;
175 for (const auto &choice : std::as_const(chlist)) {
176 values.append(choices.prefix + choice.name);
177 }
178
179 if (choices.name().isEmpty()) {
180 if (cfg().globalEnums) {
181 stream() << whitespace() << "enum " << enumName(entry->name, entry->choices) << " { " << values.join(QStringLiteral(", ")) << " };\n";
182 if (cfg().generateProperties) {
183 stream() << whitespace() << "Q_ENUM(" << enumName(entry->name, entry->choices) << ")\n";
184 }
185 } else {
186 // Create an automatically named enum
187 stream() << whitespace() << "class " << enumName(entry->name, entry->choices) << '\n';
188 stream() << whitespace() << "{\n";
189 stream() << whitespace() << " public:\n";
190 stream() << whitespace() << " enum type { " << values.join(QStringLiteral(", ")) << ", COUNT };\n";
191 stream() << whitespace() << "};\n";
192 }
193 } else if (!choices.external()) {
194 // Create a named enum
195 stream() << whitespace() << "enum " << enumName(entry->name, entry->choices) << " { " << values.join(QStringLiteral(", ")) << " };\n";
196 }
197}
198
199void KConfigHeaderGenerator::implementValueEnums(const CfgEntry *entry, const QStringList &values)
200{
201 if (values.isEmpty()) {
202 return;
203 }
204
205 if (cfg().globalEnums) {
206 // ### FIXME!!
207 // make the following string table an index-based string search!
208 // ###
209 stream() << whitespace() << "enum " << enumName(entry->param) << " { " << values.join(QStringLiteral(", ")) << " };\n";
210 stream() << whitespace() << "static const char* const " << enumName(entry->param) << "ToString[];\n";
211 } else {
212 stream() << whitespace() << "class " << enumName(entry->param) << '\n';
213 stream() << whitespace() << "{\n";
214 stream() << whitespace() << " public:\n";
215 stream() << whitespace() << " enum type { " << values.join(QStringLiteral(", ")) << ", COUNT };\n";
216 stream() << whitespace() << " static const char* const enumToString[];\n";
217 stream() << whitespace() << "};\n";
218 }
219}
220
221void KConfigHeaderGenerator::implementEnums()
222{
223 if (!parseResult.entries.size()) {
224 return;
225 }
226
227 for (const auto *entry : std::as_const(parseResult.entries)) {
228 const CfgEntry::Choices &choices = entry->choices;
229 const QStringList values = entry->paramValues;
230
231 implementChoiceEnums(entry, choices);
232 implementValueEnums(entry, values);
233 }
234 stream() << '\n';
235}
236
237void KConfigHeaderGenerator::createSignals()
238{
239 // Signal definition.
240 if (parseResult.signalList.isEmpty()) {
241 return;
242 }
243
244 stream() << "\n enum {\n";
245
246 // HACK: Use C-Style for add a comma in all but the last element,
247 // just to make the source generated code equal to the old one.
248 // When we are sure, revert this to a range-based-for and just add
249 // a last comma, as it's valid c++.
250 for (int i = 0, end = parseResult.signalList.size(); i < end; i++) {
251 auto signal = parseResult.signalList.at(i);
252 stream() << whitespace() << " " << signalEnumName(signal.name) << " = " << (i + 1);
253 if (i != end - 1) {
254 stream() << ",\n";
255 }
256 }
257 stream() << '\n';
258 stream() << whitespace() << "};\n\n";
259
260 stream() << " Q_SIGNALS:";
261 for (const Signal &signal : std::as_const(parseResult.signalList)) {
262 stream() << '\n';
263 if (!signal.label.isEmpty()) {
264 stream() << whitespace() << "/**\n";
265 stream() << whitespace() << " " << signal.label << '\n';
266 stream() << whitespace() << "*/\n";
267 }
268 stream() << whitespace() << "void " << signal.name << "(";
269
270 auto it = signal.arguments.cbegin();
271 const auto itEnd = signal.arguments.cend();
272 while (it != itEnd) {
273 Param argument = *it;
274 QString type = param(argument.type);
275 if (cfg().useEnumTypes && argument.type == QLatin1String("Enum")) {
276 for (const auto *entry : std::as_const(parseResult.entries)) {
277 if (entry->name == argument.name) {
278 type = enumType(entry, cfg().globalEnums);
279 break;
280 }
281 }
282 }
283 stream() << type << " " << argument.name;
284 if (++it != itEnd) {
285 stream() << ", ";
286 }
287 }
288 stream() << ");\n";
289 }
290 stream() << '\n';
291
292 stream() << " private:\n";
293 stream() << whitespace() << "void itemChanged(quint64 signalFlag);\n";
294 stream() << '\n';
295}
296
297void KConfigHeaderGenerator::createDPointer()
298{
299 if (!cfg().dpointer) {
300 return;
301 }
302
303 // use a private class for both member variables and items
304 stream() << " private:\n";
305 for (const auto *entry : std::as_const(parseResult.entries)) {
306 if (cfg().allDefaultGetters || cfg().defaultGetters.contains(entry->name)) {
307 stream() << whitespace() << "";
308 if (cfg().staticAccessors) {
309 stream() << "static ";
310 }
311 stream() << cppType(entry->type) << " " << getDefaultFunction(entry->name) << "_helper(";
312 if (!entry->param.isEmpty()) {
313 stream() << " " << cppType(entry->paramType) << " i ";
314 }
315 stream() << ")" << Const() << ";\n";
316 }
317 }
318 stream() << whitespace() << "" << cfg().className << "Private *d;\n";
319}
320
321void KConfigHeaderGenerator::createConstructor()
322{
323 if (cfg().singleton) {
324 stream() << whitespace() << "static " << cfg().className << " *self();\n";
325
326 if (cfg().qmlRegistration) {
327 stream() << whitespace() << "static " << cfg().className << " *create(QQmlEngine *, QJSEngine *);\n";
328 }
329
330 if (parseResult.cfgFileNameArg) {
331 stream() << whitespace() << "static void instance(const QString& cfgfilename);\n";
332 stream() << whitespace() << "static void instance(KSharedConfig::Ptr config);\n";
333 }
334 return;
335 }
336
337 stream() << whitespace() << "" << cfg().className << "(";
338 if (parseResult.cfgFileNameArg) {
339 if (cfg().forceStringFilename) {
340 stream() << " const QString &cfgfilename" << (parseResult.parameters.isEmpty() ? " = QString()" : ", ");
341 } else if (parseResult.cfgStateConfig) {
342 stream() << " KSharedConfig::Ptr config" << (parseResult.parameters.isEmpty() ? " = KSharedConfig::openStateConfig()" : ", ");
343 } else {
344 stream() << " KSharedConfig::Ptr config" << (parseResult.parameters.isEmpty() ? " = KSharedConfig::openConfig()" : ", ");
345 }
346 }
347 if (cfg().forceStringFilename && parseResult.cfgStateConfig) {
348 std::cerr << "One can not use ForceStringFilename and use the stateConfig attribute, consider "
349 "removing the ForceStringFilename kcfgc option if you want to use state data"
350 << std::endl;
351 }
352
353 bool first = true;
354 for (const auto &parameter : std::as_const(parseResult.parameters)) {
355 if (first) {
356 first = false;
357 } else {
358 stream() << ",";
359 }
360
361 stream() << " " << param(parameter.type) << " " << parameter.name;
362 }
363
364 if (cfg().parentInConstructor) {
365 if (parseResult.cfgFileNameArg || !parseResult.parameters.isEmpty()) {
366 stream() << ",";
367 }
368 stream() << " QObject *parent = nullptr";
369 }
370 stream() << " );\n";
371}
372
373void KConfigHeaderGenerator::createDestructor()
374{
375 stream() << whitespace() << "~" << cfg().className << "() override;\n\n";
376}
377
378void KConfigHeaderGenerator::createForwardDeclarations()
379{
380 // Private class declaration
381 if (cfg().dpointer) {
382 stream() << "class " << cfg().className << "Private;\n\n";
383 }
384
385 if (cfg().qmlRegistration && cfg().singleton) {
386 stream() << "class QQmlEngine;\n";
387 stream() << "class QJSEngine;\n\n";
388 }
389}
390
391void KConfigHeaderGenerator::createProperties(const CfgEntry *entry, const QString &returnType)
392{
393 if (!cfg().generateProperties) {
394 return;
395 }
396 stream() << whitespace() << "Q_PROPERTY(" << returnType << ' ' << getFunction(entry->name);
397 stream() << " READ " << getFunction(entry->name);
398
399 if (cfg().allMutators || cfg().mutators.contains(entry->name)) {
400 const QString signal = changeSignalName(entry->name);
401 stream() << " WRITE " << setFunction(entry->name);
402 stream() << " NOTIFY " << signal;
403
404 // If we have the modified signal, we'll also need
405 // the changed signal as well
406 Signal s;
407 s.name = signal;
408 s.modify = true;
409 parseResult.signalList.append(s);
410 } else {
411 stream() << " CONSTANT";
412 }
413 stream() << ")\n";
414}
415
416void KConfigHeaderGenerator::createImmutableProperty(const CfgEntry *entry)
417{
418 if (!cfg().generateProperties) {
419 return;
420 }
421 stream() << whitespace();
422 stream() << "Q_PROPERTY(bool " << immutableFunction(entry->name);
423 stream() << " READ " << immutableFunction(entry->name);
424 stream() << " CONSTANT)\n";
425}
426
427void KConfigHeaderGenerator::createSetters(const CfgEntry *entry)
428{
429 // Manipulator
430 if (!cfg().allMutators && !cfg().mutators.contains(entry->name)) {
431 return;
432 }
433
434 stream() << whitespace() << "/**\n";
435 stream() << whitespace() << " Set " << entry->label << '\n';
436 stream() << whitespace() << "*/\n";
437
438 if (cfg().staticAccessors) {
439 stream() << whitespace() << "static\n";
440 }
441
442 stream() << whitespace() << "void " << setFunction(entry->name) << "( ";
443 if (!entry->param.isEmpty()) {
444 stream() << cppType(entry->paramType) << " i, ";
445 }
446
447 stream() << (cfg().useEnumTypes && entry->type == QLatin1String("Enum") ? enumType(entry, cfg().globalEnums) : param(entry->type));
448
449 stream() << " v )";
450
451 // function body inline only if not using dpointer
452 // for BC mode
453 if (!cfg().dpointer) {
454 stream() << '\n';
455 startScope();
456 memberMutatorBody(entry);
457 endScope();
458 stream() << '\n';
459 } else {
460 stream() << ";\n\n";
461 }
462}
463
464void KConfigHeaderGenerator::createGetters(const CfgEntry *entry, const QString &returnType)
465{
466 // Accessor
467 stream() << whitespace() << "/**\n";
468 stream() << whitespace() << " Get " << entry->label << '\n';
469 stream() << whitespace() << "*/\n";
470 if (cfg().staticAccessors) {
471 stream() << whitespace() << "static\n";
472 }
473 stream() << whitespace() << "";
474 stream() << returnType;
475 stream() << " " << getFunction(entry->name) << "(";
476 if (!entry->param.isEmpty()) {
477 stream() << " " << cppType(entry->paramType) << " i ";
478 }
479 stream() << ")" << Const();
480
481 // function body inline only if not using dpointer
482 // for BC mode
483 if (!cfg().dpointer) {
484 stream() << '\n';
485 startScope();
486 stream() << whitespace() << memberAccessorBody(entry, cfg().globalEnums);
487 endScope();
488 stream() << '\n';
489 } else {
490 stream() << ";\n\n";
491 }
492}
493
494void KConfigHeaderGenerator::createImmutableGetters(const CfgEntry *entry)
495{
496 stream() << whitespace() << "/**\n";
497 stream() << whitespace() << " Is " << entry->label << " Immutable\n";
498 stream() << whitespace() << "*/\n";
499 // Immutable
500 if (cfg().staticAccessors) {
501 stream() << whitespace() << "static\n";
502 }
503 stream() << whitespace() << "";
504 stream() << "bool " << immutableFunction(entry->name) << "(";
505 if (!entry->param.isEmpty()) {
506 stream() << " " << cppType(entry->paramType) << " i ";
507 }
508 stream() << ")" << Const();
509 // function body inline only if not using dpointer
510 // for BC mode
511 if (!cfg().dpointer) {
512 stream() << '\n';
513 startScope();
514 memberImmutableBody(entry, cfg().globalEnums);
515 endScope();
516 stream() << '\n';
517 } else {
518 stream() << ";\n\n";
519 }
520}
521
522void KConfigHeaderGenerator::createItemAcessors(const CfgEntry *entry, const QString &returnType)
523{
524 Q_UNUSED(returnType)
525
526 // Item accessor
527 if (!cfg().itemAccessors) {
528 return;
529 }
530
531 const QString declType = entry->signalList.isEmpty() ? QStringLiteral("Item") + itemType(entry->type) : QStringLiteral("KConfigCompilerSignallingItem");
532
533 stream() << whitespace() << "/**\n";
534 stream() << whitespace() << " Get Item object corresponding to " << entry->name << "()" << '\n';
535 stream() << whitespace() << "*/\n";
536 stream() << whitespace() << declType << " *" << getFunction(entry->name) << "Item(";
537 if (!entry->param.isEmpty()) {
538 stream() << " " << cppType(entry->paramType) << " i ";
539 }
540 stream() << ")";
541 if (!cfg().dpointer) {
542 stream() << '\n';
543 startScope();
544 stream() << whitespace() << itemAccessorBody(entry, cfg());
545 endScope();
546 } else {
547 stream() << ";\n";
548 }
549
550 stream() << '\n';
551}
552
553void KConfigHeaderGenerator::createDefaultValueMember(const CfgEntry *entry)
554{
555 // Default value Accessor
556 if (!((cfg().allDefaultGetters || cfg().defaultGetters.contains(entry->name)) && !entry->defaultValue.isEmpty())) {
557 return;
558 }
559 stream() << whitespace() << "/**\n";
560 stream() << whitespace() << " Get " << entry->label << " default value\n";
561 stream() << whitespace() << "*/\n";
562 if (cfg().staticAccessors) {
563 stream() << whitespace() << "static\n";
564 }
565 stream() << whitespace() << "";
566 if (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) {
567 stream() << enumType(entry, cfg().globalEnums);
568 } else {
569 stream() << cppType(entry->type);
570 }
571 stream() << " " << getDefaultFunction(entry->name) << "(";
572 if (!entry->param.isEmpty()) {
573 stream() << " " << cppType(entry->paramType) << " i ";
574 }
575 stream() << ")" << Const() << '\n';
576 stream() << whitespace() << "{\n";
577 stream() << whitespace() << " return ";
578 if (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) {
579 stream() << "static_cast<" << enumType(entry, cfg().globalEnums) << ">(";
580 }
581 stream() << getDefaultFunction(entry->name) << "_helper(";
582 if (!entry->param.isEmpty()) {
583 stream() << " i ";
584 }
585 stream() << ")";
586 if (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) {
587 stream() << ")";
588 }
589 stream() << ";\n";
590 stream() << whitespace() << "}\n";
591 stream() << '\n';
592}
593
594void KConfigHeaderGenerator::createSingleton()
595{
596 // Private constructor for singleton
597 if (!cfg().singleton) {
598 return;
599 }
600
601 stream() << whitespace() << "" << cfg().className << "(";
602 if (parseResult.cfgFileNameArg) {
603 stream() << "KSharedConfig::Ptr config";
604 }
605 if (cfg().parentInConstructor) {
606 if (parseResult.cfgFileNameArg) {
607 stream() << ", ";
608 }
609 stream() << "QObject *parent = nullptr";
610 }
611 stream() << ");\n";
612 stream() << whitespace() << "friend class " << cfg().className << "Helper;\n\n";
613}
614
615void KConfigHeaderGenerator::createNonDPointerHelpers()
616{
617 if (cfg().memberVariables == QLatin1String("dpointer")) {
618 return;
619 }
620
621 QString group;
622 for (const auto *entry : std::as_const(parseResult.entries)) {
623 if (entry->group != group) {
624 group = entry->group;
625 stream() << '\n';
626 stream() << whitespace() << "// " << group << '\n';
627 }
628 stream() << whitespace() << "" << cppType(entry->type) << " " << varName(entry->name, cfg());
629 if (!entry->param.isEmpty()) {
630 stream() << QStringLiteral("[%1]").arg(entry->paramMax + 1);
631 }
632 stream() << ";\n";
633
634 if (cfg().allDefaultGetters || cfg().defaultGetters.contains(entry->name)) {
635 stream() << whitespace() << "";
636 if (cfg().staticAccessors) {
637 stream() << "static ";
638 }
639 stream() << cppType(entry->type) << " " << getDefaultFunction(entry->name) << "_helper(";
640 if (!entry->param.isEmpty()) {
641 stream() << " " << cppType(entry->paramType) << " i ";
642 }
643 stream() << ")" << Const() << ";\n";
644 }
645 }
646
647 stream() << "\n private:\n";
648 if (cfg().itemAccessors) {
649 for (const auto *entry : std::as_const(parseResult.entries)) {
650 const QString declType =
651 entry->signalList.isEmpty() ? QStringLiteral("Item") + itemType(entry->type) : QStringLiteral("KConfigCompilerSignallingItem");
652 stream() << whitespace() << declType << " *" << itemVar(entry, cfg());
653 if (!entry->param.isEmpty()) {
654 stream() << QStringLiteral("[%1]").arg(entry->paramMax + 1);
655 }
656 stream() << ";\n";
657 }
658 }
659
660 if (parseResult.hasNonModifySignals) {
661 stream() << whitespace() << "QSet<quint64> " << varName(QStringLiteral("settingsChanged"), cfg()) << ";\n";
662 }
663}
Configuration Compiler Configuration.
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
QAction * replace(const QObject *recvr, const char *slot, QObject *parent)
void append(QList< T > &&value)
bool isEmpty() const const
bool isEmpty() const const
QString toUpper() const const
QString join(QChar separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:55:16 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.