aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2/generator
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2018-02-28 10:30:27 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2018-02-28 14:01:37 +0000
commit44cb6c51e6c3b43376f284941454dc8c13b81c3f (patch)
tree7e9fda1ba3788a3b9ed051e853b08926d325df6f /sources/shiboken2/generator
parent7c83f2ebc1ac55c6e2556c8c852716502dcdba5a (diff)
Type system: Add attribute indicating C++ 11 enum classes
Introduce enumeration for the type to EnumTypeEntry which can be specified by the boolean "class" attribute. For the enum classes, the value names need to be qualified by the enum name to match the C++ API. For the C++ generator, add an overload to Shiboken::createScopedEnumItem() that takes a PyTypeObject and add the enum items to the enum so that the name is in the enum scope. Change-Id: Ia0e469d13b08c196b9ddd965b9cf3cc62a38630b Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'sources/shiboken2/generator')
-rw-r--r--sources/shiboken2/generator/shiboken2/cppgenerator.cpp29
1 files changed, 23 insertions, 6 deletions
diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
index 7a688d1da..be42adb0f 100644
--- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
+++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
@@ -4471,6 +4471,7 @@ void CppGenerator::writeEnumInitialization(QTextStream& s, const AbstractMetaEnu
const AbstractMetaClass* enclosingClass = getProperEnclosingClassForEnum(cppEnum);
const AbstractMetaClass* upper = enclosingClass ? enclosingClass->enclosingClass() : 0;
bool hasUpperEnclosingClass = upper && upper->typeEntry()->codeGeneration() != TypeEntry::GenerateForSubclass;
+ const EnumTypeEntry *enumTypeEntry = cppEnum->typeEntry();
QString enclosingObjectVariable;
if (enclosingClass)
enclosingObjectVariable = QLatin1Char('&') + cpythonTypeName(enclosingClass);
@@ -4483,14 +4484,17 @@ void CppGenerator::writeEnumInitialization(QTextStream& s, const AbstractMetaEnu
s << (cppEnum->isAnonymous() ? "anonymous enum identified by enum value" : "enum");
s << " '" << cppEnum->name() << "'." << endl;
+ QString enumVarTypeObj;
if (!cppEnum->isAnonymous()) {
- FlagsTypeEntry* flags = cppEnum->typeEntry()->flags();
+ FlagsTypeEntry* flags = enumTypeEntry->flags();
if (flags) {
s << INDENT << cpythonTypeNameExt(flags) << " = PySide::QFlags::create(\"" << flags->flagsName() << "\", &"
<< cpythonEnumName(cppEnum) << "_as_number);" << endl;
}
- s << INDENT << cpythonTypeNameExt(cppEnum->typeEntry()) << " = Shiboken::Enum::";
+ enumVarTypeObj = cpythonTypeNameExt(enumTypeEntry);
+
+ s << INDENT << enumVarTypeObj << " = Shiboken::Enum::";
s << ((enclosingClass || hasUpperEnclosingClass) ? "createScopedEnum" : "createGlobalEnum");
s << '(' << enclosingObjectVariable << ',' << endl;
{
@@ -4512,7 +4516,7 @@ void CppGenerator::writeEnumInitialization(QTextStream& s, const AbstractMetaEnu
const AbstractMetaEnumValueList &enumValues = cppEnum->values();
for (const AbstractMetaEnumValue *enumValue : enumValues) {
- if (cppEnum->typeEntry()->isEnumValueRejected(enumValue->name()))
+ if (enumTypeEntry->isEnumValueRejected(enumValue->name()))
continue;
QString enumValueText;
@@ -4528,7 +4532,8 @@ void CppGenerator::writeEnumInitialization(QTextStream& s, const AbstractMetaEnu
enumValueText += QString::number(enumValue->value());
}
- if (cppEnum->isAnonymous()) {
+ switch (enumTypeEntry->enumKind()) {
+ case EnumTypeEntry::AnonymousEnum:
if (enclosingClass || hasUpperEnclosingClass) {
s << INDENT << '{' << endl;
{
@@ -4551,15 +4556,27 @@ void CppGenerator::writeEnumInitialization(QTextStream& s, const AbstractMetaEnu
s << INDENT << "return " << m_currentErrorCode << ';' << endl;
}
}
- } else {
+ break;
+ case EnumTypeEntry::CEnum: {
s << INDENT << "if (!Shiboken::Enum::";
s << ((enclosingClass || hasUpperEnclosingClass) ? "createScopedEnumItem" : "createGlobalEnumItem");
- s << '(' << cpythonTypeNameExt(cppEnum->typeEntry()) << ',' << endl;
+ s << '(' << enumVarTypeObj << ',' << endl;
Indentation indent(INDENT);
s << INDENT << enclosingObjectVariable << ", \"" << enumValue->name() << "\", ";
s << enumValueText << "))" << endl;
s << INDENT << "return " << m_currentErrorCode << ';' << endl;
}
+ break;
+ case EnumTypeEntry::EnumClass: {
+ s << INDENT << "if (!Shiboken::Enum::createScopedEnumItem("
+ << enumVarTypeObj << ',' << endl;
+ Indentation indent(INDENT);
+ s << INDENT << enumVarTypeObj<< ", \"" << enumValue->name() << "\", "
+ << enumValueText << "))" << endl
+ << INDENT << "return " << m_currentErrorCode << ';' << endl;
+ }
+ break;
+ }
}
writeEnumConverterInitialization(s, cppEnum);