From 362ecdb583293e09b55b95d270a72eae84510935 Mon Sep 17 00:00:00 2001 From: flow Date: Thu, 4 Aug 2022 13:58:30 -0300 Subject: refactor+fix: use QSharedPointer for shared_qobject_ptr Signed-off-by: flow --- launcher/QObjectPtr.h | 95 ++++++++++++++------------------------------------- 1 file changed, 26 insertions(+), 69 deletions(-) (limited to 'launcher/QObjectPtr.h') diff --git a/launcher/QObjectPtr.h b/launcher/QObjectPtr.h index 173dc5e7..7c453adb 100644 --- a/launcher/QObjectPtr.h +++ b/launcher/QObjectPtr.h @@ -1,91 +1,48 @@ #pragma once +#include +#include #include #include -#include -namespace details -{ -struct DeleteQObjectLater +namespace details { +[[maybe_unused]] static void do_delete_later(QObject* obj) { - void operator()(QObject *obj) const - { + 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 using unique_qobject_ptr = std::unique_ptr; +template +using unique_qobject_ptr = std::unique_ptr; /** * A shared pointer class with shared pointer semantics intended for derivates of QObject * Calls deleteLater() instead of destroying the contained object immediately */ template -class shared_qobject_ptr -{ -public: - shared_qobject_ptr(){} - shared_qobject_ptr(T * wrap) - { - reset(wrap); - } - shared_qobject_ptr(const shared_qobject_ptr& other) - { - m_ptr = other.m_ptr; - } - template - shared_qobject_ptr(const shared_qobject_ptr &other) - { - m_ptr = other.unwrap(); - } +class shared_qobject_ptr : public QSharedPointer { + public: + constexpr shared_qobject_ptr() : QSharedPointer() {} + constexpr shared_qobject_ptr(T* ptr) : QSharedPointer(ptr, details::do_delete_later) {} + constexpr shared_qobject_ptr(std::nullptr_t null_ptr) : QSharedPointer(null_ptr, details::do_delete_later) {} -public: - void reset(T * wrap) - { - using namespace std::placeholders; - m_ptr.reset(wrap, std::bind(&QObject::deleteLater, _1)); - } - void reset(const shared_qobject_ptr &other) - { - m_ptr = other.m_ptr; - } - void reset() - { - m_ptr.reset(); - } - T * get() const - { - return m_ptr.get(); - } - T * operator->() const - { - return m_ptr.get(); - } - T & operator*() const - { - return *m_ptr.get(); - } - operator bool() const - { - return m_ptr.get() != nullptr; - } - const std::shared_ptr unwrap() const + template + constexpr shared_qobject_ptr(const shared_qobject_ptr& other) : QSharedPointer(other) + {} + + void reset() { QSharedPointer::reset(); } + void reset(const shared_qobject_ptr& other) { - return m_ptr; - } - template - bool operator==(const shared_qobject_ptr& other) const { - return m_ptr == other.m_ptr; + shared_qobject_ptr t(other); + this->swap(t); } - template - bool operator!=(const shared_qobject_ptr& other) const { - return m_ptr != other.m_ptr; - } - -private: - std::shared_ptr m_ptr; }; -- cgit