diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-13 14:53:36 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-14 12:07:59 +0200 |
commit | 871a7e2ea74e093d862f954b7ddca374c02cd5b8 (patch) | |
tree | 086a42937d751de3ceb1e3eee9ba8a11edd9cbbb | |
parent | 805cc07cb18c972eceaea7dc3d48f840f4795243 (diff) |
shiboken2: Handle default parameters of const pointers
Occurs in Qt 6:
QKeyEvent(..., const QInputDevice *device = QInputDevice::primaryKeyboard());
We need a const-cast here since shiboken needs a QInputDevice * for type
conversion.
Change-Id: Iea1137eac44a26b7bc9cf0e1908c0e42ba2de39f
Reviewed-by: Christian Tismer <tismer@stackless.com>
7 files changed, 38 insertions, 6 deletions
diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index b071ae181..65e12c7bc 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -2424,8 +2424,17 @@ void CppGenerator::writePythonToCppTypeConversion(QTextStream &s, s << ' ' << cppOut; } else if (treatAsPointer || isPointerOrObjectType) { s << " *" << cppOut; - if (!defaultValue.isEmpty()) - s << " = " << defaultValue; + if (!defaultValue.isEmpty()) { + const bool needsConstCast = !isNullPtr(defaultValue) + && type->indirections() == 1 && type->isConstant() + && type->referenceType() == NoReference; + s << " = "; + if (needsConstCast) + s << "const_cast<" << typeName << " *>("; + s << defaultValue; + if (needsConstCast) + s << ')'; + } } else if (type->referenceType() == LValueReference && !typeEntry->isPrimitive() && isNotContainerEnumOrFlags) { s << " *" << cppOut << " = &" << cppOutAux; } else { diff --git a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp index 6abaef698..43ebefe14 100644 --- a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp @@ -1145,6 +1145,12 @@ bool ShibokenGenerator::visibilityModifiedToPrivate(const AbstractMetaFunction * return false; } +bool ShibokenGenerator::isNullPtr(const QString &value) +{ + return value == QLatin1String("0") || value == QLatin1String("nullptr") + || value == QLatin1String("NULLPTR") || value == QLatin1String("{}"); +} + QString ShibokenGenerator::cpythonCheckFunction(const AbstractMetaType *metaType, bool genericNumberType) { QString customCheck; diff --git a/sources/shiboken2/generator/shiboken2/shibokengenerator.h b/sources/shiboken2/generator/shiboken2/shibokengenerator.h index d8259d245..da0c16851 100644 --- a/sources/shiboken2/generator/shiboken2/shibokengenerator.h +++ b/sources/shiboken2/generator/shiboken2/shibokengenerator.h @@ -301,6 +301,8 @@ protected: static bool visibilityModifiedToPrivate(const AbstractMetaFunction *func); + static bool isNullPtr(const QString &value); + QString converterObject(const AbstractMetaType *type); QString converterObject(const TypeEntry *type); diff --git a/sources/shiboken2/tests/libsample/objecttype.cpp b/sources/shiboken2/tests/libsample/objecttype.cpp index 855c08611..afaaa9d77 100644 --- a/sources/shiboken2/tests/libsample/objecttype.cpp +++ b/sources/shiboken2/tests/libsample/objecttype.cpp @@ -57,6 +57,12 @@ ObjectType::createWithChild() return parent; } +const ObjectType *ObjectType::defaultInstance() +{ + static ObjectType result; + return &result; +} + void ObjectType::removeChild(ObjectType* child) { diff --git a/sources/shiboken2/tests/libsample/objecttype.h b/sources/shiboken2/tests/libsample/objecttype.h index 1f2a9c7e8..caa50f02e 100644 --- a/sources/shiboken2/tests/libsample/objecttype.h +++ b/sources/shiboken2/tests/libsample/objecttype.h @@ -78,6 +78,8 @@ public: inline static ObjectType* create() { return new ObjectType(); } static ObjectType* createWithChild(); + static const ObjectType *defaultInstance(); + void setParent(ObjectType* parent); inline ObjectType* parent() const { return m_parent; } inline const ObjectTypeList& children() const { return m_children; } diff --git a/sources/shiboken2/tests/libsample/objecttypeholder.cpp b/sources/shiboken2/tests/libsample/objecttypeholder.cpp index ff2f14f12..be225a0d2 100644 --- a/sources/shiboken2/tests/libsample/objecttypeholder.cpp +++ b/sources/shiboken2/tests/libsample/objecttypeholder.cpp @@ -30,8 +30,14 @@ ObjectTypeHolder::ObjectTypeHolder(const char* objectName) { - m_objectType = new ObjectType(); - m_objectType->setObjectName(objectName); + auto object = new ObjectType(); + object->setObjectName(objectName); + m_objectType = object; +} + +ObjectTypeHolder::ObjectTypeHolder(const ObjectType *object) : + m_objectType(object) +{ } ObjectTypeHolder::~ObjectTypeHolder() diff --git a/sources/shiboken2/tests/libsample/objecttypeholder.h b/sources/shiboken2/tests/libsample/objecttypeholder.h index ce13de74f..7558b11ee 100644 --- a/sources/shiboken2/tests/libsample/objecttypeholder.h +++ b/sources/shiboken2/tests/libsample/objecttypeholder.h @@ -37,15 +37,16 @@ class LIBSAMPLE_API ObjectTypeHolder { public: explicit ObjectTypeHolder(const char* objectName); + explicit ObjectTypeHolder(const ObjectType *object = ObjectType::defaultInstance()); virtual ~ObjectTypeHolder(); - ObjectType* getObjecType() { return m_objectType; } + const ObjectType* getObjecType() { return m_objectType; } virtual Str passObjectTypeAsReference(const ObjectType& objectType); Str callPassObjectTypeAsReference(); private: - ObjectType* m_objectType; + const ObjectType *m_objectType; }; #endif |