2015-01-11 21:04:31 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-08-04 17:58:30 +01:00
|
|
|
#include <QObject>
|
|
|
|
#include <QSharedPointer>
|
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
|
|
|
|
2022-08-04 17:58:30 +01:00
|
|
|
namespace details {
|
|
|
|
[[maybe_unused]] static void do_delete_later(QObject* obj)
|
2015-10-20 16:18:53 +01:00
|
|
|
{
|
2022-08-04 17:58:30 +01:00
|
|
|
if (obj)
|
2018-07-15 13:51:05 +01:00
|
|
|
obj->deleteLater();
|
2015-10-20 16:18:53 +01:00
|
|
|
}
|
2022-08-04 17:58:30 +01:00
|
|
|
struct DeleteQObjectLater {
|
|
|
|
void operator()(QObject* obj) const { do_delete_later(obj); }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace details
|
|
|
|
|
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>
|
|
|
|
using unique_qobject_ptr = std::unique_ptr<T, details::DeleteQObjectLater>;
|
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>() {}
|
|
|
|
constexpr shared_qobject_ptr(T* ptr) : QSharedPointer<T>(ptr, details::do_delete_later) {}
|
|
|
|
constexpr shared_qobject_ptr(std::nullptr_t null_ptr) : QSharedPointer<T>(null_ptr, details::do_delete_later) {}
|
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)
|
|
|
|
{}
|
|
|
|
|
|
|
|
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
|
|
|
};
|