qdbusxml2cpp: Move shared state to a class

For better OOP code structure and to get overview
over functions that need this shared state.

Task-number: QTBUG-2597
Change-Id: Ib3a26a1116362c3c798052b6bce8db2062451bdb
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ievgenii Meshcheriakov 2023-06-29 15:43:15 +02:00
parent bcc93850fc
commit 2f82ba66e2

View File

@ -27,18 +27,45 @@
using namespace Qt::StringLiterals;
static QString globalClassName;
static QString parentClassName;
static QString proxyFile;
static QString adaptorFile;
static QString inputFile;
static bool skipNamespaces;
static bool verbose;
static bool includeMocs;
static QString commandLine;
static QStringList includes;
static QStringList globalIncludes;
static QStringList wantedInterfaces;
class QDBusXmlToCpp final
{
public:
int run(const QCoreApplication &app);
private:
enum ClassType { Proxy, Adaptor };
void writeAdaptor(const QString &filename, const QDBusIntrospection::Interfaces &interfaces);
void writeProxy(const QString &filename, const QDBusIntrospection::Interfaces &interfaces);
QDBusIntrospection::Interfaces readInput();
void cleanInterfaces(QDBusIntrospection::Interfaces &interfaces);
QTextStream &writeHeader(QTextStream &ts, bool changesWillBeLost);
QString classNameForInterface(const QString &interface, ClassType classType);
QByteArray qtTypeName(const QString &where, const QString &signature,
const QDBusIntrospection::Annotations &annotations,
qsizetype paramId = -1, const char *direction = "Out");
void
writeArgList(QTextStream &ts, const QStringList &argNames,
const QDBusIntrospection::Annotations &annotations,
const QDBusIntrospection::Arguments &inputArgs,
const QDBusIntrospection::Arguments &outputArgs = QDBusIntrospection::Arguments());
void writeSignalArgList(QTextStream &ts, const QStringList &argNames,
const QDBusIntrospection::Annotations &annotations,
const QDBusIntrospection::Arguments &outputArgs);
QString propertyGetter(const QDBusIntrospection::Property &property);
QString propertySetter(const QDBusIntrospection::Property &property);
QString globalClassName;
QString parentClassName;
QString inputFile;
bool skipNamespaces = false;
bool includeMocs = false;
QString commandLine;
QStringList includes;
QStringList globalIncludes;
QStringList wantedInterfaces;
};
static const char includeList[] =
"#include <QtCore/QByteArray>\n"
@ -51,7 +78,7 @@ static const char includeList[] =
static const char forwardDeclarations[] =
"#include <QtCore/qcontainerfwd.h>\n";
static QDBusIntrospection::Interfaces readInput()
QDBusIntrospection::Interfaces QDBusXmlToCpp::readInput()
{
QFile input(inputFile);
if (inputFile.isEmpty() || inputFile == "-"_L1)
@ -73,7 +100,7 @@ static QDBusIntrospection::Interfaces readInput()
exit(1);
}
static void cleanInterfaces(QDBusIntrospection::Interfaces &interfaces)
void QDBusXmlToCpp::cleanInterfaces(QDBusIntrospection::Interfaces &interfaces)
{
if (!wantedInterfaces.isEmpty()) {
QDBusIntrospection::Interfaces::Iterator it = interfaces.begin();
@ -179,7 +206,7 @@ static QString moc(const QString &name)
return retval;
}
static QTextStream &writeHeader(QTextStream &ts, bool changesWillBeLost)
QTextStream &QDBusXmlToCpp::writeHeader(QTextStream &ts, bool changesWillBeLost)
{
ts << "/*\n"
" * This file was generated by " PROGRAMNAME " version " PROGRAMVERSION "\n"
@ -200,8 +227,8 @@ static QTextStream &writeHeader(QTextStream &ts, bool changesWillBeLost)
return ts;
}
enum ClassType { Proxy, Adaptor };
static QString classNameForInterface(const QString &interface, ClassType classType)
QString QDBusXmlToCpp::classNameForInterface(const QString &interface,
QDBusXmlToCpp::ClassType classType)
{
if (!globalClassName.isEmpty())
return globalClassName;
@ -226,9 +253,9 @@ static QString classNameForInterface(const QString &interface, ClassType classTy
return retval;
}
static QByteArray qtTypeName(const QString &where, const QString &signature,
const QDBusIntrospection::Annotations &annotations, qsizetype paramId = -1,
const char *direction = "Out")
QByteArray QDBusXmlToCpp::qtTypeName(const QString &where, const QString &signature,
const QDBusIntrospection::Annotations &annotations,
qsizetype paramId, const char *direction)
{
int type = QDBusMetaType::signatureToMetaType(signature.toLatin1()).id();
if (type == QMetaType::UnknownType) {
@ -321,10 +348,10 @@ static QStringList makeArgNames(const QDBusIntrospection::Arguments &inputArgs,
return retval;
}
static void writeArgList(QTextStream &ts, const QStringList &argNames,
const QDBusIntrospection::Annotations &annotations,
const QDBusIntrospection::Arguments &inputArgs,
const QDBusIntrospection::Arguments &outputArgs = QDBusIntrospection::Arguments())
void QDBusXmlToCpp::writeArgList(QTextStream &ts, const QStringList &argNames,
const QDBusIntrospection::Annotations &annotations,
const QDBusIntrospection::Arguments &inputArgs,
const QDBusIntrospection::Arguments &outputArgs)
{
// input args:
bool first = true;
@ -354,9 +381,9 @@ static void writeArgList(QTextStream &ts, const QStringList &argNames,
}
}
static void writeSignalArgList(QTextStream &ts, const QStringList &argNames,
const QDBusIntrospection::Annotations &annotations,
const QDBusIntrospection::Arguments &outputArgs)
void QDBusXmlToCpp::writeSignalArgList(QTextStream &ts, const QStringList &argNames,
const QDBusIntrospection::Annotations &annotations,
const QDBusIntrospection::Arguments &outputArgs)
{
bool first = true;
qsizetype argPos = 0;
@ -372,7 +399,7 @@ static void writeSignalArgList(QTextStream &ts, const QStringList &argNames,
}
}
static QString propertyGetter(const QDBusIntrospection::Property &property)
QString QDBusXmlToCpp::propertyGetter(const QDBusIntrospection::Property &property)
{
auto annotation = property.annotations.value("org.qtproject.QtDBus.PropertyGetter"_L1);
if (!annotation.value.isEmpty())
@ -392,7 +419,7 @@ static QString propertyGetter(const QDBusIntrospection::Property &property)
return getter;
}
static QString propertySetter(const QDBusIntrospection::Property &property)
QString QDBusXmlToCpp::propertySetter(const QDBusIntrospection::Property &property)
{
auto annotation = property.annotations.value("org.qtproject.QtDBus.PropertySetter"_L1);
if (!annotation.value.isEmpty())
@ -458,7 +485,8 @@ static bool openFile(const QString &fileName, QFile &file)
return isOk;
}
static void writeProxy(const QString &filename, const QDBusIntrospection::Interfaces &interfaces)
void QDBusXmlToCpp::writeProxy(const QString &filename,
const QDBusIntrospection::Interfaces &interfaces)
{
// open the file
QString headerName = header(filename);
@ -769,7 +797,8 @@ static void writeProxy(const QString &filename, const QDBusIntrospection::Interf
}
}
static void writeAdaptor(const QString &filename, const QDBusIntrospection::Interfaces &interfaces)
void QDBusXmlToCpp::writeAdaptor(const QString &filename,
const QDBusIntrospection::Interfaces &interfaces)
{
// open the file
QString headerName = header(filename);
@ -1061,12 +1090,8 @@ static void writeAdaptor(const QString &filename, const QDBusIntrospection::Inte
}
}
int main(int argc, char **argv)
int QDBusXmlToCpp::run(const QCoreApplication &app)
{
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationName(QStringLiteral(PROGRAMNAME));
QCoreApplication::setApplicationVersion(QStringLiteral(PROGRAMVERSION));
QCommandLineParser parser;
parser.setApplicationDescription(
"Produces the C++ code to implement the interfaces defined in the input file.\n\n"
@ -1123,15 +1148,15 @@ int main(int argc, char **argv)
parser.process(app);
adaptorFile = parser.value(adapterCodeOption);
QString adaptorFile = parser.value(adapterCodeOption);
globalClassName = parser.value(classNameOption);
includes = parser.values(addIncludeOption);
globalIncludes = parser.values(addGlobalIncludeOption);
parentClassName = parser.value(adapterParentOption);
includeMocs = parser.isSet(mocIncludeOption);
skipNamespaces = parser.isSet(noNamespaceOption);
proxyFile = parser.value(proxyCodeOption);
verbose = parser.isSet(verboseOption);
QString proxyFile = parser.value(proxyCodeOption);
bool verbose = parser.isSet(verboseOption);
wantedInterfaces = parser.positionalArguments();
if (!wantedInterfaces.isEmpty()) {
@ -1168,3 +1193,11 @@ int main(int argc, char **argv)
return 0;
}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationName(QStringLiteral(PROGRAMNAME));
QCoreApplication::setApplicationVersion(QStringLiteral(PROGRAMVERSION));
return QDBusXmlToCpp().run(app);
}