File indexing completed on 2026-09-04 09:22:58
0001
0002
0003
0004
0005 #ifndef QPOINTER_H
0006 #define QPOINTER_H
0007
0008 #include <QtCore/qcompare.h>
0009 #include <QtCore/qsharedpointer.h>
0010 #include <QtCore/qtypeinfo.h>
0011
0012 #ifndef QT_NO_QOBJECT
0013
0014 QT_BEGIN_NAMESPACE
0015
0016 class QVariant;
0017
0018 template <class T>
0019 class QPointer
0020 {
0021 static_assert(!std::is_pointer<T>::value, "QPointer's template type must not be a pointer type");
0022
0023 template <typename X>
0024 using if_convertible = std::enable_if_t<std::is_convertible_v<X*, T*>, bool>;
0025 template <typename X>
0026 friend class QPointer;
0027
0028 using QObjectType =
0029 typename std::conditional<std::is_const<T>::value, const QObject, QObject>::type;
0030 QWeakPointer<QObjectType> wp;
0031 public:
0032 Q_NODISCARD_CTOR
0033 QPointer() noexcept = default;
0034 Q_NODISCARD_CTOR
0035 constexpr QPointer(std::nullptr_t) noexcept : QPointer{} {}
0036 Q_WEAK_OVERLOAD
0037 Q_NODISCARD_CTOR
0038 inline QPointer(T *p) : wp(p, true) { }
0039
0040
0041
0042 template <typename X, if_convertible<X> = true>
0043 Q_NODISCARD_CTOR
0044 QPointer(QPointer<X> &&other) noexcept
0045 : wp(std::exchange(other.wp, nullptr).internalData(), true) {}
0046 template <typename X, if_convertible<X> = true>
0047 Q_NODISCARD_CTOR
0048 QPointer(const QPointer<X> &other) noexcept
0049 : wp(other.wp.internalData(), true) {}
0050
0051 template <typename X, if_convertible<X> = true>
0052 QPointer &operator=(const QPointer<X> &other) noexcept
0053 {
0054 QPointer(other).swap(*this);
0055 return *this;
0056 }
0057
0058 template <typename X, if_convertible<X> = true>
0059 QPointer &operator=(QPointer<X> &&other) noexcept
0060 {
0061 QPointer(std::move(other)).swap(*this);
0062 return *this;
0063 }
0064
0065 #ifdef Q_QDOC
0066
0067 ~QPointer();
0068 #endif
0069
0070 inline void swap(QPointer &other) noexcept { wp.swap(other.wp); }
0071
0072 inline QPointer<T> &operator=(T* p)
0073 { wp.assign(static_cast<QObjectType*>(p)); return *this; }
0074
0075 T* data() const noexcept
0076 { return static_cast<T*>(wp.internalData()); }
0077 T* get() const noexcept
0078 { return data(); }
0079 T* operator->() const noexcept
0080 { return data(); }
0081 T& operator*() const noexcept
0082 { return *data(); }
0083 operator T*() const noexcept
0084 { return data(); }
0085
0086 bool isNull() const noexcept
0087 { return wp.isNull(); }
0088 explicit operator bool() const noexcept { return !isNull(); }
0089
0090 void clear() noexcept
0091 { wp.clear(); }
0092
0093 friend void swap(QPointer &lhs, QPointer &rhs) noexcept
0094 { lhs.swap(rhs); }
0095
0096 private:
0097 template <typename X>
0098 friend bool comparesEqual(const QPointer &lhs, const QPointer<X> &rhs) noexcept
0099 { return lhs.data() == rhs.data(); }
0100 QT_DECLARE_EQUALITY_OPERATORS_HELPER(QPointer, QPointer<X>, ,
0101 noexcept(true), template <typename X>)
0102
0103 template <typename X>
0104 friend bool comparesEqual(const QPointer &lhs, X *rhs) noexcept
0105 { return lhs.data() == rhs; }
0106 Q_DECLARE_EQUALITY_COMPARABLE(QPointer, X*, template <typename X>)
0107
0108 friend bool comparesEqual(const QPointer &lhs, std::nullptr_t) noexcept
0109 { return lhs.isNull(); }
0110 Q_DECLARE_EQUALITY_COMPARABLE(QPointer, std::nullptr_t)
0111 };
0112 template <class T> Q_DECLARE_TYPEINFO_BODY(QPointer<T>, Q_RELOCATABLE_TYPE);
0113
0114 template<typename T>
0115 QPointer<T>
0116 qPointerFromVariant(const QVariant &variant)
0117 {
0118 const auto wp = QtSharedPointer::weakPointerFromVariant_internal(variant);
0119 return QPointer<T>{qobject_cast<T*>(QtPrivate::EnableInternalData::internalData(wp))};
0120 }
0121
0122 QT_END_NAMESPACE
0123
0124 #endif
0125
0126 #endif