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 <rasmus.karlsson@pajlada.com>
This commit is contained in:
@@ -1,86 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <QApplication>
|
||||
#include <QObject>
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace chatterino {
|
||||
/// Holds a pointer to a QObject and resets it to nullptr if the QObject
|
||||
/// gets destroyed.
|
||||
template <typename T>
|
||||
class QObjectRef
|
||||
{
|
||||
public:
|
||||
QObjectRef()
|
||||
{
|
||||
static_assert(std::is_base_of_v<QObject, T>);
|
||||
}
|
||||
|
||||
explicit QObjectRef(T *t)
|
||||
{
|
||||
static_assert(std::is_base_of_v<QObject, T>);
|
||||
|
||||
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 *> t_{};
|
||||
QMetaObject::Connection conn_;
|
||||
};
|
||||
} // namespace chatterino
|
||||
Reference in New Issue
Block a user