From 875bb91cc542103d2f0ad777452d5ac8ed960471 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 23 Nov 2021 14:57:29 +0100 Subject: shiboken6: Add further methods to smart pointers Add attributes for reset() and null-check. The shared pointer in the test uses isNull() which works due to the --use-isnull-as-nb_nonzero option, but it should be possible to explicitly specify that. [ChangeLog][shiboken6] Attributes for reset() and null-check of smart pointers have been added. Task-number: PYSIDE-454 Change-Id: I22571eeb43f7f98b6a77b31066bf8daa681cb044 Reviewed-by: Christian Tismer (cherry picked from commit eef987d0faaf1122f191a6ad92343d98f197715d) Reviewed-by: Qt Cherry-pick Bot --- sources/shiboken6/ApiExtractor/messages.cpp | 6 +++++ sources/shiboken6/ApiExtractor/messages.h | 2 ++ sources/shiboken6/ApiExtractor/typesystem.cpp | 26 ++++++++++++++++++++++ sources/shiboken6/ApiExtractor/typesystem.h | 5 +++++ .../shiboken6/ApiExtractor/typesystemparser.cpp | 8 +++++++ .../shiboken6/doc/typesystem_specifying_types.rst | 8 +++++++ .../shiboken6/generator/shiboken/cppgenerator.cpp | 11 +++++++++ .../tests/smartbinding/typesystem_smart.xml | 1 + 8 files changed, 67 insertions(+) (limited to 'sources') diff --git a/sources/shiboken6/ApiExtractor/messages.cpp b/sources/shiboken6/ApiExtractor/messages.cpp index 605b99966..762630fad 100644 --- a/sources/shiboken6/ApiExtractor/messages.cpp +++ b/sources/shiboken6/ApiExtractor/messages.cpp @@ -615,6 +615,12 @@ QString msgCannotFindSmartPointer(const QString &instantiationType, return result; } +QString msgMethodNotFound(const AbstractMetaClass *klass, const QString &name) +{ + return u"Method \""_qs + name + u"\" not found in class "_qs + + klass->name() + u'.'; +} + // main.cpp QString msgLeftOverArguments(const QVariantMap &remainingArgs) diff --git a/sources/shiboken6/ApiExtractor/messages.h b/sources/shiboken6/ApiExtractor/messages.h index 686c0efd7..11da21c8e 100644 --- a/sources/shiboken6/ApiExtractor/messages.h +++ b/sources/shiboken6/ApiExtractor/messages.h @@ -191,6 +191,8 @@ QString msgConversionTypesDiffer(const QString &varType, const QString &conversi QString msgCannotFindSmartPointer(const QString &instantiationType, const AbstractMetaClassCList &pointers); +QString msgMethodNotFound(const AbstractMetaClass *klass, const QString &name); + QString msgLeftOverArguments(const QVariantMap &remainingArgs); QString msgInvalidVersion(const QString &package, const QString &version); diff --git a/sources/shiboken6/ApiExtractor/typesystem.cpp b/sources/shiboken6/ApiExtractor/typesystem.cpp index 6ad87a736..e88022e80 100644 --- a/sources/shiboken6/ApiExtractor/typesystem.cpp +++ b/sources/shiboken6/ApiExtractor/typesystem.cpp @@ -1759,6 +1759,8 @@ public: QString m_getterName; QString m_smartPointerType; QString m_refCountMethodName; + QString m_nullCheckMethod; + QString m_resetMethod; SmartPointerTypeEntry::Instantiations m_instantiations; }; @@ -1784,6 +1786,30 @@ QString SmartPointerTypeEntry::refCountMethodName() const return d->m_refCountMethodName; } +QString SmartPointerTypeEntry::nullCheckMethod() const +{ + S_D(const SmartPointerTypeEntry); + return d->m_nullCheckMethod; +} + +void SmartPointerTypeEntry::setNullCheckMethod(const QString &f) +{ + S_D(SmartPointerTypeEntry); + d->m_nullCheckMethod = f; +} + +QString SmartPointerTypeEntry::resetMethod() const +{ + S_D(const SmartPointerTypeEntry); + return d->m_resetMethod; +} + +void SmartPointerTypeEntry::setResetMethod(const QString &f) +{ + S_D(SmartPointerTypeEntry); + d->m_resetMethod = f; +} + TypeEntry *SmartPointerTypeEntry::clone() const { S_D(const SmartPointerTypeEntry); diff --git a/sources/shiboken6/ApiExtractor/typesystem.h b/sources/shiboken6/ApiExtractor/typesystem.h index fc77fc6ca..f80ff886d 100644 --- a/sources/shiboken6/ApiExtractor/typesystem.h +++ b/sources/shiboken6/ApiExtractor/typesystem.h @@ -718,6 +718,11 @@ public: QString refCountMethodName() const; + QString nullCheckMethod() const; + void setNullCheckMethod(const QString &); + QString resetMethod() const; + void setResetMethod(const QString &); + TypeEntry *clone() const override; Instantiations instantiations() const; diff --git a/sources/shiboken6/ApiExtractor/typesystemparser.cpp b/sources/shiboken6/ApiExtractor/typesystemparser.cpp index a3730ef29..efce5ae90 100644 --- a/sources/shiboken6/ApiExtractor/typesystemparser.cpp +++ b/sources/shiboken6/ApiExtractor/typesystemparser.cpp @@ -1276,6 +1276,8 @@ SmartPointerTypeEntry * QString smartPointerType; QString getter; QString refCountMethodName; + QString nullCheckMethod; + QString resetMethod; QString instantiations; for (int i = attributes->size() - 1; i >= 0; --i) { const auto name = attributes->at(i).qualifiedName(); @@ -1287,6 +1289,10 @@ SmartPointerTypeEntry * refCountMethodName = attributes->takeAt(i).value().toString(); } else if (name == QLatin1String("instantiations")) { instantiations = attributes->takeAt(i).value().toString(); + } else if (name == u"null-check-method") { + nullCheckMethod = attributes->takeAt(i).value().toString(); + } else if (name == u"reset-method") { + resetMethod = attributes->takeAt(i).value().toString(); } } @@ -1322,6 +1328,8 @@ SmartPointerTypeEntry * refCountMethodName, since, currentParentTypeEntry()); if (!applyCommonAttributes(reader, type, attributes)) return nullptr; + type->setNullCheckMethod(nullCheckMethod); + type->setResetMethod(resetMethod); m_smartPointerInstantiations.insert(type, instantiations); return type; } diff --git a/sources/shiboken6/doc/typesystem_specifying_types.rst b/sources/shiboken6/doc/typesystem_specifying_types.rst index 779ea3cf7..2f99a7407 100644 --- a/sources/shiboken6/doc/typesystem_specifying_types.rst +++ b/sources/shiboken6/doc/typesystem_specifying_types.rst @@ -567,9 +567,17 @@ smart-pointer-type type="..." getter="..." ref-count-method="..." + null-check-method="..." + reset-method="..." instantiations="..."/> + The *optional* attribute **null-check-method** specifies a method + that can be used to check for ``nullptr``. + + The *optional* attribute **reset-method** specifies a method + that can be used to clear the pointer. + .. _function: function diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index aacbd8240..7f8158128 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -304,6 +304,17 @@ QList AbstractMetaFunctionCPtr CppGenerator::boolCast(const AbstractMetaClass *metaClass) const { const auto *te = metaClass->typeEntry(); + if (te->isSmartPointer()) { + auto *ste = static_cast(te); + auto nullCheckMethod = ste->nullCheckMethod(); + if (!nullCheckMethod.isEmpty()) { + const auto func = metaClass->findFunction(nullCheckMethod); + if (func.isNull()) + throw Exception(msgMethodNotFound(metaClass, nullCheckMethod)); + return func; + } + } + auto mode = te->operatorBoolMode(); if (useOperatorBoolAsNbNonZero() ? mode != TypeSystem::BoolCast::Disabled : mode == TypeSystem::BoolCast::Enabled) { diff --git a/sources/shiboken6/tests/smartbinding/typesystem_smart.xml b/sources/shiboken6/tests/smartbinding/typesystem_smart.xml index 46cb08267..64600de6c 100644 --- a/sources/shiboken6/tests/smartbinding/typesystem_smart.xml +++ b/sources/shiboken6/tests/smartbinding/typesystem_smart.xml @@ -45,6 +45,7 @@ won't work. --> -- cgit v1.2.3