14#include "KConfigHeaderGenerator.h"
21 : KConfigCodeGeneratorBase(inputFile, baseDir, baseDir + cfg.baseName +
QLatin1Char(
'.') + cfg.headerExtension, cfg, result)
25void KConfigHeaderGenerator::start()
27 KConfigCodeGeneratorBase::start();
33 createForwardDeclarations();
41void KConfigHeaderGenerator::doClassDefinition()
43 stream() <<
"class " << cfg().visibility << cfg().className <<
" : public " << cfg().inherits <<
'\n';
47 if (!parseResult.signalList.isEmpty() || cfg().generateProperties) {
48 stream() <<
" Q_OBJECT\n";
51 if (cfg().qmlRegistration) {
52 stream() <<
" QML_ELEMENT\n";
54 if (cfg().singleton) {
55 stream() <<
" QML_SINGLETON\n";
58 if (cfg().qmlUncreatable) {
59 stream() <<
" QML_UNCREATABLE(\"\")\n";
62 stream() <<
" public:\n";
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);
71 createProperties(entry, returnType);
72 createImmutableProperty(entry);
73 createGetters(entry, returnType);
74 createImmutableGetters(entry);
75 createDefaultValueMember(entry);
76 createItemAcessors(entry, returnType);
80 stream() <<
" protected:\n";
84 if (parseResult.hasNonModifySignals) {
85 stream() << whitespace() <<
"bool usrSave() override;\n";
89 if (!cfg().memberVariables.isEmpty()
90 && cfg().memberVariables != QLatin1String(
"private")
91 && cfg().memberVariables != QLatin1String(
"dpointer")) {
92 stream() <<
" " << cfg().memberVariables <<
":\n";
96 for (
const auto ¶meter : std::as_const(parseResult.parameters)) {
97 stream() << whitespace() <<
"" << cppType(parameter.type) <<
" mParam" << parameter.name <<
";\n";
100 createNonDPointerHelpers();
103 if (cfg().customAddons) {
104 stream() << whitespace() <<
"// Include custom additions\n";
105 stream() << whitespace() <<
"#include \"" << cfg().baseName <<
"_addons." << cfg().headerExtension <<
"\"\n";
108 endScope(ScopeFinalizer::Semicolon);
111void KConfigHeaderGenerator::createHeaders()
113 addHeaders(cfg().headerIncludes);
114 if (cfg().headerIncludes.size()) {
118 if (!cfg().singleton && parseResult.parameters.isEmpty()) {
119 addHeaders({QStringLiteral(
"qglobal.h")});
122 if (cfg().inherits == QLatin1String(
"KCoreConfigSkeleton")) {
123 addHeaders({QStringLiteral(
"kcoreconfigskeleton.h")});
125 addHeaders({QStringLiteral(
"kconfigskeleton.h")});
128 addHeaders({QStringLiteral(
"QCoreApplication"), QStringLiteral(
"QDebug")});
129 if (!cfg().dpointer && parseResult.hasNonModifySignals) {
130 addHeaders({QStringLiteral(
"QSet")});
133 if (cfg().qmlRegistration) {
134 addHeaders({QStringLiteral(
"qqmlintegration.h")});
139 addHeaders(parseResult.includes);
140 if (parseResult.includes.size()) {
145void KConfigHeaderGenerator::startHeaderGuards()
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");
152 stream() <<
"#ifndef " << defineName <<
'\n';
153 stream() <<
"#define " << defineName <<
'\n';
157void KConfigHeaderGenerator::endHeaderGuards()
160 stream() <<
"#endif";
166void KConfigHeaderGenerator::implementChoiceEnums(
const CfgEntry *entry,
const CfgEntry::Choices &choices)
168 const QList<CfgEntry::Choice> chlist = choices.choices;
175 for (
const auto &choice : std::as_const(chlist)) {
176 values.
append(choices.prefix + choice.name);
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";
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";
193 }
else if (!choices.external()) {
195 stream() << whitespace() <<
"enum " << enumName(entry->name, entry->choices) <<
" { " << values.
join(QStringLiteral(
", ")) <<
" };\n";
199void KConfigHeaderGenerator::implementValueEnums(
const CfgEntry *entry,
const QStringList &values)
205 if (cfg().globalEnums) {
209 stream() << whitespace() <<
"enum " << enumName(entry->param) <<
" { " << values.
join(QStringLiteral(
", ")) <<
" };\n";
210 stream() << whitespace() <<
"static const char* const " << enumName(entry->param) <<
"ToString[];\n";
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";
221void KConfigHeaderGenerator::implementEnums()
223 if (!parseResult.entries.size()) {
227 for (
const auto *entry : std::as_const(parseResult.entries)) {
228 const CfgEntry::Choices &choices = entry->choices;
229 const QStringList values = entry->paramValues;
231 implementChoiceEnums(entry, choices);
232 implementValueEnums(entry, values);
237void KConfigHeaderGenerator::createSignals()
240 if (parseResult.signalList.isEmpty()) {
244 stream() <<
"\n enum {\n";
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);
258 stream() << whitespace() <<
"};\n\n";
260 stream() <<
" Q_SIGNALS:";
261 for (
const Signal &signal : std::as_const(parseResult.signalList)) {
263 if (!signal.label.isEmpty()) {
264 stream() << whitespace() <<
"/**\n";
265 stream() << whitespace() <<
" " << signal.label <<
'\n';
266 stream() << whitespace() <<
"*/\n";
268 stream() << whitespace() <<
"void " << signal.name <<
"(";
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);
283 stream() <<
type <<
" " << argument.name;
292 stream() <<
" private:\n";
293 stream() << whitespace() <<
"void itemChanged(quint64 signalFlag);\n";
297void KConfigHeaderGenerator::createDPointer()
299 if (!cfg().dpointer) {
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 ";
311 stream() << cppType(entry->type) <<
" " << getDefaultFunction(entry->name) <<
"_helper(";
313 stream() <<
" " << cppType(entry->paramType) <<
" i ";
315 stream() <<
")" << Const() <<
";\n";
318 stream() << whitespace() <<
"" << cfg().className <<
"Private *d;\n";
321void KConfigHeaderGenerator::createConstructor()
323 if (cfg().singleton) {
324 stream() << whitespace() <<
"static " << cfg().className <<
" *self();\n";
326 if (cfg().qmlRegistration) {
327 stream() << whitespace() <<
"static " << cfg().className <<
" *create(QQmlEngine *, QJSEngine *);\n";
330 if (parseResult.cfgFileNameArg) {
331 stream() << whitespace() <<
"static void instance(const QString& cfgfilename);\n";
332 stream() << whitespace() <<
"static void instance(KSharedConfig::Ptr config);\n";
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()" :
", ");
344 stream() <<
" KSharedConfig::Ptr config" << (parseResult.parameters.isEmpty() ?
" = KSharedConfig::openConfig()" :
", ");
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"
354 for (
const auto ¶meter : std::as_const(parseResult.parameters)) {
361 stream() <<
" " << param(parameter.type) <<
" " << parameter.name;
364 if (cfg().parentInConstructor) {
365 if (parseResult.cfgFileNameArg || !parseResult.parameters.isEmpty()) {
368 stream() <<
" QObject *parent = nullptr";
373void KConfigHeaderGenerator::createDestructor()
375 stream() << whitespace() <<
"~" << cfg().className <<
"() override;\n\n";
378void KConfigHeaderGenerator::createForwardDeclarations()
381 if (cfg().dpointer) {
382 stream() <<
"class " << cfg().className <<
"Private;\n\n";
385 if (cfg().qmlRegistration && cfg().singleton) {
386 stream() <<
"class QQmlEngine;\n";
387 stream() <<
"class QJSEngine;\n\n";
391void KConfigHeaderGenerator::createProperties(
const CfgEntry *entry,
const QString &returnType)
393 if (!cfg().generateProperties) {
396 stream() << whitespace() <<
"Q_PROPERTY(" << returnType <<
' ' << getFunction(entry->name);
397 stream() <<
" READ " << getFunction(entry->name);
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;
409 parseResult.signalList.append(s);
411 stream() <<
" CONSTANT";
416void KConfigHeaderGenerator::createImmutableProperty(
const CfgEntry *entry)
418 if (!cfg().generateProperties) {
421 stream() << whitespace();
422 stream() <<
"Q_PROPERTY(bool " << immutableFunction(entry->name);
423 stream() <<
" READ " << immutableFunction(entry->name);
424 stream() <<
" CONSTANT)\n";
427void KConfigHeaderGenerator::createSetters(
const CfgEntry *entry)
430 if (!cfg().allMutators && !cfg().mutators.contains(entry->name)) {
434 stream() << whitespace() <<
"/**\n";
435 stream() << whitespace() <<
" Set " << entry->label <<
'\n';
436 stream() << whitespace() <<
"*/\n";
438 if (cfg().staticAccessors) {
439 stream() << whitespace() <<
"static\n";
442 stream() << whitespace() <<
"void " << setFunction(entry->name) <<
"( ";
444 stream() << cppType(entry->paramType) <<
" i, ";
447 stream() << (cfg().useEnumTypes && entry->type == QLatin1String(
"Enum") ? enumType(entry, cfg().globalEnums) : param(entry->
type));
453 if (!cfg().dpointer) {
456 memberMutatorBody(entry);
464void KConfigHeaderGenerator::createGetters(
const CfgEntry *entry,
const QString &returnType)
467 stream() << whitespace() <<
"/**\n";
468 stream() << whitespace() <<
" Get " << entry->label <<
'\n';
469 stream() << whitespace() <<
"*/\n";
470 if (cfg().staticAccessors) {
471 stream() << whitespace() <<
"static\n";
473 stream() << whitespace() <<
"";
474 stream() << returnType;
475 stream() <<
" " << getFunction(entry->name) <<
"(";
477 stream() <<
" " << cppType(entry->paramType) <<
" i ";
479 stream() <<
")" << Const();
483 if (!cfg().dpointer) {
486 stream() << whitespace() << memberAccessorBody(entry, cfg().globalEnums);
494void KConfigHeaderGenerator::createImmutableGetters(
const CfgEntry *entry)
496 stream() << whitespace() <<
"/**\n";
497 stream() << whitespace() <<
" Is " << entry->label <<
" Immutable\n";
498 stream() << whitespace() <<
"*/\n";
500 if (cfg().staticAccessors) {
501 stream() << whitespace() <<
"static\n";
503 stream() << whitespace() <<
"";
504 stream() <<
"bool " << immutableFunction(entry->name) <<
"(";
506 stream() <<
" " << cppType(entry->paramType) <<
" i ";
508 stream() <<
")" << Const();
511 if (!cfg().dpointer) {
514 memberImmutableBody(entry, cfg().globalEnums);
522void KConfigHeaderGenerator::createItemAcessors(
const CfgEntry *entry,
const QString &returnType)
527 if (!cfg().itemAccessors) {
531 const QString declType = entry->signalList.
isEmpty() ? QStringLiteral(
"Item") + itemType(entry->type) : QStringLiteral(
"KConfigCompilerSignallingItem");
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(";
538 stream() <<
" " << cppType(entry->paramType) <<
" i ";
541 if (!cfg().dpointer) {
544 stream() << whitespace() << itemAccessorBody(entry, cfg());
553void KConfigHeaderGenerator::createDefaultValueMember(
const CfgEntry *entry)
556 if (!((cfg().allDefaultGetters || cfg().defaultGetters.contains(entry->name)) && !entry->defaultValue.
isEmpty())) {
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";
565 stream() << whitespace() <<
"";
566 if (cfg().useEnumTypes && entry->type == QLatin1String(
"Enum")) {
567 stream() << enumType(entry, cfg().globalEnums);
569 stream() << cppType(entry->type);
571 stream() <<
" " << getDefaultFunction(entry->name) <<
"(";
573 stream() <<
" " << cppType(entry->paramType) <<
" i ";
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) <<
">(";
581 stream() << getDefaultFunction(entry->name) <<
"_helper(";
586 if (cfg().useEnumTypes && entry->type == QLatin1String(
"Enum")) {
590 stream() << whitespace() <<
"}\n";
594void KConfigHeaderGenerator::createSingleton()
597 if (!cfg().singleton) {
601 stream() << whitespace() <<
"" << cfg().className <<
"(";
602 if (parseResult.cfgFileNameArg) {
603 stream() <<
"KSharedConfig::Ptr config";
605 if (cfg().parentInConstructor) {
606 if (parseResult.cfgFileNameArg) {
609 stream() <<
"QObject *parent = nullptr";
612 stream() << whitespace() <<
"friend class " << cfg().className <<
"Helper;\n\n";
615void KConfigHeaderGenerator::createNonDPointerHelpers()
617 if (cfg().memberVariables == QLatin1String(
"dpointer")) {
622 for (
const auto *entry : std::as_const(parseResult.entries)) {
623 if (entry->group != group) {
624 group = entry->group;
626 stream() << whitespace() <<
"// " << group <<
'\n';
628 stream() << whitespace() <<
"" << cppType(entry->type) <<
" " << varName(entry->name, cfg());
630 stream() << QStringLiteral(
"[%1]").arg(entry->paramMax + 1);
634 if (cfg().allDefaultGetters || cfg().defaultGetters.contains(entry->name)) {
635 stream() << whitespace() <<
"";
636 if (cfg().staticAccessors) {
637 stream() <<
"static ";
639 stream() << cppType(entry->type) <<
" " << getDefaultFunction(entry->name) <<
"_helper(";
641 stream() <<
" " << cppType(entry->paramType) <<
" i ";
643 stream() <<
")" << Const() <<
";\n";
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());
654 stream() << QStringLiteral(
"[%1]").arg(entry->paramMax + 1);
660 if (parseResult.hasNonModifySignals) {
661 stream() << whitespace() <<
"QSet<quint64> " << varName(QStringLiteral(
"settingsChanged"), cfg()) <<
";\n";
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