Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpointer.h
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5#ifndef QPOINTER_H
6#define QPOINTER_H
7
8#include <QtCore/qcompare.h>
9#include <QtCore/qsharedpointer.h>
10#include <QtCore/qtypeinfo.h>
11
12#ifndef QT_NO_QOBJECT
13
14#include <QtCore/qxptype_traits.h>
15
17
18class QVariant;
19
20template <class T>
22{
23 static_assert(!std::is_pointer<T>::value, "QPointer's template type must not be a pointer type");
24
25 template <typename X>
27 template <typename X>
28 friend class QPointer;
29
30 template <typename X>
31 using compilation_test = decltype(std::declval<T*>() == std::declval<X*>());
32
33 template <typename X>
35
36 using QObjectType =
37 typename std::conditional<std::is_const<T>::value, const QObject, QObject>::type;
38 QWeakPointer<QObjectType> wp;
39
40 template <typename U>
42public:
43 using element_type = T;
44
46 QPointer() noexcept = default;
48 constexpr QPointer(std::nullptr_t) noexcept : QPointer{} {}
49 Q_WEAK_OVERLOAD
50 Q_NODISCARD_CTOR
51 inline QPointer(T *p) : wp(p, true) { }
52 // compiler-generated copy/move ctor/assignment operators are fine!
53 // compiler-generated dtor is fine!
54
55 template <typename X, if_convertible<X> = true>
57 QPointer(QPointer<X> &&other) noexcept
58 : wp(std::exchange(other.wp, nullptr).internalData(), true) {}
59 template <typename X, if_convertible<X> = true>
61 QPointer(const QPointer<X> &other) noexcept
62 : wp(other.wp.internalData(), true) {}
63
64 template <typename X, if_convertible<X> = true>
65 QPointer &operator=(const QPointer<X> &other) noexcept
66 {
67 QPointer(other).swap(*this);
68 return *this;
69 }
70
71 template <typename X, if_convertible<X> = true>
72 QPointer &operator=(QPointer<X> &&other) noexcept
73 {
74 QPointer(std::move(other)).swap(*this);
75 return *this;
76 }
77
78#ifdef Q_QDOC
79 // Stop qdoc from complaining about missing function
80 ~QPointer();
81#endif
82
83 inline void swap(QPointer &other) noexcept { wp.swap(other.wp); }
84
85 inline QPointer<T> &operator=(T* p)
86 { wp.assign(static_cast<QObjectType*>(p)); return *this; }
87
88 T* data() const noexcept
89 { return static_cast<T*>(wp.internalData()); }
90 T* get() const noexcept
91 { return data(); }
92 T* operator->() const noexcept
93 { return data(); }
94 T& operator*() const noexcept
95 { return *data(); }
96 operator T*() const noexcept
97 { return data(); }
98
99 bool isNull() const noexcept
100 { return wp.isNull(); }
101 explicit operator bool() const noexcept { return !isNull(); }
102
103 void clear() noexcept
104 { wp.clear(); }
105
106 friend void swap(QPointer &lhs, QPointer &rhs) noexcept
107 { lhs.swap(rhs); }
108
109private:
110 QObjectType *internalData() const noexcept { return wp.internalData(); }
111
112 template <typename X>
113 friend bool comparesEqual(const QPointer &lhs, const QPointer<X> &rhs) noexcept
114 {
115 // ensure dropping to QObject* (see below) doesn't accept something we shouldn't:
116 static_assert(QPointer::is_comparable<X>::value, "cannot compare T* and X*"); // ### constrain?
117 // X is-a QObject(!):
118 // compare as QObject* to avoid potentially invalid QObject* → T* casts:
119 return comparesEqual(rhs, lhs.internalData()); // we're not friends with QPointer<X>...
120 }
121 QT_DECLARE_EQUALITY_OPERATORS_HELPER(QPointer, QPointer<X>, /* non-constexpr */,
122 noexcept(true), template <typename X>)
123
124 template <typename X>
125 friend bool comparesEqual(const QPointer &lhs, X *rhs) noexcept
126 {
127 if constexpr (std::is_base_of_v<QObject, X>) {
128 // ensure dropping to QObject* (see below) doesn't accept something we shouldn't:
129 static_assert(QPointer::is_comparable<X>::value, "cannot compare T* and X*");
130 // X is-a QObject:
131 // compare as QObject* to avoid potentially invalid QObject* → T* casts:
132 return lhs.internalData() == rhs;
133 } else {
134 // X is not-a QObject (e.g. a Q_INTERFACE): have to compare as T*
135 return lhs.data() == rhs;
136 }
137 }
138 Q_DECLARE_EQUALITY_COMPARABLE(QPointer, X*, template <typename X>)
139
140 friend bool comparesEqual(const QPointer &lhs, std::nullptr_t) noexcept
141 { return lhs.isNull(); }
143
144 template <typename X, typename U = T, if_qobject_castable<U> = true>
145 friend X qobject_cast(const QPointer &p)
146 {
147 return qobject_cast<X>(p.internalData());
148 }
149};
151
152template<typename T>
153QPointer<T>
154qPointerFromVariant(const QVariant &variant)
155{
156 const auto wp = QtSharedPointer::weakPointerFromVariant_internal(variant);
157 return QPointer<T>{qobject_cast<T*>(QtPrivate::EnableInternalData::internalData(wp))};
158}
159
160QT_END_NAMESPACE
161
162#endif // QT_NO_QOBJECT
163
164#endif // QPOINTER_H
\inmodule QtCore
Definition qcoreevent.h:389
\inmodule QtCore
int scopeLevel() const
int loopLevel() const
QDeferredDeleteEvent(int loopLevel, int scopeLevel)
Constructs a deferred delete event with the given loop and scope level.
QDisconnectNotifyEvent(int signalIndex)
\inmodule QtCore
Definition qeventloop.h:60
friend void swap(QEventLoopLocker &lhs, QEventLoopLocker &rhs) noexcept
Definition qeventloop.h:72
Q_CORE_EXPORT ~QEventLoopLocker()
Destroys this event loop locker object.
void swap(QEventLoopLocker &other) noexcept
Definition qeventloop.h:71
\inmodule QtCore
Definition qeventloop.h:17
\inmodule QtCore
Definition qcoreevent.h:50
T * operator->() const noexcept
Definition qpointer.h:92
Q_WEAK_OVERLOAD Q_NODISCARD_CTOR QPointer(T *p)
Definition qpointer.h:51
QPointer< T > & operator=(T *p)
Definition qpointer.h:85
void swap(QPointer &other) noexcept
Definition qpointer.h:83
void clear() noexcept
Definition qpointer.h:103
T & operator*() const noexcept
Definition qpointer.h:94
T * data() const noexcept
Definition qpointer.h:88
bool isNull() const noexcept
Definition qpointer.h:99
QPointer & operator=(QPointer< X > &&other) noexcept
Definition qpointer.h:72
operator bool() const noexcept
Definition qpointer.h:101
QPointer & operator=(const QPointer< X > &other) noexcept
Definition qpointer.h:65
friend void swap(QPointer &lhs, QPointer &rhs) noexcept
Definition qpointer.h:106
T element_type
Definition qpointer.h:43
T * get() const noexcept
Definition qpointer.h:90
friend bool comparesEqual(const QPointer &lhs, const QPointer< X > &rhs) noexcept
Definition qpointer.h:113
operator T*() const noexcept
Definition qpointer.h:96
QSpanCommon< T > Base
Definition qspan.h:177
constexpr QSpanBase(It first, qsizetype count)
Definition qspan.h:192
static constexpr qsizetype m_size
Definition qspan.h:181
constexpr QSpanBase(It first, End last)
Definition qspan.h:199
Definition qspan.h:320
constexpr bool empty() const noexcept
Definition qspan.h:372
constexpr pointer data() const noexcept
Definition qspan.h:379
static constexpr std::size_t extent
Definition qspan.h:350
constexpr reverse_iterator rbegin() const noexcept
Definition qspan.h:386
constexpr reference front() const
Definition qspan.h:377
constexpr QSpan< T > sliced(size_type pos, size_type n) const
Definition qspan.h:439
constexpr QSpan< T > sliced(size_type pos) const
Definition qspan.h:438
const T * const_pointer
Definition qspan.h:343
constexpr size_type size() const noexcept
Definition qspan.h:370
const_pointer const_iterator
Definition qspan.h:347
T & reference
Definition qspan.h:344
constexpr iterator end() const noexcept
Definition qspan.h:383
constexpr const_iterator cbegin() const noexcept
Definition qspan.h:384
constexpr QSpan< T > subspan(size_type pos, size_type n) const
Definition qspan.h:433
constexpr const_iterator cend() const noexcept
Definition qspan.h:385
constexpr bool isEmpty() const noexcept
Definition qspan.h:436
constexpr reverse_iterator rend() const noexcept
Definition qspan.h:387
constexpr QSpan< T, Count > last() const noexcept(subspan_always_succeeds_v< Count >)
Definition qspan.h:403
T * pointer
Definition qspan.h:342
constexpr QSpan< T > last(size_type n) const
Definition qspan.h:431
constexpr const_reverse_iterator crend() const noexcept
Definition qspan.h:389
pointer iterator
Definition qspan.h:346
friend QSpan< const std::byte, E==q20::dynamic_extent ? q20::dynamic_extent :E *sizeof(T)> as_bytes(QSpan s) noexcept
Definition qspan.h:467
constexpr size_type size_bytes() const noexcept
Definition qspan.h:371
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition qspan.h:349
constexpr const_reverse_iterator crbegin() const noexcept
Definition qspan.h:388
constexpr reference operator[](size_type idx) const
Definition qspan.h:375
constexpr QSpan< T > first(size_type n) const
Definition qspan.h:430
constexpr reference back() const
Definition qspan.h:378
constexpr QSpan< T > subspan(size_type pos) const
Definition qspan.h:432
constexpr auto subspan() const noexcept(subspan_always_succeeds_v< Offset+Count >)
Definition qspan.h:426
constexpr QSpan< T, Count > first() const noexcept(subspan_always_succeeds_v< Count >)
Definition qspan.h:393
const T & const_reference
Definition qspan.h:345
std::reverse_iterator< iterator > reverse_iterator
Definition qspan.h:348
constexpr QSpan< T > chopped(size_type n) const
Definition qspan.h:440
friend QSpan< std::byte, E==q20::dynamic_extent ? q20::dynamic_extent :E *sizeof(T)> as_writable_bytes(QSpan s) noexcept
Definition qspan.h:481
constexpr iterator begin() const noexcept
Definition qspan.h:382
constexpr auto subspan() const noexcept(subspan_always_succeeds_v< Offset >)
Definition qspan.h:413
\inmodule QtCore
void setLocalData(T t)
Sets the local data for the calling thread to data.
~QThreadStorage()=default
Destroys the per-thread data storage object.
QThreadStorage()=default
Constructs a new per-thread data storage object.
T localData() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & localData()
Returns a reference to the data that was set by the calling thread.
QThreadStorage(const QThreadStorage &rhs)=delete
bool hasLocalData() const
If T is a pointer type, returns true if the calling thread has non-zero data available.
QThreadStorage & operator=(const QThreadStorage &rhs)=delete
\inmodule QtCore
Definition qcoreevent.h:371
AdlTester::size_result< Range > adl_size(Range &&r)
Definition qspan.h:103
std::conditional_t< std::is_const_v< From >, const To &, To & > const_propagated(To &in)
Definition qspan.h:50
decltype(QSpanPrivate::adl_begin(std::declval< Range & >())) iterator_t
Definition qspan.h:108
AdlTester::data_result< Range > adl_data(Range &&r)
Definition qspan.h:101
AdlTester::begin_result< Range > adl_begin(Range &&r)
Definition qspan.h:99
constexpr bool is_qualification_conversion_v
Definition qspan.h:83
std::is_convertible< From(*)[], To(*)[]> is_qualification_conversion
Definition qspan.h:80
Combined button and popup list for selecting options.
constexpr auto dynamic_extent
Definition qspan.h:27
#define __has_include(x)
QString qAppName()
Q_TRACE_POINT(qtcore, QCoreApplication_notify_exit, bool consumed, bool filtered)
void QT_MANGLE_NAMESPACE qt_startup_hook()
static QString qAppFileName()
QList< QtStartUpFunction > QStartUpFuncList
Q_TRACE_METADATA(qtcore, "ENUM { AUTO, RANGE User ... MaxUser } QEvent::Type;")
QString qtTrId(const char *id, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_exit)
static void qt_call_pre_routines()
qsizetype qGlobalPostedEventsCount()
static Q_CONSTINIT bool preRoutinesCalled
QList< QtCleanUpFunction > QVFuncList
static void replacePercentN(QString *result, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_event_compressed, QObject *receiver, QEvent *event)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_entry, QObject *receiver, QEvent *event, QEvent::Type type)
Q_TRACE_PREFIX(qtcore, "#include <qcoreevent.h>")
Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction)
void(*) QtStartUpFunction()
void(*) QtCleanUpFunction()
Q_CORE_EXPORT void qAddPreRoutine(QtStartUpFunction)
Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction)
#define qApp
#define Q_EVENT_DISABLE_COPY(Class)
Definition qcoreevent.h:17
#define Q_DECL_EVENT_COMMON(Class)
Definition qcoreevent.h:24
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
Q_DECLARE_TYPEINFO_BODY(QPointer< T >, Q_RELOCATABLE_TYPE)
QPointer< T > qPointerFromVariant(const QVariant &variant)
Definition qpointer.h:154
#define MAKE_ADL_TEST(what)
Definition qspan.h:86
QSpan(const std::array< T, N > &) -> QSpan< const T, N >
#define QT_ONLY_IF_DYNAMIC_SPAN(DECL)
Definition qspan.h:446
QSpan(T(&)[N]) -> QSpan< T, N >
QSpan(std::array< T, N > &) -> QSpan< T, N >
void qThreadStorage_deleteLocalData(T *t)
bool qThreadStorage_hasLocalData(const std::unique_ptr< T *, U > &data)
void qThreadStorage_deleteLocalData(T **t)