2015-01-12 02:34:31 +05:30
|
|
|
#pragma once
|
|
|
|
|
2022-08-04 22:28:30 +05:30
|
|
|
#include <QObject>
|
|
|
|
#include <QSharedPointer>
|
2022-08-05 01:39:32 +05:30
|
|
|
|
2017-04-19 20:59:50 +05:30
|
|
|
#include <functional>
|
2015-01-12 02:34:31 +05:30
|
|
|
#include <memory>
|
2015-10-20 20:48:53 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 22:28:30 +05:30
|
|
|
template <typename T>
|
2022-08-05 01:39:32 +05:30
|
|
|
using unique_qobject_ptr = QScopedPointer<T, QScopedPointerDeleteLater>;
|
2015-01-12 02:34:31 +05:30
|
|
|
|
|
|
|
/**
|
2015-10-20 20:48:53 +05:30
|
|
|
* A shared pointer class with shared pointer semantics intended for derivates of QObject
|
2015-01-12 02:34:31 +05:30
|
|
|
* Calls deleteLater() instead of destroying the contained object immediately
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2022-08-04 22:28:30 +05:30
|
|
|
class shared_qobject_ptr : public QSharedPointer<T> {
|
|
|
|
public:
|
2023-01-25 01:22:09 +05:30
|
|
|
constexpr explicit shared_qobject_ptr() : QSharedPointer<T>() {}
|
|
|
|
constexpr explicit shared_qobject_ptr(T* ptr) : QSharedPointer<T>(ptr, &QObject::deleteLater) {}
|
2022-08-05 01:39:32 +05:30
|
|
|
constexpr shared_qobject_ptr(std::nullptr_t null_ptr) : QSharedPointer<T>(null_ptr, &QObject::deleteLater) {}
|
2015-01-12 02:34:31 +05:30
|
|
|
|
2022-08-04 22:28:30 +05:30
|
|
|
template <typename Derived>
|
|
|
|
constexpr shared_qobject_ptr(const shared_qobject_ptr<Derived>& other) : QSharedPointer<T>(other)
|
|
|
|
{}
|
|
|
|
|
2023-01-03 22:28:27 +05:30
|
|
|
template <typename Derived>
|
|
|
|
constexpr shared_qobject_ptr(const QSharedPointer<Derived>& other) : QSharedPointer<T>(other)
|
|
|
|
{}
|
|
|
|
|
2022-08-04 22:28:30 +05:30
|
|
|
void reset() { QSharedPointer<T>::reset(); }
|
2023-01-25 01:22:09 +05:30
|
|
|
void reset(T*&& other)
|
|
|
|
{
|
|
|
|
shared_qobject_ptr<T> t(other);
|
|
|
|
this->swap(t);
|
|
|
|
}
|
2022-08-04 22:28:30 +05:30
|
|
|
void reset(const shared_qobject_ptr<T>& other)
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2022-08-04 22:28:30 +05:30
|
|
|
shared_qobject_ptr<T> t(other);
|
|
|
|
this->swap(t);
|
2021-11-23 05:55:24 +05:30
|
|
|
}
|
2015-01-12 02:34:31 +05:30
|
|
|
};
|
2023-01-25 01:22:09 +05:30
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
shared_qobject_ptr<T> makeShared(Args... args)
|
|
|
|
{
|
|
|
|
auto obj = new T(args...);
|
|
|
|
return shared_qobject_ptr<T>(obj);
|
|
|
|
}
|