refactor: simplify smart pointers more
Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
parent
362ecdb583
commit
d835e1d14e
@ -2,27 +2,16 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace details {
|
||||
[[maybe_unused]] static void do_delete_later(QObject* obj)
|
||||
{
|
||||
if (obj)
|
||||
obj->deleteLater();
|
||||
}
|
||||
struct DeleteQObjectLater {
|
||||
void operator()(QObject* obj) const { do_delete_later(obj); }
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
|
||||
/**
|
||||
* A unique pointer class with unique pointer semantics intended for derivates of QObject
|
||||
* Calls deleteLater() instead of destroying the contained object immediately
|
||||
*/
|
||||
template <typename T>
|
||||
using unique_qobject_ptr = std::unique_ptr<T, details::DeleteQObjectLater>;
|
||||
using unique_qobject_ptr = QScopedPointer<T, QScopedPointerDeleteLater>;
|
||||
|
||||
/**
|
||||
* A shared pointer class with shared pointer semantics intended for derivates of QObject
|
||||
@ -32,8 +21,8 @@ template <typename T>
|
||||
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) {}
|
||||
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) {}
|
||||
|
||||
template <typename Derived>
|
||||
constexpr shared_qobject_ptr(const shared_qobject_ptr<Derived>& other) : QSharedPointer<T>(other)
|
||||
|
Loading…
Reference in New Issue
Block a user