aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2021-11-23 14:57:29 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2021-11-25 08:55:26 +0100
commit875bb91cc542103d2f0ad777452d5ac8ed960471 (patch)
tree1f21161abbc253824cb7a5b9032f0efaa2518153
parent3c24f0a856a166b4672921c4e1c2347ca397fdac (diff)
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 <tismer@stackless.com> (cherry picked from commit eef987d0faaf1122f191a6ad92343d98f197715d) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--sources/shiboken6/ApiExtractor/messages.cpp6
-rw-r--r--sources/shiboken6/ApiExtractor/messages.h2
-rw-r--r--sources/shiboken6/ApiExtractor/typesystem.cpp26
-rw-r--r--sources/shiboken6/ApiExtractor/typesystem.h5
-rw-r--r--sources/shiboken6/ApiExtractor/typesystemparser.cpp8
-rw-r--r--sources/shiboken6/doc/typesystem_specifying_types.rst8
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.cpp11
-rw-r--r--sources/shiboken6/tests/smartbinding/typesystem_smart.xml1
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" />