mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
gcc Pepega
This commit is contained in:
parent
7bf5a79f8a
commit
4679fd6753
2 changed files with 61 additions and 59 deletions
|
@ -3,34 +3,34 @@
|
|||
#include <QObject>
|
||||
#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
|
||||
/// gets destroyed.
|
||||
template <typename T>
|
||||
class QObjectRef
|
||||
{
|
||||
public:
|
||||
public:
|
||||
QObjectRef()
|
||||
{
|
||||
static_assert(std::is_base_of_v<QObject, T>);
|
||||
}
|
||||
|
||||
explicit QObjectRef(T* t)
|
||||
explicit QObjectRef(T *t)
|
||||
{
|
||||
static_assert(std::is_base_of_v<QObject, T>);
|
||||
|
||||
this->swap(t);
|
||||
this->set(t);
|
||||
}
|
||||
|
||||
~QObjectRef()
|
||||
{
|
||||
this->swap(nullptr);
|
||||
this->set(nullptr);
|
||||
}
|
||||
|
||||
QObjectRef& operator=(T* t)
|
||||
QObjectRef &operator=(T *t)
|
||||
{
|
||||
this->swap(t);
|
||||
this->set(t);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -39,17 +39,18 @@ namespace chatterino
|
|||
return t_;
|
||||
}
|
||||
|
||||
T* operator->()
|
||||
T *operator->()
|
||||
{
|
||||
return t_;
|
||||
}
|
||||
|
||||
T* get()
|
||||
T *get()
|
||||
{
|
||||
return t_;
|
||||
}
|
||||
|
||||
QObject* swap(T* other)
|
||||
private:
|
||||
void set(T *other)
|
||||
{
|
||||
// old
|
||||
if (this->conn_)
|
||||
|
@ -60,15 +61,15 @@ namespace chatterino
|
|||
// new
|
||||
if (other)
|
||||
{
|
||||
this->conn_ =
|
||||
QObject::connect(other, &QObject::destroyed,
|
||||
[this]() { this->swap(nullptr); });
|
||||
[this](QObject *) { this->set(nullptr); });
|
||||
}
|
||||
|
||||
return std::exchange(this->t_, other);
|
||||
this->t_ = other;
|
||||
}
|
||||
|
||||
private:
|
||||
T* t_{};
|
||||
T *t_{};
|
||||
QMetaObject::Connection conn_;
|
||||
};
|
||||
};
|
||||
} // namespace chatterino
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "util/QObjectRef.hpp"
|
||||
#include "widgets/BaseWidget.hpp"
|
||||
#include "widgets/dialogs/EmotePopup.hpp"
|
||||
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
|
|
Loading…
Reference in a new issue