diff options
-rw-r--r-- | sources/shiboken6/ApiExtractor/messages.cpp | 6 | ||||
-rw-r--r-- | sources/shiboken6/ApiExtractor/messages.h | 2 | ||||
-rw-r--r-- | sources/shiboken6/ApiExtractor/typesystem.cpp | 26 | ||||
-rw-r--r-- | sources/shiboken6/ApiExtractor/typesystem.h | 5 | ||||
-rw-r--r-- | sources/shiboken6/ApiExtractor/typesystemparser.cpp | 8 | ||||
-rw-r--r-- | sources/shiboken6/doc/typesystem_specifying_types.rst | 8 | ||||
-rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.cpp | 11 | ||||
-rw-r--r-- | sources/shiboken6/tests/smartbinding/typesystem_smart.xml | 1 |
8 files changed, 67 insertions, 0 deletions
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="..."/> </typesystem> + 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<AbstractMetaFunctionCList> AbstractMetaFunctionCPtr CppGenerator::boolCast(const AbstractMetaClass *metaClass) const { const auto *te = metaClass->typeEntry(); + if (te->isSmartPointer()) { + auto *ste = static_cast<const SmartPointerTypeEntry *>(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. --> <smart-pointer-type name="SharedPtr" type="shared" getter="data" ref-count-method="useCount" + null-check-method="isNull" instantiations="Integer,Smart::Integer2,Obj"/> <object-type name="Obj" /> |