| 1 | // Copyright (C) 2016 The Qt Company Ltd. | 
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
| 3 |  | 
| 4 | #ifndef QPOINTER_H | 
| 5 | #define QPOINTER_H | 
| 6 |  | 
| 7 | #include <QtCore/qcompare.h> | 
| 8 | #include <QtCore/qsharedpointer.h> | 
| 9 | #include <QtCore/qtypeinfo.h> | 
| 10 |  | 
| 11 | #ifndef QT_NO_QOBJECT | 
| 12 |  | 
| 13 | QT_BEGIN_NAMESPACE | 
| 14 |  | 
| 15 | class QVariant; | 
| 16 |  | 
| 17 | template <class T> | 
| 18 | class QPointer | 
| 19 | { | 
| 20 |     static_assert(!std::is_pointer<T>::value, "QPointer's template type must not be a pointer type" ); | 
| 21 |  | 
| 22 |     template <typename X> | 
| 23 |     using if_convertible = std::enable_if_t<std::is_convertible_v<X*, T*>, bool>; | 
| 24 |     template <typename X> | 
| 25 |     friend class QPointer; | 
| 26 |  | 
| 27 |     using QObjectType = | 
| 28 |         typename std::conditional<std::is_const<T>::value, const QObject, QObject>::type; | 
| 29 |     QWeakPointer<QObjectType> wp; | 
| 30 | public: | 
| 31 |     Q_NODISCARD_CTOR | 
| 32 |     QPointer() noexcept = default; | 
| 33 |     Q_NODISCARD_CTOR | 
| 34 |     constexpr QPointer(std::nullptr_t) noexcept : QPointer{} {} | 
| 35 |     Q_WEAK_OVERLOAD | 
| 36 |     Q_NODISCARD_CTOR | 
| 37 |     inline QPointer(T *p) : wp(p, true) { } | 
| 38 |     // compiler-generated copy/move ctor/assignment operators are fine! | 
| 39 |     // compiler-generated dtor is fine! | 
| 40 |  | 
| 41 |     template <typename X, if_convertible<X> = true> | 
| 42 |     Q_NODISCARD_CTOR | 
| 43 |     QPointer(QPointer<X> &&other) noexcept | 
| 44 |         : wp(std::exchange(other.wp, nullptr).internalData(), true) {} | 
| 45 |     template <typename X, if_convertible<X> = true> | 
| 46 |     Q_NODISCARD_CTOR | 
| 47 |     QPointer(const QPointer<X> &other) noexcept | 
| 48 |         : wp(other.wp.internalData(), true) {} | 
| 49 |  | 
| 50 |     template <typename X, if_convertible<X> = true> | 
| 51 |     QPointer &operator=(const QPointer<X> &other) noexcept | 
| 52 |     { | 
| 53 |         QPointer(other).swap(*this); | 
| 54 |         return *this; | 
| 55 |     } | 
| 56 |  | 
| 57 |     template <typename X, if_convertible<X> = true> | 
| 58 |     QPointer &operator=(QPointer<X> &&other) noexcept | 
| 59 |     { | 
| 60 |         QPointer(std::move(other)).swap(*this); | 
| 61 |         return *this; | 
| 62 |     } | 
| 63 |  | 
| 64 | #ifdef Q_QDOC | 
| 65 |     // Stop qdoc from complaining about missing function | 
| 66 |     ~QPointer(); | 
| 67 | #endif | 
| 68 |  | 
| 69 |     inline void swap(QPointer &other) noexcept { wp.swap(other.wp); } | 
| 70 |  | 
| 71 |     inline QPointer<T> &operator=(T* p) | 
| 72 |     { wp.assign(static_cast<QObjectType*>(p)); return *this; } | 
| 73 |  | 
| 74 |     T* data() const noexcept | 
| 75 |     { return static_cast<T*>(wp.internalData()); } | 
| 76 |     T* get() const noexcept | 
| 77 |     { return data(); } | 
| 78 |     T* operator->() const noexcept | 
| 79 |     { return data(); } | 
| 80 |     T& operator*() const noexcept | 
| 81 |     { return *data(); } | 
| 82 |     operator T*() const noexcept | 
| 83 |     { return data(); } | 
| 84 |  | 
| 85 |     bool isNull() const noexcept | 
| 86 |     { return wp.isNull(); } | 
| 87 |  | 
| 88 |     void clear() noexcept | 
| 89 |     { wp.clear(); } | 
| 90 |  | 
| 91 |     friend void swap(QPointer &lhs, QPointer &rhs) noexcept | 
| 92 |     { lhs.swap(rhs); } | 
| 93 |  | 
| 94 | private: | 
| 95 |     template <typename X> | 
| 96 |     friend bool comparesEqual(const QPointer &lhs, const QPointer<X> &rhs) noexcept | 
| 97 |     { return lhs.data() == rhs.data(); } | 
| 98 |     QT_DECLARE_EQUALITY_OPERATORS_HELPER(QPointer, QPointer<X>, /* non-constexpr */, | 
| 99 |                                          noexcept(true), template <typename X>) | 
| 100 |  | 
| 101 |     template <typename X> | 
| 102 |     friend bool comparesEqual(const QPointer &lhs, X *rhs) noexcept | 
| 103 |     { return lhs.data() == rhs; } | 
| 104 |     Q_DECLARE_EQUALITY_COMPARABLE(QPointer, X*, template <typename X>) | 
| 105 |  | 
| 106 |     friend bool comparesEqual(const QPointer &lhs, std::nullptr_t) noexcept | 
| 107 |     { return lhs.data() == nullptr; } | 
| 108 |     Q_DECLARE_EQUALITY_COMPARABLE(QPointer, std::nullptr_t) | 
| 109 | }; | 
| 110 | template <class T> Q_DECLARE_TYPEINFO_BODY(QPointer<T>, Q_RELOCATABLE_TYPE); | 
| 111 |  | 
| 112 | template<typename T> | 
| 113 | QPointer<T> | 
| 114 | qPointerFromVariant(const QVariant &variant) | 
| 115 | { | 
| 116 |     const auto wp = QtSharedPointer::weakPointerFromVariant_internal(variant); | 
| 117 |     return QPointer<T>{qobject_cast<T*>(QtPrivate::EnableInternalData::internalData(p: wp))}; | 
| 118 | } | 
| 119 |  | 
| 120 | QT_END_NAMESPACE | 
| 121 |  | 
| 122 | #endif // QT_NO_QOBJECT | 
| 123 |  | 
| 124 | #endif // QPOINTER_H | 
| 125 |  |