From 6681ed5bfb5c798c3141d0d51828509d5ac50404 Mon Sep 17 00:00:00 2001 From: Arne <78976058+4rneee@users.noreply.github.com> Date: Sun, 4 Jun 2023 13:24:04 +0200 Subject: [PATCH] Remove QObjectRef in favor of QPointer (#4666) * replace usage of QObjectRef with QPointer * delete QObjectRef class * inlucde QPointer header * Add changelog entry * use isNull() instead of ! data() --------- Co-authored-by: Rasmus Karlsson --- CHANGELOG.md | 1 + src/common/NetworkPrivate.cpp | 10 ++-- src/common/NetworkPrivate.hpp | 4 +- src/providers/irc/IrcServer.cpp | 4 +- src/util/QObjectRef.hpp | 86 ------------------------------- src/widgets/splits/SplitInput.cpp | 14 ++--- src/widgets/splits/SplitInput.hpp | 6 +-- 7 files changed, 20 insertions(+), 105 deletions(-) delete mode 100644 src/util/QObjectRef.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index d7808315194..9387dd41e8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Dev: Expanded upon `$$$` test channels. (#4655) - Dev: Added tools to help debug image GC. (#4578) - Dev: Removed duplicate license when having plugins enabled. (#4665) +- Dev: Replace our QObjectRef class with Qt's QPointer class. (#4666) ## 2.4.4 diff --git a/src/common/NetworkPrivate.cpp b/src/common/NetworkPrivate.cpp index f0a74ed5ab4..9af3018837b 100644 --- a/src/common/NetworkPrivate.cpp +++ b/src/common/NetworkPrivate.cpp @@ -174,7 +174,7 @@ void loadUncached(std::shared_ptr &&data) } auto handleReply = [data, reply]() mutable { - if (data->hasCaller_ && !data->caller_.get()) + if (data->hasCaller_ && data->caller_.isNull()) { return; } @@ -350,7 +350,7 @@ void loadCached(std::shared_ptr &&data) // XXX: If outcome is Failure, we should invalidate the cache file // somehow/somewhere /*auto outcome =*/ - if (data->hasCaller_ && !data->caller_.get()) + if (data->hasCaller_ && data->caller_.isNull()) { return; } @@ -359,7 +359,7 @@ void loadCached(std::shared_ptr &&data) else { postToThread([data, result]() { - if (data->hasCaller_ && !data->caller_.get()) + if (data->hasCaller_ && data->caller_.isNull()) { return; } @@ -373,7 +373,7 @@ void loadCached(std::shared_ptr &&data) { if (data->executeConcurrently_ || isGuiThread()) { - if (data->hasCaller_ && !data->caller_.get()) + if (data->hasCaller_ && data->caller_.isNull()) { return; } @@ -383,7 +383,7 @@ void loadCached(std::shared_ptr &&data) else { postToThread([data]() { - if (data->hasCaller_ && !data->caller_.get()) + if (data->hasCaller_ && data->caller_.isNull()) { return; } diff --git a/src/common/NetworkPrivate.hpp b/src/common/NetworkPrivate.hpp index 3fe841bc22f..d48000aeb1f 100644 --- a/src/common/NetworkPrivate.hpp +++ b/src/common/NetworkPrivate.hpp @@ -1,10 +1,10 @@ #pragma once #include "common/NetworkCommon.hpp" -#include "util/QObjectRef.hpp" #include #include +#include #include #include @@ -38,7 +38,7 @@ struct NetworkData { QNetworkRequest request_; bool hasCaller_{}; - QObjectRef caller_; + QPointer caller_; bool cache_{}; bool executeConcurrently_{}; diff --git a/src/providers/irc/IrcServer.cpp b/src/providers/irc/IrcServer.cpp index 5ae01c56bb5..ad94d030584 100644 --- a/src/providers/irc/IrcServer.cpp +++ b/src/providers/irc/IrcServer.cpp @@ -11,9 +11,9 @@ #include "providers/twitch/TwitchIrcServer.hpp" // NOTE: Included to access the mentions channel #include "singletons/Settings.hpp" #include "util/IrcHelpers.hpp" -#include "util/QObjectRef.hpp" #include +#include #include #include @@ -151,7 +151,7 @@ void IrcServer::initializeConnection(IrcConnection *connection, [[fallthrough]]; case IrcAuthType::Pass: this->data_->getPassword( - this, [conn = new QObjectRef(connection) /* can't copy */, + this, [conn = new QPointer(connection) /* can't copy */, this](const QString &password) mutable { if (*conn) { diff --git a/src/util/QObjectRef.hpp b/src/util/QObjectRef.hpp deleted file mode 100644 index 07444d08535..00000000000 --- a/src/util/QObjectRef.hpp +++ /dev/null @@ -1,86 +0,0 @@ -#pragma once - -#include -#include - -#include - -namespace chatterino { -/// Holds a pointer to a QObject and resets it to nullptr if the QObject -/// gets destroyed. -template -class QObjectRef -{ -public: - QObjectRef() - { - static_assert(std::is_base_of_v); - } - - explicit QObjectRef(T *t) - { - static_assert(std::is_base_of_v); - - this->set(t); - } - - QObjectRef(const QObjectRef &other) - { - this->set(other.t_); - } - - ~QObjectRef() - { - this->set(nullptr); - } - - QObjectRef &operator=(T *t) - { - this->set(t); - - return *this; - } - - operator bool() - { - return t_; - } - - T *operator->() - { - return t_; - } - - T *get() - { - return t_; - } - -private: - void set(T *other) - { - // old - if (this->conn_) - { - QObject::disconnect(this->conn_); - } - - // new - if (other) - { - // the cast here should absolutely not be necessary, but gcc still requires it - this->conn_ = - QObject::connect((QObject *)other, &QObject::destroyed, qApp, - [this](QObject *) { - this->set(nullptr); - }, - Qt::DirectConnection); - } - - this->t_ = other; - } - - std::atomic t_{}; - QMetaObject::Connection conn_; -}; -} // namespace chatterino diff --git a/src/widgets/splits/SplitInput.cpp b/src/widgets/splits/SplitInput.cpp index 56af3e5d589..c76e48f63a2 100644 --- a/src/widgets/splits/SplitInput.cpp +++ b/src/widgets/splits/SplitInput.cpp @@ -630,7 +630,7 @@ bool SplitInput::eventFilter(QObject *obj, QEvent *event) if (event->type() == QEvent::ShortcutOverride || event->type() == QEvent::Shortcut) { - if (auto popup = this->inputCompletionPopup_.get()) + if (auto popup = this->inputCompletionPopup_.data()) { if (popup->isVisible()) { @@ -650,7 +650,7 @@ void SplitInput::installKeyPressedEvent() { this->ui_.textEdit->keyPressed.disconnectAll(); this->ui_.textEdit->keyPressed.connect([this](QKeyEvent *event) { - if (auto *popup = this->inputCompletionPopup_.get()) + if (auto *popup = this->inputCompletionPopup_.data()) { if (popup->isVisible()) { @@ -764,12 +764,12 @@ void SplitInput::updateCompletionPopup() void SplitInput::showCompletionPopup(const QString &text, bool emoteCompletion) { - if (!this->inputCompletionPopup_.get()) + if (this->inputCompletionPopup_.isNull()) { this->inputCompletionPopup_ = new InputCompletionPopup(this); this->inputCompletionPopup_->setInputAction( - [that = QObjectRef(this)](const QString &text) mutable { - if (auto *this2 = that.get()) + [that = QPointer(this)](const QString &text) mutable { + if (auto *this2 = that.data()) { this2->insertCompletionText(text); this2->hideCompletionPopup(); @@ -777,7 +777,7 @@ void SplitInput::showCompletionPopup(const QString &text, bool emoteCompletion) }); } - auto *popup = this->inputCompletionPopup_.get(); + auto *popup = this->inputCompletionPopup_.data(); assert(popup); if (emoteCompletion) @@ -798,7 +798,7 @@ void SplitInput::showCompletionPopup(const QString &text, bool emoteCompletion) void SplitInput::hideCompletionPopup() { - if (auto *popup = this->inputCompletionPopup_.get()) + if (auto *popup = this->inputCompletionPopup_.data()) { popup->hide(); } diff --git a/src/widgets/splits/SplitInput.hpp b/src/widgets/splits/SplitInput.hpp index 2199f855d78..192795c7344 100644 --- a/src/widgets/splits/SplitInput.hpp +++ b/src/widgets/splits/SplitInput.hpp @@ -1,12 +1,12 @@ #pragma once -#include "util/QObjectRef.hpp" #include "widgets/BaseWidget.hpp" #include #include #include #include +#include #include #include #include @@ -113,8 +113,8 @@ class SplitInput : public BaseWidget Split *const split_; ChannelView *const channelView_; - QObjectRef emotePopup_; - QObjectRef inputCompletionPopup_; + QPointer emotePopup_; + QPointer inputCompletionPopup_; struct { ResizingTextEdit *textEdit;