From 28ddfd304cbb1b7fb7d7d062b88b05f8713fc0c5 Mon Sep 17 00:00:00 2001 From: Miklos Marton Date: Thu, 27 Mar 2025 10:46:19 +0100 Subject: [PATCH] Add methods to being able to generate typedef-ed enums --- code_generation/enum.cpp | 17 +++++++++++++++-- code_generation/enum.h | 8 ++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/code_generation/enum.cpp b/code_generation/enum.cpp index 934a4c9e..4dfdb2b2 100644 --- a/code_generation/enum.cpp +++ b/code_generation/enum.cpp @@ -32,6 +32,7 @@ class Enum::Private QString mName; QStringList mEnums; bool mCombinable = false; + bool mTypedef = false; bool mIsQENUM = false; }; @@ -71,7 +72,10 @@ QString Enum::name() const void Enum::printDeclaration(Code &code) const { - code.addLine(QStringLiteral("enum %1 {").arg(d->mName)); + if (d->mTypedef) + code.addLine(QStringLiteral("typedef enum {")); + else + code.addLine(QStringLiteral("enum %1 {").arg(d->mName)); code.indent(); auto last = (d->mEnums.count() - 1); for (int value = 0; value <= last; value++) { @@ -88,7 +92,11 @@ void Enum::printDeclaration(Code &code) const } } code.unindent(); - code.addLine("};"); + if (d->mTypedef) + code.addLine(QString("} %1;").arg(d->mName)); + else + code.addLine("};"); + if (d->mIsQENUM) code.addLine(QStringLiteral("Q_ENUM(%1)").arg(d->mName)); code.newLine(); @@ -98,3 +106,8 @@ void Enum::setIsQENUM(bool qenum) { d->mIsQENUM = qenum; } + +void Enum::setTypedef(bool typeDef) +{ + d->mTypedef = typeDef; +} diff --git a/code_generation/enum.h b/code_generation/enum.h index 1b2ed373..84a80c78 100644 --- a/code_generation/enum.h +++ b/code_generation/enum.h @@ -84,6 +84,14 @@ class KODE_EXPORT Enum */ void setIsQENUM(bool qenum = true); + /** + * @brief setTypedef + * This method can be used to generate typedef for the given enum. + * Using typedefs is mostly useful for C code and not recommended in C++ code. + * @param typeDef + */ + void setTypedef(bool typeDef = true); + private: class Private; Private *d;