From 5864934335fa3f4b7124db998c644982499ef481 Mon Sep 17 00:00:00 2001 From: Arne Date: Sun, 4 Jun 2023 08:55:18 +0200 Subject: [PATCH 1/5] replace usage of QObjectRef with QPointer --- src/common/NetworkPrivate.cpp | 10 +++++----- src/common/NetworkPrivate.hpp | 3 +-- src/providers/irc/IrcServer.cpp | 3 +-- src/widgets/splits/SplitInput.cpp | 14 +++++++------- src/widgets/splits/SplitInput.hpp | 5 ++--- 5 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/common/NetworkPrivate.cpp b/src/common/NetworkPrivate.cpp index f0a74ed5ab4..943b2e4840a 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_.data()) { 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_.data()) { 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_.data()) { 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_.data()) { 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_.data()) { return; } diff --git a/src/common/NetworkPrivate.hpp b/src/common/NetworkPrivate.hpp index 3fe841bc22f..98e0de01367 100644 --- a/src/common/NetworkPrivate.hpp +++ b/src/common/NetworkPrivate.hpp @@ -1,7 +1,6 @@ #pragma once #include "common/NetworkCommon.hpp" -#include "util/QObjectRef.hpp" #include #include @@ -38,7 +37,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..e57a60c68d9 100644 --- a/src/providers/irc/IrcServer.cpp +++ b/src/providers/irc/IrcServer.cpp @@ -11,7 +11,6 @@ #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 @@ -151,7 +150,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/widgets/splits/SplitInput.cpp b/src/widgets/splits/SplitInput.cpp index 56af3e5d589..70fecd1d5bc 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_.data()) { 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..650d4809aab 100644 --- a/src/widgets/splits/SplitInput.hpp +++ b/src/widgets/splits/SplitInput.hpp @@ -1,6 +1,5 @@ #pragma once -#include "util/QObjectRef.hpp" #include "widgets/BaseWidget.hpp" #include @@ -113,8 +112,8 @@ class SplitInput : public BaseWidget Split *const split_; ChannelView *const channelView_; - QObjectRef emotePopup_; - QObjectRef inputCompletionPopup_; + QPointer emotePopup_; + QPointer inputCompletionPopup_; struct { ResizingTextEdit *textEdit; From 185857da91810b2e4936036ae6afdb58720a5e06 Mon Sep 17 00:00:00 2001 From: Arne Date: Sun, 4 Jun 2023 08:56:14 +0200 Subject: [PATCH 2/5] delete QObjectRef class --- src/util/QObjectRef.hpp | 86 ----------------------------------------- 1 file changed, 86 deletions(-) delete mode 100644 src/util/QObjectRef.hpp 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 From ab9a1e26dbb885c15c62c11f6fa6613564788db5 Mon Sep 17 00:00:00 2001 From: Arne Date: Sun, 4 Jun 2023 09:15:53 +0200 Subject: [PATCH 3/5] inlucde QPointer header --- src/common/NetworkPrivate.hpp | 1 + src/providers/irc/IrcServer.cpp | 1 + src/widgets/splits/SplitInput.hpp | 1 + 3 files changed, 3 insertions(+) diff --git a/src/common/NetworkPrivate.hpp b/src/common/NetworkPrivate.hpp index 98e0de01367..d48000aeb1f 100644 --- a/src/common/NetworkPrivate.hpp +++ b/src/common/NetworkPrivate.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include diff --git a/src/providers/irc/IrcServer.cpp b/src/providers/irc/IrcServer.cpp index e57a60c68d9..ad94d030584 100644 --- a/src/providers/irc/IrcServer.cpp +++ b/src/providers/irc/IrcServer.cpp @@ -13,6 +13,7 @@ #include "util/IrcHelpers.hpp" #include +#include #include #include diff --git a/src/widgets/splits/SplitInput.hpp b/src/widgets/splits/SplitInput.hpp index 650d4809aab..192795c7344 100644 --- a/src/widgets/splits/SplitInput.hpp +++ b/src/widgets/splits/SplitInput.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include From e109cdc70ef96dc0d0242e9f7af80e9ba9052747 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 4 Jun 2023 11:45:52 +0200 Subject: [PATCH 4/5] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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 From 4676d9729e34a3988f1fbb27cd198dd57a53aae3 Mon Sep 17 00:00:00 2001 From: Arne Date: Sun, 4 Jun 2023 12:44:06 +0200 Subject: [PATCH 5/5] use isNull() instead of ! data() --- src/common/NetworkPrivate.cpp | 10 +++++----- src/widgets/splits/SplitInput.cpp | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/common/NetworkPrivate.cpp b/src/common/NetworkPrivate.cpp index 943b2e4840a..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_.data()) + 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_.data()) + 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_.data()) + 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_.data()) + 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_.data()) + if (data->hasCaller_ && data->caller_.isNull()) { return; } diff --git a/src/widgets/splits/SplitInput.cpp b/src/widgets/splits/SplitInput.cpp index 70fecd1d5bc..c76e48f63a2 100644 --- a/src/widgets/splits/SplitInput.cpp +++ b/src/widgets/splits/SplitInput.cpp @@ -764,7 +764,7 @@ void SplitInput::updateCompletionPopup() void SplitInput::showCompletionPopup(const QString &text, bool emoteCompletion) { - if (!this->inputCompletionPopup_.data()) + if (this->inputCompletionPopup_.isNull()) { this->inputCompletionPopup_ = new InputCompletionPopup(this); this->inputCompletionPopup_->setInputAction(