From 0e7212460ba7fab19a47f960b09a011973a7c475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 14 Jul 2021 00:50:29 +0200 Subject: [PATCH] Use member function instead of template function to resolve native interface The use of a freestanding function is not needed now that the name doesn't alias the nativeInterface accessor function, and was just adding complexity to the machinery. People not familiar with the code will have an easier time following the flow through the helper member function, and we no longer need to declare our own export macros. Pick-to: 6.2 Change-Id: I17530b7e89939cfc19ab8ffaa076b7129ae02dcf Reviewed-by: Fabian Kosmale --- src/corelib/global/qnativeinterface.h | 52 ++++++++----------- src/corelib/kernel/qcoreapplication.cpp | 5 +- src/gui/kernel/qguiapplication.cpp | 5 +- src/gui/kernel/qkeymapper.cpp | 5 +- src/gui/kernel/qoffscreensurface.cpp | 11 ++-- src/gui/kernel/qopenglcontext.cpp | 7 ++- src/gui/kernel/qscreen.cpp | 6 +-- src/gui/kernel/qwindow.cpp | 5 +- src/widgets/kernel/qapplication.cpp | 5 +- .../qnativeinterface/tst_qnativeinterface.cpp | 18 ++++--- 10 files changed, 53 insertions(+), 66 deletions(-) diff --git a/src/corelib/global/qnativeinterface.h b/src/corelib/global/qnativeinterface.h index 395a4ba1da3..5e5c78beece 100644 --- a/src/corelib/global/qnativeinterface.h +++ b/src/corelib/global/qnativeinterface.h @@ -43,14 +43,6 @@ #include #include -#ifndef QT_STATIC -# define Q_NATIVE_INTERFACE_EXPORT Q_DECL_EXPORT -# define Q_NATIVE_INTERFACE_IMPORT Q_DECL_IMPORT -#else -# define Q_NATIVE_INTERFACE_EXPORT -# define Q_NATIVE_INTERFACE_IMPORT -#endif - QT_BEGIN_NAMESPACE // We declare a virtual non-inline function in the form @@ -166,9 +158,6 @@ namespace QNativeInterface::Private { template struct NativeInterface : TypeInfo {}; - template - Q_NATIVE_INTERFACE_IMPORT void *resolveInterface(const T *that, const char *name, int revision); - Q_CORE_EXPORT Q_DECLARE_LOGGING_CATEGORY(lcNativeInterface) } // QNativeInterface::Private @@ -179,10 +168,12 @@ namespace QNativeInterface::Private { typename BaseType = T, std::enable_if_t, bool> = true> \ NativeInterface *nativeInterface() const \ { \ - return static_cast( \ - QNativeInterface::Private::resolveInterface(this, \ - TypeInfo::name(), TypeInfo::revision())); \ - } + return static_cast(resolveInterface( \ + TypeInfo::name(), TypeInfo::revision())); \ + } \ + protected: \ + void *resolveInterface(const char *name, int revision) const; \ + public: // Provides a definition for the interface destructor #define QT_DEFINE_NATIVE_INTERFACE_2(Namespace, InterfaceClass) \ @@ -192,22 +183,25 @@ namespace QNativeInterface::Private { #define QT_DEFINE_PRIVATE_NATIVE_INTERFACE(...) QT_OVERLOADED_MACRO(QT_DEFINE_NATIVE_INTERFACE, QNativeInterface::Private, __VA_ARGS__) #define QT_NATIVE_INTERFACE_RETURN_IF(NativeInterface, baseType) \ - using QNativeInterface::Private::lcNativeInterface; \ - qCDebug(lcNativeInterface, "Comparing requested interface name %s with available %s", \ - name, TypeInfo::name()); \ - if (qstrcmp(name, TypeInfo::name()) == 0) { \ - qCDebug(lcNativeInterface, "Match for interface %s. Comparing revisions (requested %d / available %d)", \ - name, revision, TypeInfo::revision()); \ - if (revision == TypeInfo::revision()) { \ - qCDebug(lcNativeInterface) << "Full match. Returning dynamic cast of" << baseType; \ - return dynamic_cast(baseType); \ + { \ + using QNativeInterface::Private::lcNativeInterface; \ + using QNativeInterface::Private::TypeInfo; \ + qCDebug(lcNativeInterface, "Comparing requested interface name %s with available %s", \ + name, TypeInfo::name()); \ + if (qstrcmp(name, TypeInfo::name()) == 0) { \ + qCDebug(lcNativeInterface, "Match for interface %s. Comparing revisions (requested %d / available %d)", \ + name, revision, TypeInfo::revision()); \ + if (revision == TypeInfo::revision()) { \ + qCDebug(lcNativeInterface) << "Full match. Returning dynamic cast of" << baseType; \ + return dynamic_cast(baseType); \ + } else { \ + qCWarning(lcNativeInterface, "Native interface revision mismatch (requested %d / available %d) for interface %s", \ + revision, TypeInfo::revision(), name); \ + return nullptr; \ + } \ } else { \ - qCWarning(lcNativeInterface, "Native interface revision mismatch (requested %d / available %d) for interface %s", \ - revision, TypeInfo::revision(), name); \ - return nullptr; \ + qCDebug(lcNativeInterface, "No match for requested interface name %s", name); \ } \ - } else { \ - qCDebug(lcNativeInterface, "No match for requested interface name %s", name); \ } QT_END_NAMESPACE diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 81ea3dc5bee..8f53790cb79 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -3246,10 +3246,9 @@ QCoreApplication::checkPermission(const QString &permission) } #endif // future && QT_NO_QOBJECT -template <> -Q_NATIVE_INTERFACE_EXPORT void *QNativeInterface::Private::resolveInterface(const QCoreApplication *that, const char *name, int revision) +void *QCoreApplication::resolveInterface(const char *name, int revision) const { - Q_UNUSED(that); Q_UNUSED(name); Q_UNUSED(revision); + Q_UNUSED(name); Q_UNUSED(revision); return nullptr; } diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index f41c37c771e..c93e4834388 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -4203,8 +4203,7 @@ QInputDeviceManager *QGuiApplicationPrivate::inputDeviceManager() return m_inputDeviceManager; } -template <> -Q_NATIVE_INTERFACE_EXPORT void *QNativeInterface::Private::resolveInterface(const QGuiApplication *that, const char *name, int revision) +void *QGuiApplication::resolveInterface(const char *name, int revision) const { using namespace QNativeInterface::Private; @@ -4215,7 +4214,7 @@ Q_NATIVE_INTERFACE_EXPORT void *QNativeInterface::Private::resolveInterface(cons QT_NATIVE_INTERFACE_RETURN_IF(QWindowsApplication, platformIntegration); #endif - return resolveInterface(that, name, revision); + return QCoreApplication::resolveInterface(name, revision); } #include "moc_qguiapplication.cpp" diff --git a/src/gui/kernel/qkeymapper.cpp b/src/gui/kernel/qkeymapper.cpp index e3d39f80dc5..054d850dbbf 100644 --- a/src/gui/kernel/qkeymapper.cpp +++ b/src/gui/kernel/qkeymapper.cpp @@ -135,10 +135,9 @@ QList QKeyMapperPrivate::possibleKeys(QKeyEvent *e) return extractKeyFromEvent(e); } -template <> -Q_NATIVE_INTERFACE_EXPORT void *QNativeInterface::Private::resolveInterface(const QKeyMapper *that, const char *name, int revision) +void *QKeyMapper::resolveInterface(const char *name, int revision) const { - Q_UNUSED(that); Q_UNUSED(name); Q_UNUSED(revision); + Q_UNUSED(name); Q_UNUSED(revision); using namespace QNativeInterface::Private; #if QT_CONFIG(evdev) diff --git a/src/gui/kernel/qoffscreensurface.cpp b/src/gui/kernel/qoffscreensurface.cpp index 7cb8050504e..8fbb188bd18 100644 --- a/src/gui/kernel/qoffscreensurface.cpp +++ b/src/gui/kernel/qoffscreensurface.cpp @@ -371,16 +371,15 @@ QPlatformSurface *QOffscreenSurface::surfaceHandle() const using namespace QNativeInterface; -template <> -Q_NATIVE_INTERFACE_EXPORT void *QNativeInterface::Private::resolveInterface(const QOffscreenSurface *that, const char *name, int revision) +void *QOffscreenSurface::resolveInterface(const char *name, int revision) const { - Q_UNUSED(that); Q_UNUSED(name); Q_UNUSED(revision); + Q_UNUSED(name); Q_UNUSED(revision); - auto *surfacePrivate = QOffscreenSurfacePrivate::get(const_cast(that)); - Q_UNUSED(surfacePrivate); + Q_D(const QOffscreenSurface); + Q_UNUSED(d); #if defined(Q_OS_ANDROID) - QT_NATIVE_INTERFACE_RETURN_IF(QAndroidOffscreenSurface, surfacePrivate->platformOffscreenSurface); + QT_NATIVE_INTERFACE_RETURN_IF(QAndroidOffscreenSurface, d->platformOffscreenSurface); #endif return nullptr; diff --git a/src/gui/kernel/qopenglcontext.cpp b/src/gui/kernel/qopenglcontext.cpp index eef85bf0d03..bdbd18f7717 100644 --- a/src/gui/kernel/qopenglcontext.cpp +++ b/src/gui/kernel/qopenglcontext.cpp @@ -1312,12 +1312,11 @@ QDebug operator<<(QDebug debug, const QOpenGLContextGroup *cg) using namespace QNativeInterface; -template <> -Q_NATIVE_INTERFACE_EXPORT void *QNativeInterface::Private::resolveInterface(const QOpenGLContext *that, const char *name, int revision) +void *QOpenGLContext::resolveInterface(const char *name, int revision) const { - Q_UNUSED(that); Q_UNUSED(name); Q_UNUSED(revision); + Q_UNUSED(name); Q_UNUSED(revision); - auto *platformContext = that->handle(); + auto *platformContext = handle(); Q_UNUSED(platformContext); #if defined(Q_OS_MACOS) diff --git a/src/gui/kernel/qscreen.cpp b/src/gui/kernel/qscreen.cpp index 6f9f48d6c4b..e6f408875ec 100644 --- a/src/gui/kernel/qscreen.cpp +++ b/src/gui/kernel/qscreen.cpp @@ -785,13 +785,11 @@ QPixmap QScreen::grabWindow(WId window, int x, int y, int width, int height) result.setDevicePixelRatio(result.devicePixelRatio() * factor); return result; } - -template <> -Q_NATIVE_INTERFACE_EXPORT void *QNativeInterface::Private::resolveInterface(const QScreen *that, const char *name, int revision) +void *QScreen::resolveInterface(const char *name, int revision) const { using namespace QNativeInterface::Private; - auto *platformScreen = that->handle(); + auto *platformScreen = handle(); Q_UNUSED(platformScreen); Q_UNUSED(name); Q_UNUSED(revision); diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index 61c201559a5..7f6e5044cb2 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -3004,12 +3004,11 @@ bool QWindowPrivate::applyCursor() } #endif // QT_NO_CURSOR -template <> -Q_NATIVE_INTERFACE_EXPORT void *QNativeInterface::Private::resolveInterface(const QWindow *that, const char *name, int revision) +void *QWindow::resolveInterface(const char *name, int revision) const { using namespace QNativeInterface::Private; - auto *platformWindow = that->handle(); + auto *platformWindow = handle(); Q_UNUSED(platformWindow); Q_UNUSED(name); Q_UNUSED(revision); diff --git a/src/widgets/kernel/qapplication.cpp b/src/widgets/kernel/qapplication.cpp index fb881420769..25afb8b9b11 100644 --- a/src/widgets/kernel/qapplication.cpp +++ b/src/widgets/kernel/qapplication.cpp @@ -4101,10 +4101,9 @@ QPixmap QApplicationPrivate::applyQIconStyleHelper(QIcon::Mode mode, const QPixm return QApplication::style()->generatedIconPixmap(mode, base, &opt); } -template <> -Q_NATIVE_INTERFACE_EXPORT void *QNativeInterface::Private::resolveInterface(const QApplication *that, const char *name, int revision) +void *QApplication::resolveInterface(const char *name, int revision) const { - return resolveInterface(that, name, revision); + return QGuiApplication::resolveInterface(name, revision); } QT_END_NAMESPACE diff --git a/tests/auto/corelib/global/qnativeinterface/tst_qnativeinterface.cpp b/tests/auto/corelib/global/qnativeinterface/tst_qnativeinterface.cpp index dfd8d4fca19..dd4cc4755b5 100644 --- a/tests/auto/corelib/global/qnativeinterface/tst_qnativeinterface.cpp +++ b/tests/auto/corelib/global/qnativeinterface/tst_qnativeinterface.cpp @@ -37,6 +37,8 @@ private slots: void typeInfo() const; void resolve() const; void accessor() const; + + friend struct PublicClass; }; struct InterfaceImplementation; @@ -46,6 +48,8 @@ struct PublicClass PublicClass(); QT_DECLARE_NATIVE_INTERFACE_ACCESSOR(PublicClass) std::unique_ptr m_implementation; + + friend void tst_QNativeInterface::resolve() const; }; QT_BEGIN_NAMESPACE @@ -84,11 +88,9 @@ struct InterfaceImplementation : public Interface PublicClass::PublicClass() : m_implementation(new InterfaceImplementation) {} -template <> -void* QNativeInterface::Private::resolveInterface( - PublicClass const* that, char const* name, int revision) +void* PublicClass::resolveInterface(char const* name, int revision) const { - auto *implementation = that->m_implementation.get(); + auto *implementation = m_implementation.get(); QT_NATIVE_INTERFACE_RETURN_IF(Interface, implementation); QT_NATIVE_INTERFACE_RETURN_IF(OtherInterface, implementation); return nullptr; @@ -116,14 +118,14 @@ void tst_QNativeInterface::resolve() const PublicClass foo; - QVERIFY(resolveInterface(&foo, "Interface", 10)); + QVERIFY(foo.resolveInterface("Interface", 10)); QTest::ignoreMessage(QtWarningMsg, "Native interface revision mismatch " "(requested 5 / available 10) for interface Interface"); - QCOMPARE(resolveInterface(&foo, "Interface", 5), nullptr); - QCOMPARE(resolveInterface(&foo, "NotInterface", 10), nullptr); - QCOMPARE(resolveInterface(&foo, "OtherInterface", 10), nullptr); + QCOMPARE(foo.resolveInterface("Interface", 5), nullptr); + QCOMPARE(foo.resolveInterface("NotInterface", 10), nullptr); + QCOMPARE(foo.resolveInterface("OtherInterface", 10), nullptr); } void tst_QNativeInterface::accessor() const