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
13QT_BEGIN_NAMESPACE
14
15class QVariant;
16
17template <class T>
18class 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;
30public:
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(other&: *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(other&: *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 explicit operator bool() const noexcept { return !isNull(); }
88
89 void clear() noexcept
90 { wp.clear(); }
91
92 friend void swap(QPointer &lhs, QPointer &rhs) noexcept
93 { lhs.swap(other&: rhs); }
94
95private:
96 template <typename X>
97 friend bool comparesEqual(const QPointer &lhs, const QPointer<X> &rhs) noexcept
98 { return lhs.data() == rhs.data(); }
99 QT_DECLARE_EQUALITY_OPERATORS_HELPER(QPointer, QPointer<X>, /* non-constexpr */,
100 noexcept(true), template <typename X>)
101
102 template <typename X>
103 friend bool comparesEqual(const QPointer &lhs, X *rhs) noexcept
104 { return lhs.data() == rhs; }
105 Q_DECLARE_EQUALITY_COMPARABLE(QPointer, X*, template <typename X>)
106
107 friend bool comparesEqual(const QPointer &lhs, std::nullptr_t) noexcept
108 { return lhs.isNull(); }
109 Q_DECLARE_EQUALITY_COMPARABLE(QPointer, std::nullptr_t)
110};
111template <class T> Q_DECLARE_TYPEINFO_BODY(QPointer<T>, Q_RELOCATABLE_TYPE);
112
113template<typename T>
114QPointer<T>
115qPointerFromVariant(const QVariant &variant)
116{
117 const auto wp = QtSharedPointer::weakPointerFromVariant_internal(variant);
118 return QPointer<T>{qobject_cast<T*>(QtPrivate::EnableInternalData::internalData(p: wp))};
119}
120
121QT_END_NAMESPACE
122
123#endif // QT_NO_QOBJECT
124
125#endif // QPOINTER_H
126

source code of qtbase/src/corelib/kernel/qpointer.h