diff options
author | Sefa Eyeoglu <contact@scrumplex.net> | 2022-08-20 12:50:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-20 12:50:50 +0200 |
commit | a517f442ea4c86b10607f58cb2d705c368286956 (patch) | |
tree | edb10f2b9386ecb3ce5407a1750c27cab857e778 /launcher/QObjectPtr.h | |
parent | 311758233b3ab103bf16c8ad71d9b52f2edbb824 (diff) | |
parent | d835e1d14e426ea73a66a0f8e225898598581f4a (diff) | |
download | PrismLauncher-a517f442ea4c86b10607f58cb2d705c368286956.tar.gz PrismLauncher-a517f442ea4c86b10607f58cb2d705c368286956.tar.bz2 PrismLauncher-a517f442ea4c86b10607f58cb2d705c368286956.zip |
Merge pull request #1023 from flowln/better_shared_ptr
Diffstat (limited to 'launcher/QObjectPtr.h')
-rw-r--r-- | launcher/QObjectPtr.h | 90 |
1 files changed, 18 insertions, 72 deletions
diff --git a/launcher/QObjectPtr.h b/launcher/QObjectPtr.h index 173dc5e7..b1ef1c8d 100644 --- a/launcher/QObjectPtr.h +++ b/launcher/QObjectPtr.h @@ -1,91 +1,37 @@ #pragma once +#include <QObject> +#include <QSharedPointer> + #include <functional> #include <memory> -#include <QObject> -namespace details -{ -struct DeleteQObjectLater -{ - void operator()(QObject *obj) const - { - obj->deleteLater(); - } -}; -} /** * 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 = QScopedPointer<T, QScopedPointerDeleteLater>; /** * 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, &QObject::deleteLater) {} + constexpr shared_qobject_ptr(std::nullptr_t null_ptr) : QSharedPointer<T>(null_ptr, &QObject::deleteLater) {} -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; + 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; - } - 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; }; |