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