From 4679fd67530d7fe9e790a3b6e16955aad0683410 Mon Sep 17 00:00:00 2001 From: fourtf Date: Tue, 13 Aug 2019 18:48:22 +0200 Subject: [PATCH] gcc Pepega --- src/util/QObjectRef.hpp | 119 +++++++++++++++--------------- src/widgets/splits/SplitInput.hpp | 1 + 2 files changed, 61 insertions(+), 59 deletions(-) diff --git a/src/util/QObjectRef.hpp b/src/util/QObjectRef.hpp index 183844d88..1065e8e7d 100644 --- a/src/util/QObjectRef.hpp +++ b/src/util/QObjectRef.hpp @@ -3,72 +3,73 @@ #include #include -namespace chatterino +namespace chatterino { +/// Holds a pointer to a QObject and resets it to nullptr if the QObject +/// gets destroyed. +template +class QObjectRef { - /// Holds a pointer to a QObject and resets it to nullptr if the QObject - /// gets destroyed. - template - class QObjectRef +public: + 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() + { + 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_) { - static_assert(std::is_base_of_v); + QObject::disconnect(this->conn_); } - explicit QObjectRef(T* t) + // new + if (other) { - static_assert(std::is_base_of_v); - - this->swap(t); - } - - ~QObjectRef() - { - this->swap(nullptr); - } - - QObjectRef& operator=(T* t) - { - this->swap(t); - return *this; - } - - operator bool() - { - return t_; - } - - T* operator->() - { - return t_; - } - - T* get() - { - return t_; - } - - QObject* swap(T* other) - { - // old - if (this->conn_) - { - QObject::disconnect(this->conn_); - } - - // new - if (other) - { + this->conn_ = QObject::connect(other, &QObject::destroyed, - [this]() { this->swap(nullptr); }); - } - - return std::exchange(this->t_, other); + [this](QObject *) { this->set(nullptr); }); } - private: - T* t_{}; - QMetaObject::Connection conn_; - }; + this->t_ = other; + } + + T *t_{}; + QMetaObject::Connection conn_; +}; } // namespace chatterino diff --git a/src/widgets/splits/SplitInput.hpp b/src/widgets/splits/SplitInput.hpp index 784b2f50b..53ecf9d70 100644 --- a/src/widgets/splits/SplitInput.hpp +++ b/src/widgets/splits/SplitInput.hpp @@ -2,6 +2,7 @@ #include "util/QObjectRef.hpp" #include "widgets/BaseWidget.hpp" +#include "widgets/dialogs/EmotePopup.hpp" #include #include