diff options
author | flow <flowlnlnln@gmail.com> | 2022-08-04 13:58:30 -0300 |
---|---|---|
committer | flow <flowlnlnln@gmail.com> | 2022-08-04 15:33:51 -0300 |
commit | 362ecdb583293e09b55b95d270a72eae84510935 (patch) | |
tree | e3a0bad6c9e354f9552559306d267db27b7b015d /launcher/QObjectPtr.h | |
parent | 6fd3672618f425bb539f4d554a5fda5d1236d042 (diff) | |
download | PrismLauncher-362ecdb583293e09b55b95d270a72eae84510935.tar.gz PrismLauncher-362ecdb583293e09b55b95d270a72eae84510935.tar.bz2 PrismLauncher-362ecdb583293e09b55b95d270a72eae84510935.zip |
refactor+fix: use QSharedPointer for shared_qobject_ptr
Signed-off-by: flow <flowlnlnln@gmail.com>
Diffstat (limited to 'launcher/QObjectPtr.h')
-rw-r--r-- | launcher/QObjectPtr.h | 95 |
1 files changed, 26 insertions, 69 deletions
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 <QObject> +#include <QSharedPointer> #include <functional> #include <memory> -#include <QObject> -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<typename T> using unique_qobject_ptr = std::unique_ptr<T, details::DeleteQObjectLater>; +template <typename T> +using unique_qobject_ptr = std::unique_ptr<T, details::DeleteQObjectLater>; /** * A shared pointer class with shared pointer semantics intended for derivates of QObject * Calls deleteLater() instead of destroying the contained object immediately */ template <typename T> -class shared_qobject_ptr -{ -public: - shared_qobject_ptr(){} - shared_qobject_ptr(T * wrap) - { - reset(wrap); - } - shared_qobject_ptr(const shared_qobject_ptr<T>& other) - { - m_ptr = other.m_ptr; - } - template<typename Derived> - shared_qobject_ptr(const shared_qobject_ptr<Derived> &other) - { - m_ptr = other.unwrap(); - } +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) {} -public: - void reset(T * wrap) - { - using namespace std::placeholders; - m_ptr.reset(wrap, std::bind(&QObject::deleteLater, _1)); - } - void reset(const shared_qobject_ptr<T> &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 <T> unwrap() const + 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) { - return m_ptr; - } - template<typename U> - bool operator==(const shared_qobject_ptr<U>& other) const { - return m_ptr == other.m_ptr; + shared_qobject_ptr<T> t(other); + this->swap(t); } - template<typename U> - bool operator!=(const shared_qobject_ptr<U>& other) const { - return m_ptr != other.m_ptr; - } - -private: - std::shared_ptr <T> m_ptr; }; |