diff options
author | Marc Mutz <marc.mutz@qt.io> | 2023-07-03 07:43:28 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2023-07-04 18:29:28 +0000 |
commit | d18a9dd977a59bf735ed3b6a19e101d4b26a1cef (patch) | |
tree | c548552b3fdd259cb9ecd9423b3239b4155b119d /src/corelib/kernel/qeventloop.cpp | |
parent | 586e07785ce276547d30502a8a44b212d37b95c7 (diff) |
QEventLoopLocker: use visit() to DRY ctors
Collapse all three Private ctors into one (void*, Type) one.
Task-number: QTBUG-114793
Pick-to: 6.6
Change-Id: Ia5c67c0ffdcddfdecb38fe3e095d60f6761f1160
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'src/corelib/kernel/qeventloop.cpp')
-rw-r--r-- | src/corelib/kernel/qeventloop.cpp | 50 |
1 files changed, 22 insertions, 28 deletions
diff --git a/src/corelib/kernel/qeventloop.cpp b/src/corelib/kernel/qeventloop.cpp index f3ddc84758..768175257f 100644 --- a/src/corelib/kernel/qeventloop.cpp +++ b/src/corelib/kernel/qeventloop.cpp @@ -312,35 +312,11 @@ void QEventLoop::quit() class QEventLoopLockerPrivate { - friend class QEventLoopLocker; public: - explicit QEventLoopLockerPrivate(QEventLoopPrivate *loop) - : QEventLoopLockerPrivate(loop, EventLoop) - { - loop->ref(); - } - - explicit QEventLoopLockerPrivate(QThreadPrivate *thread) - : QEventLoopLockerPrivate(thread, Thread) - { - thread->ref(); - } - - explicit QEventLoopLockerPrivate(QCoreApplicationPrivate *app) - : QEventLoopLockerPrivate(app, Application) - { - app->ref(); - } - -private: + using Type = QEventLoopLocker::Type; QEventLoopPrivate *loop() const { return static_cast<QEventLoopPrivate *>(pointer()); } QThreadPrivate *thread() const { return static_cast<QThreadPrivate *>(pointer()); } QCoreApplicationPrivate *app() const { return static_cast<QCoreApplicationPrivate *>(pointer()); } - enum Type { - EventLoop, - Thread, - Application - }; explicit QEventLoopLockerPrivate(void *ptr, Type t) noexcept : p{quintptr(ptr) | quintptr(t)} {} quintptr p; @@ -348,11 +324,19 @@ private: Type type() const { return Type(p & TypeMask); } void *pointer() const { return reinterpret_cast<void *>(p & ~TypeMask); } }; +namespace { // If any of these trigger, the Type bits will interfere with the pointer values: static_assert(alignof(QEventLoopPrivate) >= 4); static_assert(alignof(QThreadPrivate) >= 4); static_assert(alignof(QCoreApplicationPrivate) >= 4); +template <typename Private> +Private *o2p(QObject *o) +{ + return static_cast<Private*>(QObjectPrivate::get(o)); +} +} // unnamed namespace + /*! \class QEventLoopLocker \inmodule QtCore @@ -381,7 +365,8 @@ static_assert(alignof(QCoreApplicationPrivate) >= 4); \sa QCoreApplication::quit(), QCoreApplication::isQuitLockEnabled() */ QEventLoopLocker::QEventLoopLocker() - : d_ptr(new QEventLoopLockerPrivate(static_cast<QCoreApplicationPrivate*>(QObjectPrivate::get(QCoreApplication::instance())))) + : QEventLoopLocker{o2p<QCoreApplicationPrivate>(QCoreApplication::instance()), + Type::Application} { } @@ -394,7 +379,7 @@ QEventLoopLocker::QEventLoopLocker() \sa QEventLoop::quit() */ QEventLoopLocker::QEventLoopLocker(QEventLoop *loop) - : d_ptr(new QEventLoopLockerPrivate(static_cast<QEventLoopPrivate*>(QObjectPrivate::get(loop)))) + : QEventLoopLocker{o2p<QEventLoopPrivate>(loop), Type::EventLoop} { } @@ -407,7 +392,7 @@ QEventLoopLocker::QEventLoopLocker(QEventLoop *loop) \sa QThread::quit() */ QEventLoopLocker::QEventLoopLocker(QThread *thread) - : d_ptr(new QEventLoopLockerPrivate(static_cast<QThreadPrivate*>(QObjectPrivate::get(thread)))) + : QEventLoopLocker{o2p<QThreadPrivate>(thread), Type::Thread} { } @@ -424,6 +409,15 @@ QEventLoopLocker::~QEventLoopLocker() /*! \internal */ +QEventLoopLocker::QEventLoopLocker(void *ptr, Type t) noexcept + : d_ptr(new QEventLoopLockerPrivate{ptr, t}) +{ + visit([](auto p) { p->ref(); }); +} + +/*! + \internal +*/ template <typename Func> void QEventLoopLocker::visit(Func f) const { |