gcc Pepega

This commit is contained in:
fourtf 2019-08-13 18:48:22 +02:00
parent 7bf5a79f8a
commit 4679fd6753
2 changed files with 61 additions and 59 deletions

View file

@ -3,72 +3,73 @@
#include <QObject> #include <QObject>
#include <type_traits> #include <type_traits>
namespace chatterino namespace chatterino {
/// Holds a pointer to a QObject and resets it to nullptr if the QObject
/// gets destroyed.
template <typename T>
class QObjectRef
{ {
/// Holds a pointer to a QObject and resets it to nullptr if the QObject public:
/// gets destroyed. QObjectRef()
template <typename T>
class QObjectRef
{ {
public: static_assert(std::is_base_of_v<QObject, T>);
QObjectRef() }
explicit QObjectRef(T *t)
{
static_assert(std::is_base_of_v<QObject, T>);
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, T>); QObject::disconnect(this->conn_);
} }
explicit QObjectRef(T* t) // new
if (other)
{ {
static_assert(std::is_base_of_v<QObject, T>); this->conn_ =
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)
{
QObject::connect(other, &QObject::destroyed, QObject::connect(other, &QObject::destroyed,
[this]() { this->swap(nullptr); }); [this](QObject *) { this->set(nullptr); });
}
return std::exchange(this->t_, other);
} }
private: this->t_ = other;
T* t_{}; }
QMetaObject::Connection conn_;
}; T *t_{};
QMetaObject::Connection conn_;
};
} // namespace chatterino } // namespace chatterino

View file

@ -2,6 +2,7 @@
#include "util/QObjectRef.hpp" #include "util/QObjectRef.hpp"
#include "widgets/BaseWidget.hpp" #include "widgets/BaseWidget.hpp"
#include "widgets/dialogs/EmotePopup.hpp"
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>