14#include "KConfigCodeGeneratorBase.h"
24KConfigCodeGeneratorBase::KConfigCodeGeneratorBase(
const QString &inputFile,
28 ParseResult &parseResult)
29 : parseResult(parseResult)
30 , m_inputFile(inputFile)
32 , m_fileName(fileName)
35 m_file.setFileName(m_fileName);
37 std::cerr <<
"Can not open '" << qPrintable(m_fileName) <<
"for writing." << std::endl;
40 m_stream.setDevice(&m_file);
42 if (m_cfg.staticAccessors) {
43 m_this = QStringLiteral(
"self()->");
45 m_const = QStringLiteral(
" const");
49KConfigCodeGeneratorBase::~KConfigCodeGeneratorBase()
54void KConfigCodeGeneratorBase::save()
60void KConfigCodeGeneratorBase::indent()
62 if (m_indentLevel >= 4) {
69void KConfigCodeGeneratorBase::unindent()
71 if (m_indentLevel > 4) {
78QString KConfigCodeGeneratorBase::whitespace()
const
81 for (
int i = 0; i < m_indentLevel; i++) {
87void KConfigCodeGeneratorBase::startScope()
94void KConfigCodeGeneratorBase::endScope(ScopeFinalizer finalizer)
98 if (finalizer == ScopeFinalizer::Semicolon) {
104void KConfigCodeGeneratorBase::start()
107 m_stream <<
"// This file is generated by kconfig_compiler_kf6 from " << m_fileName <<
".kcfg"
109 m_stream <<
"// All changes you do to this file will be lost.\n";
112void KConfigCodeGeneratorBase::addHeaders(
const QStringList &headerList)
114 for (
const auto &include : headerList) {
116 m_stream <<
"#include " << include <<
'\n';
118 m_stream <<
"#include <" << include <<
">\n";
125void KConfigCodeGeneratorBase::beginNamespaces()
127 if (!m_cfg.nameSpace.
isEmpty()) {
128 const auto nameSpaceList = m_cfg.nameSpace.
split(QStringLiteral(
"::"));
129 for (
const QString &ns : nameSpaceList) {
130 m_stream <<
"namespace " << ns <<
" {\n";
138void KConfigCodeGeneratorBase::endNamespaces()
140 if (!m_cfg.nameSpace.
isEmpty()) {
142 const int namespaceCount = m_cfg.nameSpace.
count(QStringLiteral(
"::")) + 1;
143 for (
int i = 0; i < namespaceCount; ++i) {
152QString KConfigCodeGeneratorBase::memberAccessorBody(
const CfgEntry *e,
bool globalEnums)
const
158 bool useEnumType = m_cfg.useEnumTypes && t ==
QLatin1String(
"Enum");
162 out <<
"static_cast<" << enumType(e, globalEnums) <<
">(";
164 out << m_this << varPath(n, m_cfg);
176void KConfigCodeGeneratorBase::memberImmutableBody(
const CfgEntry *e,
bool globalEnums)
178 stream() << whitespace() <<
"return " << m_this <<
"isImmutable( QStringLiteral( \"";
182 stream() <<
"QLatin1String( ";
185 stream() << enumName(e->param) <<
"ToString[i]";
187 stream() << enumName(e->param) <<
"::enumToString[i]";
196 stream() << e->name <<
"\" )";
201void KConfigCodeGeneratorBase::createIfSetLogic(
const CfgEntry *e,
const QString &varExpression)
203 const bool hasBody = !e->signalList.
empty() || m_cfg.generateProperties;
205 m_stream << whitespace() <<
"if (";
207 m_stream <<
"v != " << varExpression <<
" && ";
210 const auto immutablefunction = immutableFunction(e->name, m_cfg.dpointer ? m_cfg.className :
QString{});
211 m_stream <<
"!" << m_this << immutablefunction <<
"(";
218void KConfigCodeGeneratorBase::memberMutatorBody(
const CfgEntry *e)
223 m_stream << whitespace() <<
"if (v < " << e->min <<
")\n";
224 m_stream << whitespace() <<
"{\n";
225 m_stream << whitespace();
226 addDebugMethod(m_stream, m_cfg, e->name);
227 m_stream <<
": value \" << v << \" is less than the minimum value of " << e->min <<
"\";\n";
228 m_stream << whitespace() <<
" v = " << e->min <<
";\n";
229 m_stream << whitespace() <<
"}\n";
235 m_stream << whitespace() <<
"if (v > " << e->max <<
")\n";
236 m_stream << whitespace() <<
"{\n";
237 m_stream << whitespace();
238 addDebugMethod(m_stream, m_cfg, e->name);
239 m_stream <<
": value \" << v << \" is greater than the maximum value of " << e->max <<
"\";\n";
240 m_stream << whitespace() <<
" v = " << e->max <<
";\n";
241 m_stream << whitespace() <<
"}\n\n";
244 const QString varExpression = m_this + varPath(e->name, m_cfg) + (e->param.
isEmpty() ?
QString{} : QStringLiteral(
"[i]"));
247 const bool hasBody = !e->signalList.
empty() || m_cfg.generateProperties;
250 createIfSetLogic(e, varExpression);
251 m_stream << (hasBody ?
" {" :
"") <<
'\n';
252 m_stream << whitespace() <<
" " << varExpression <<
" = v;\n";
254 const auto listSignal = e->signalList;
255 for (
const Signal &signal : std::as_const(listSignal)) {
257 m_stream << whitespace() <<
" Q_EMIT " << m_this << signal.name <<
"();\n";
259 m_stream << whitespace() <<
" " << m_this << varPath(QStringLiteral(
"settingsChanged"), m_cfg) <<
".insert(" << signalEnumName(signal.name) <<
");\n";
263 m_stream << whitespace() <<
"}\n";
Configuration Compiler Configuration.
virtual void close() override
QString fileName() const const
qsizetype count() const const
QString & append(QChar ch)
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const