diff --git a/src/kconfig_compiler/CMakeLists.txt b/src/kconfig_compiler/CMakeLists.txt --- a/src/kconfig_compiler/CMakeLists.txt +++ b/src/kconfig_compiler/CMakeLists.txt @@ -6,6 +6,7 @@ KConfigHeaderGenerator.cpp KConfigSourceGenerator.cpp KConfigXmlParser.cpp + KConfigTypeInformation.cpp kconfig_compiler.cpp ) diff --git a/src/kconfig_compiler/KConfigCommonStructs.h b/src/kconfig_compiler/KConfigCommonStructs.h --- a/src/kconfig_compiler/KConfigCommonStructs.h +++ b/src/kconfig_compiler/KConfigCommonStructs.h @@ -126,8 +126,6 @@ // TODO: Move those methods QString enumName(const QString &n); QString enumName(const QString &n, const CfgEntry::Choices &c); -QString param(const QString &t); -QString cppType(const QString &t); QString itemType(const QString &type); QString changeSignalName(const QString &n); @@ -184,7 +182,6 @@ QString paramString(const QString &s, const CfgEntry *e, int i); QString paramString(const QString &group, const QList ¶meters); -QString defaultValue(const QString &t); QString memberGetDefaultBody(const CfgEntry *e); QString literalString(const QString &s); QString enumTypeQualifier(const QString &n, const CfgEntry::Choices &c); diff --git a/src/kconfig_compiler/KConfigHeaderGenerator.cpp b/src/kconfig_compiler/KConfigHeaderGenerator.cpp --- a/src/kconfig_compiler/KConfigHeaderGenerator.cpp +++ b/src/kconfig_compiler/KConfigHeaderGenerator.cpp @@ -25,6 +25,7 @@ #include "KConfigHeaderGenerator.h" #include "KConfigCommonStructs.h" +#include "KConfigTypeInformation.h" #include #include @@ -75,7 +76,7 @@ const QString returnType = (cfg().useEnumTypes && t == QLatin1String("Enum")) ? enumType(entry, cfg().globalEnums) - : cppType(t); + : KConfigTypeInformation(t).asType(); createSetters(entry); createProperties(entry, returnType); @@ -102,7 +103,7 @@ // Class Parameters for (const auto ¶meter : parseResult.parameters) { - stream() << whitespace() << "" << cppType(parameter.type) << " mParam" << parameter.name << ";\n"; + stream() << whitespace() << "" << KConfigTypeInformation(parameter.type).asType() << " mParam" << parameter.name << ";\n"; } createNonDPointerHelpers(); @@ -277,7 +278,7 @@ QList::ConstIterator it, itEnd = signal.arguments.constEnd(); for (it = signal.arguments.constBegin(); it != itEnd;) { Param argument = *it; - QString type = param(argument.type); + QString type = KConfigTypeInformation(argument.type).asParameter(); if (cfg().useEnumTypes && argument.type == QLatin1String("Enum")) { for (auto *entry : parseResult.entries) { if (entry->name == argument.name) { @@ -314,9 +315,9 @@ if (cfg().staticAccessors) { stream() << "static "; } - stream() << cppType(entry->type) << " " << getDefaultFunction(entry->name) << "_helper("; + stream() << KConfigTypeInformation(entry->type).asType() << " " << getDefaultFunction(entry->name) << "_helper("; if (!entry->param.isEmpty()) { - stream() << " " << cppType(entry->paramType) << " i "; + stream() << " " << KConfigTypeInformation(entry->paramType).asType() << " i "; } stream() << ")" << Const() << ";\n"; } @@ -351,7 +352,7 @@ stream() << ","; } - stream() << " " << param(parameter.type) << " " << parameter.name; + stream() << " " << KConfigTypeInformation(parameter.type).asParameter() << " " << parameter.name; } if (cfg().parentInConstructor) { @@ -418,12 +419,12 @@ stream() << whitespace() << "void " << setFunction(entry->name) << "( "; if (!entry->param.isEmpty()) { - stream() <paramType) << " i, "; + stream() << KConfigTypeInformation(entry->paramType).asType() << " i, "; } stream() << (cfg().useEnumTypes && entry->type == QLatin1String("Enum") ? enumType(entry, cfg().globalEnums) - : param(entry->type)); + : KConfigTypeInformation(entry->type).asParameter()); stream() << " v )"; @@ -453,7 +454,7 @@ stream() << returnType; stream() << " " << getFunction(entry->name) << "("; if (!entry->param.isEmpty()) { - stream() << " " << cppType(entry->paramType) << " i "; + stream() << " " << KConfigTypeInformation(entry->paramType).asType() << " i "; } stream() << ")" << Const(); @@ -483,7 +484,7 @@ stream() << whitespace() << "Item" << itemType(entry->type) << " *" << getFunction(entry->name) << "Item("; if (!entry->param.isEmpty()) { - stream() << " " << cppType(entry->paramType) << " i "; + stream() << " " << KConfigTypeInformation(entry->paramType).asType() << " i "; } stream() << ")"; if (!cfg().dpointer) { @@ -514,11 +515,11 @@ if (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) { stream() << enumType(entry, cfg().globalEnums); } else { - stream() << cppType(entry->type); + stream() << KConfigTypeInformation(entry->type).asType(); } stream() << " " << getDefaultFunction(entry->name) << "("; if (!entry->param.isEmpty()) { - stream() << " " << cppType(entry->paramType) << " i "; + stream() << " " << KConfigTypeInformation(entry->paramType).asType() << " i "; } stream() << ")" << Const() << '\n'; stream() << whitespace() << "{\n"; @@ -573,7 +574,7 @@ stream() << '\n'; stream() << whitespace() << "// " << group << '\n'; } - stream() << whitespace() << "" << cppType(entry->type) << " " << varName(entry->name, cfg()); + stream() << whitespace() << "" << KConfigTypeInformation(entry->type).asType() << " " << varName(entry->name, cfg()); if (!entry->param.isEmpty()) { stream() << QStringLiteral("[%1]").arg(entry->paramMax + 1); } @@ -584,9 +585,9 @@ if (cfg().staticAccessors) { stream() << "static "; } - stream() << cppType(entry->type) << " " << getDefaultFunction(entry->name) << "_helper("; + stream() << KConfigTypeInformation(entry->type).asType() << " " << getDefaultFunction(entry->name) << "_helper("; if (!entry->param.isEmpty()) { - stream() << " " << cppType(entry->paramType) << " i "; + stream() << " " << KConfigTypeInformation(entry->paramType).asType() << " i "; } stream() << ")" << Const() << ";\n"; } diff --git a/src/kconfig_compiler/KConfigSourceGenerator.cpp b/src/kconfig_compiler/KConfigSourceGenerator.cpp --- a/src/kconfig_compiler/KConfigSourceGenerator.cpp +++ b/src/kconfig_compiler/KConfigSourceGenerator.cpp @@ -25,7 +25,7 @@ #include "KConfigSourceGenerator.h" #include "KConfigCommonStructs.h" - +#include "KConfigTypeInformation.h" KConfigSourceGenerator::KConfigSourceGenerator( const QString &inputFile, @@ -114,7 +114,7 @@ stream() << '\n'; stream() << " // " << group << '\n'; } - stream() << " " << cppType(entry->type) << " " << varName(entry->name, cfg()); + stream() << " " << KConfigTypeInformation(entry->type).asType() << " " << varName(entry->name, cfg()); if (!entry->param.isEmpty()) { stream() << QStringLiteral("[%1]").arg(entry->paramMax + 1); } @@ -236,7 +236,7 @@ if (it != parseResult.parameters.constBegin()) { stream() << ","; } - stream() << " " << param((*it).type) << " " << (*it).name; + stream() << " " << KConfigTypeInformation((*it).type).asParameter() << " " << (*it).name; } if (cfg().parentInConstructor) { @@ -347,7 +347,7 @@ QString defaultStr = !entry->paramDefaultValues[i].isEmpty() ? entry->paramDefaultValues[i] : !entry->defaultValue.isEmpty() ? paramString(entry->defaultValue, entry, i) - : defaultValue(entry->type); + : KConfigTypeInformation(entry->type).defaultValue(); stream() << " " << itemVarStr << " = " << newItem(entry, paramString(key, entry, i), defaultStr, cfg(), QStringLiteral("[%1]").arg(i)) << '\n'; @@ -460,12 +460,12 @@ if (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) { stream() << enumType(entry, cfg().globalEnums); } else { - stream() << cppType(entry->type); + stream() << KConfigTypeInformation(entry->type).asType(); } stream() << " " << getFunction(entry->name, cfg().className) << "("; if (!entry->param.isEmpty()) { - stream() << " " << cppType(entry->paramType) << " i "; + stream() << " " << KConfigTypeInformation(entry->paramType).asType() << " i "; } stream() << ")" << Const() << '\n'; @@ -487,13 +487,13 @@ stream() << "void " << setFunction(entry->name, cfg().className) << "( "; if (!entry->param.isEmpty()) { - stream() << cppType(entry->paramType) << " i, "; + stream() << KConfigTypeInformation(entry->paramType).asType() << " i, "; } if (cfg().useEnumTypes && entry->type == QLatin1String("Enum")) { stream() << enumType(entry, cfg().globalEnums); } else { - stream() << param(entry->type); + stream() << KConfigTypeInformation(entry->type).asParameter(); } stream() << " v )\n"; @@ -515,7 +515,7 @@ stream() << cfg().inherits << "::Item" << itemType(entry->type) << " *" << getFunction(entry->name, cfg().className) << "Item("; if (!entry->param.isEmpty()) { - stream() << " " << cppType(entry->paramType) << " i "; + stream() << " " << KConfigTypeInformation(entry->paramType).asType() << " i "; } stream() << ")\n"; startScope(); @@ -547,9 +547,9 @@ // Default value Accessor, as "helper" function if ((cfg().allDefaultGetters || cfg().defaultGetters.contains(n)) && !entry->defaultValue.isEmpty()) { - stream() << cppType(t) << " " << getDefaultFunction(n, cfg().className) << "_helper("; + stream() << KConfigTypeInformation(t).asType() << " " << getDefaultFunction(n, cfg().className) << "_helper("; if (!entry->param.isEmpty()) { - stream() << " " << cppType(entry->paramType) << " i "; + stream() << " " << KConfigTypeInformation(entry->paramType).asType() << " i "; } stream() << ")" << Const() << '\n'; startScope(); diff --git a/src/kconfig_compiler/KConfigTypeInformation.h b/src/kconfig_compiler/KConfigTypeInformation.h new file mode 100644 --- /dev/null +++ b/src/kconfig_compiler/KConfigTypeInformation.h @@ -0,0 +1,41 @@ +#ifndef KCONFIG_TYPE_INFORMATION_H +#define KCONFIG_TYPE_INFORMATION_H + +/* This file is part of the KDE libraries + Copyright (C) 2020 Tomaz Cananbrava (tcanabrava@kde.org) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include +#include + +/* This class handles type manipulation and conversions */ +class KConfigTypeInformation +{ +public: + KConfigTypeInformation(const QString& xmlname); + + QString asParameter() const; + QString asType() const; + QString defaultValue() const; + +private: + static const QHash m_typeMap; + QString m_type; +}; + +#endif diff --git a/src/kconfig_compiler/KConfigTypeInformation.cpp b/src/kconfig_compiler/KConfigTypeInformation.cpp new file mode 100644 --- /dev/null +++ b/src/kconfig_compiler/KConfigTypeInformation.cpp @@ -0,0 +1,89 @@ +/* This file is part of the KDE libraries + Copyright (C) 2020 Tomaz Cananbrava (tcanabrava@kde.org) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "KConfigTypeInformation.h" + +const QHash KConfigTypeInformation::m_typeMap = { + // Qt Types + {QStringLiteral("string"), QStringLiteral("QString")}, + {QStringLiteral("path"), QStringLiteral("QString")}, + {QStringLiteral("password"), QStringLiteral("QString")}, + {QStringLiteral("font"), QStringLiteral("QFont")}, + {QStringLiteral("rect"), QStringLiteral("QRect")}, + {QStringLiteral("size"), QStringLiteral("QSize")}, + {QStringLiteral("color"), QStringLiteral("QColor")}, + {QStringLiteral("point"), QStringLiteral("QPoint")}, + {QStringLiteral("datetime"), QStringLiteral("QDateTime")}, + {QStringLiteral("url"), QStringLiteral("QUrl")}, + + // Qt Containers + {QStringLiteral("intlist"), QStringLiteral("QList")}, + {QStringLiteral("urllist"), QStringLiteral("QList")}, + {QStringLiteral("stringlist"), QStringLiteral("QStringList")}, + {QStringLiteral("pathlist"), QStringLiteral("QStringList")}, + + // Primitive + {QStringLiteral("int"), QStringLiteral("int")}, + {QStringLiteral("uint"), QStringLiteral("uint")}, + {QStringLiteral("bool"), QStringLiteral("bool")}, + {QStringLiteral("double"), QStringLiteral("double")}, + {QStringLiteral("longlong"), QStringLiteral("longlong")}, + {QStringLiteral("ulonglong"), QStringLiteral("ulonglong")}, + {QStringLiteral("enum"), QStringLiteral("int")}, +}; + +KConfigTypeInformation::KConfigTypeInformation(const QString& xmlname) +{ + m_type = m_typeMap.value(xmlname.toLower()); + //TODO: Enable assert for KF6 and remove the default to QString. + //Q_ASSERT(!m_type.isEmpty()); + if (m_type.isEmpty()) { + m_type = QStringLiteral("QString"); + } +} + +QString KConfigTypeInformation::asParameter() const +{ + // If it's a Qt structure, parameter is a const ref. + if (m_type.startsWith(QLatin1Char('Q'))) { + return QStringLiteral("const ") + m_type + QStringLiteral(" &"); + } + // It's a primitive, parameter is passed by value. + return m_type; +} + +QString KConfigTypeInformation::asType() const +{ + return m_type; +} + +QString KConfigTypeInformation::defaultValue() const +{ + if (m_type == QStringLiteral("QString")) { + return QStringLiteral("\"\""); + } + if (m_type == QStringLiteral("bool")) { + return QStringLiteral("false"); + } + if (m_type.startsWith(QLatin1Char('Q'))) { + return QStringLiteral("{}"); + } + // This is returning 0 and not 0.0 for doubles, Does it really matter? + return QStringLiteral("0"); +} diff --git a/src/kconfig_compiler/kconfig_compiler.cpp b/src/kconfig_compiler/kconfig_compiler.cpp --- a/src/kconfig_compiler/kconfig_compiler.cpp +++ b/src/kconfig_compiler/kconfig_compiler.cpp @@ -211,165 +211,6 @@ return false; } -/** - Return parameter declaration for given type. -*/ -QString param(const QString &t) -{ - const QString type = t.toLower(); - if (type == QLatin1String("string")) { - return QStringLiteral("const QString &"); - } else if (type == QLatin1String("stringlist")) { - return QStringLiteral("const QStringList &"); - } else if (type == QLatin1String("font")) { - return QStringLiteral("const QFont &"); - } else if (type == QLatin1String("rect")) { - return QStringLiteral("const QRect &"); - } else if (type == QLatin1String("size")) { - return QStringLiteral("const QSize &"); - } else if (type == QLatin1String("color")) { - return QStringLiteral("const QColor &"); - } else if (type == QLatin1String("point")) { - return QStringLiteral("const QPoint &"); - } else if (type == QLatin1String("int")) { - return QStringLiteral("int"); - } else if (type == QLatin1String("uint")) { - return QStringLiteral("uint"); - } else if (type == QLatin1String("bool")) { - return QStringLiteral("bool"); - } else if (type == QLatin1String("double")) { - return QStringLiteral("double"); - } else if (type == QLatin1String("datetime")) { - return QStringLiteral("const QDateTime &"); - } else if (type == QLatin1String("longlong")) { - return QStringLiteral("qint64"); - } else if (type == QLatin1String("ulonglong")) { - return QStringLiteral("quint64"); - } else if (type == QLatin1String("intlist")) { - return QStringLiteral("const QList &"); - } else if (type == QLatin1String("enum")) { - return QStringLiteral("int"); - } else if (type == QLatin1String("path")) { - return QStringLiteral("const QString &"); - } else if (type == QLatin1String("pathlist")) { - return QStringLiteral("const QStringList &"); - } else if (type == QLatin1String("password")) { - return QStringLiteral("const QString &"); - } else if (type == QLatin1String("url")) { - return QStringLiteral("const QUrl &"); - } else if (type == QLatin1String("urllist")) { - return QStringLiteral("const QList &"); - } else { - std::cerr << "kconfig_compiler_kf5 does not support type \"" << qPrintable(type) << "\"" << std::endl; - return QStringLiteral("QString"); //For now, but an assert would be better - } -} - -/** - Actual C++ storage type for given type. -*/ -QString cppType(const QString &t) -{ - const QString type = t.toLower(); - if (type == QLatin1String("string")) { - return QStringLiteral("QString"); - } else if (type == QLatin1String("stringlist")) { - return QStringLiteral("QStringList"); - } else if (type == QLatin1String("font")) { - return QStringLiteral("QFont"); - } else if (type == QLatin1String("rect")) { - return QStringLiteral("QRect"); - } else if (type == QLatin1String("size")) { - return QStringLiteral("QSize"); - } else if (type == QLatin1String("color")) { - return QStringLiteral("QColor"); - } else if (type == QLatin1String("point")) { - return QStringLiteral("QPoint"); - } else if (type == QLatin1String("int")) { - return QStringLiteral("int"); - } else if (type == QLatin1String("uint")) { - return QStringLiteral("uint"); - } else if (type == QLatin1String("bool")) { - return QStringLiteral("bool"); - } else if (type == QLatin1String("double")) { - return QStringLiteral("double"); - } else if (type == QLatin1String("datetime")) { - return QStringLiteral("QDateTime"); - } else if (type == QLatin1String("longlong")) { - return QStringLiteral("qint64"); - } else if (type == QLatin1String("ulonglong")) { - return QStringLiteral("quint64"); - } else if (type == QLatin1String("intlist")) { - return QStringLiteral("QList"); - } else if (type == QLatin1String("enum")) { - return QStringLiteral("int"); - } else if (type == QLatin1String("path")) { - return QStringLiteral("QString"); - } else if (type == QLatin1String("pathlist")) { - return QStringLiteral("QStringList"); - } else if (type == QLatin1String("password")) { - return QStringLiteral("QString"); - } else if (type == QLatin1String("url")) { - return QStringLiteral("QUrl"); - } else if (type == QLatin1String("urllist")) { - return QStringLiteral("QList"); - } else { - std::cerr << "kconfig_compiler_kf5 does not support type \"" << qPrintable(type) << "\"" << std::endl; - return QStringLiteral("QString"); //For now, but an assert would be better - } -} - -QString defaultValue(const QString &t) -{ - const QString type = t.toLower(); - if (type == QLatin1String("string")) { - return QStringLiteral("\"\""); // Use empty string, not null string! - } else if (type == QLatin1String("stringlist")) { - return QStringLiteral("QStringList()"); - } else if (type == QLatin1String("font")) { - return QStringLiteral("QFont()"); - } else if (type == QLatin1String("rect")) { - return QStringLiteral("QRect()"); - } else if (type == QLatin1String("size")) { - return QStringLiteral("QSize()"); - } else if (type == QLatin1String("color")) { - return QStringLiteral("QColor(128, 128, 128)"); - } else if (type == QLatin1String("point")) { - return QStringLiteral("QPoint()"); - } else if (type == QLatin1String("int")) { - return QStringLiteral("0"); - } else if (type == QLatin1String("uint")) { - return QStringLiteral("0"); - } else if (type == QLatin1String("bool")) { - return QStringLiteral("false"); - } else if (type == QLatin1String("double")) { - return QStringLiteral("0.0"); - } else if (type == QLatin1String("datetime")) { - return QStringLiteral("QDateTime()"); - } else if (type == QLatin1String("longlong")) { - return QStringLiteral("0"); - } else if (type == QLatin1String("ulonglong")) { - return QStringLiteral("0"); - } else if (type == QLatin1String("intlist")) { - return QStringLiteral("QList()"); - } else if (type == QLatin1String("enum")) { - return QStringLiteral("0"); - } else if (type == QLatin1String("path")) { - return QStringLiteral("\"\""); // Use empty string, not null string! - } else if (type == QLatin1String("pathlist")) { - return QStringLiteral("QStringList()"); - } else if (type == QLatin1String("password")) { - return QStringLiteral("\"\""); // Use empty string, not null string! - } else if (type == QLatin1String("url")) { - return QStringLiteral("QUrl()"); - } else if (type == QLatin1String("urllist")) { - return QStringLiteral("QList()"); - } else { - std::cerr << "Error, kconfig_compiler_kf5 does not support the \"" << qPrintable(type) << "\" type!" << std::endl; - return QStringLiteral("QString"); //For now, but an assert would be better - } -} - QString itemType(const QString &type) { QString t;