QEventLoopLocker: add a visit() member

Still suffers from a bit of an impedance mismatch, because it's the
first step in making QEventLoopLocker shed its Private, but will be
used in a subsequent commit to DRY more code.

Task-number: QTBUG-114793
Pick-to: 6.6
Change-Id: Ia14effb6255961edae68eaf941fece9dca0cb844
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2023-07-03 07:29:29 +02:00
parent c70257e9b2
commit 8c2c4f178a
2 changed files with 25 additions and 16 deletions

View File

@ -312,6 +312,7 @@ void QEventLoop::quit()
class QEventLoopLockerPrivate
{
friend class QEventLoopLocker;
public:
explicit QEventLoopLockerPrivate(QEventLoopPrivate *loop)
: QEventLoopLockerPrivate(loop, EventLoop)
@ -331,22 +332,6 @@ public:
app->ref();
}
~QEventLoopLockerPrivate()
{
switch (type())
{
case EventLoop:
loop()->deref();
break;
case Thread:
thread()->deref();
break;
default:
app()->deref();
break;
}
}
private:
QEventLoopPrivate *loop() const { return static_cast<QEventLoopPrivate *>(pointer()); }
QThreadPrivate *thread() const { return static_cast<QThreadPrivate *>(pointer()); }
@ -432,9 +417,26 @@ QEventLoopLocker::QEventLoopLocker(QThread *thread)
*/
QEventLoopLocker::~QEventLoopLocker()
{
visit([](auto p) { p->deref(); });
delete d_ptr;
}
/*!
\internal
*/
template <typename Func>
void QEventLoopLocker::visit(Func f) const
{
using Type = QEventLoopLockerPrivate::Type;
const auto ptr = d_ptr->pointer();
switch (d_ptr->type()) {
case Type::EventLoop: return f(static_cast<QEventLoopPrivate *>(ptr));
case Type::Thread: return f(static_cast<QThreadPrivate *>(ptr));
case Type::Application: return f(static_cast<QCoreApplicationPrivate *>(ptr));
}
Q_UNREACHABLE();
}
QT_END_NAMESPACE
#include "moc_qeventloop.cpp"

View File

@ -63,7 +63,14 @@ public:
private:
Q_DISABLE_COPY(QEventLoopLocker)
//
// Private implementation details.
// Do not call from public inline API!
//
QEventLoopLockerPrivate *d_ptr;
template <typename Func>
void visit(Func func) const;
};
QT_END_NAMESPACE