2015-01-11 21:04:31 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-08-04 17:58:30 +01:00
|
|
|
#include <QObject>
|
|
|
|
#include <QSharedPointer>
|
2022-08-04 21:09:32 +01:00
|
|
|
|
2017-04-19 16:29:50 +01:00
|
|
|
#include <functional>
|
2015-01-11 21:04:31 +00:00
|
|
|
#include <memory>
|
2015-10-20 16:18:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A unique pointer class with unique pointer semantics intended for derivates of QObject
|
|
|
|
* Calls deleteLater() instead of destroying the contained object immediately
|
|
|
|
*/
|
2022-08-04 17:58:30 +01:00
|
|
|
template <typename T>
|
2022-08-04 21:09:32 +01:00
|
|
|
using unique_qobject_ptr = QScopedPointer<T, QScopedPointerDeleteLater>;
|
2015-01-11 21:04:31 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-20 16:18:53 +01:00
|
|
|
* A shared pointer class with shared pointer semantics intended for derivates of QObject
|
2015-01-11 21:04:31 +00:00
|
|
|
* Calls deleteLater() instead of destroying the contained object immediately
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2022-08-04 17:58:30 +01:00
|
|
|
class shared_qobject_ptr : public QSharedPointer<T> {
|
|
|
|
public:
|
|
|
|
constexpr shared_qobject_ptr() : QSharedPointer<T>() {}
|
2022-08-04 21:09:32 +01:00
|
|
|
constexpr shared_qobject_ptr(T* ptr) : QSharedPointer<T>(ptr, &QObject::deleteLater) {}
|
|
|
|
constexpr shared_qobject_ptr(std::nullptr_t null_ptr) : QSharedPointer<T>(null_ptr, &QObject::deleteLater) {}
|
2015-01-11 21:04:31 +00:00
|
|
|
|
2022-08-04 17:58:30 +01:00
|
|
|
template <typename Derived>
|
|
|
|
constexpr shared_qobject_ptr(const shared_qobject_ptr<Derived>& other) : QSharedPointer<T>(other)
|
|
|
|
{}
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
template <typename Derived>
|
|
|
|
constexpr shared_qobject_ptr(const QSharedPointer<Derived>& other) : QSharedPointer<T>(other)
|
|
|
|
{}
|
|
|
|
|
2022-08-04 17:58:30 +01:00
|
|
|
void reset() { QSharedPointer<T>::reset(); }
|
|
|
|
void reset(const shared_qobject_ptr<T>& other)
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2022-08-04 17:58:30 +01:00
|
|
|
shared_qobject_ptr<T> t(other);
|
|
|
|
this->swap(t);
|
2021-11-23 00:25:24 +00:00
|
|
|
}
|
2015-01-11 21:04:31 +00:00
|
|
|
};
|