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
15
16class QVariant;
17
18template <class T>
20{
21 static_assert(!std::is_pointer<T>::value, "QPointer's template type must not be a pointer type");
22
23 template <typename X>
25 template <typename X>
26 friend class QPointer;
27
28 using QObjectType =
29 typename std::conditional<std::is_const<T>::value, const QObject, QObject>::type;
30 QWeakPointer<QObjectType> wp;
31public:
32 using element_type = T;
33
35 QPointer() noexcept = default;
37 constexpr QPointer(std::nullptr_t) noexcept : QPointer{} {}
38 Q_WEAK_OVERLOAD
39 Q_NODISCARD_CTOR
40 inline QPointer(T *p) : wp(p, true) { }
41 // compiler-generated copy/move ctor/assignment operators are fine!
42 // compiler-generated dtor is fine!
43
44 template <typename X, if_convertible<X> = true>
46 QPointer(QPointer<X> &&other) noexcept
47 : wp(std::exchange(other.wp, nullptr).internalData(), true) {}
48 template <typename X, if_convertible<X> = true>
50 QPointer(const QPointer<X> &other) noexcept
51 : wp(other.wp.internalData(), true) {}
52
53 template <typename X, if_convertible<X> = true>
54 QPointer &operator=(const QPointer<X> &other) noexcept
55 {
56 QPointer(other).swap(*this);
57 return *this;
58 }
59
60 template <typename X, if_convertible<X> = true>
62 {
63 QPointer(std::move(other)).swap(*this);
64 return *this;
65 }
66
67#ifdef Q_QDOC
68 // Stop qdoc from complaining about missing function
69 ~QPointer();
70#endif
71
72 inline void swap(QPointer &other) noexcept { wp.swap(other.wp); }
73
74 inline QPointer<T> &operator=(T* p)
75 { wp.assign(static_cast<QObjectType*>(p)); return *this; }
76
77 T* data() const noexcept
78 { return static_cast<T*>(wp.internalData()); }
79 T* get() const noexcept
80 { return data(); }
81 T* operator->() const noexcept
82 { return data(); }
83 T& operator*() const noexcept
84 { return *data(); }
85 operator T*() const noexcept
86 { return data(); }
87
88 bool isNull() const noexcept
89 { return wp.isNull(); }
90 explicit operator bool() const noexcept { return !isNull(); }
91
92 void clear() noexcept
93 { wp.clear(); }
94
95 friend void swap(QPointer &lhs, QPointer &rhs) noexcept
96 { lhs.swap(rhs); }
97
98private:
99 template <typename X>
100 friend bool comparesEqual(const QPointer &lhs, const QPointer<X> &rhs) noexcept
101 { return lhs.data() == rhs.data(); }
103 noexcept(true), template <typename X>)
104
105 template <typename X>
106 friend bool comparesEqual(const QPointer &lhs, X *rhs) noexcept
107 { return lhs.data() == rhs; }
108 Q_DECLARE_EQUALITY_COMPARABLE(QPointer, X*, template <typename X>)
109
110 friend bool comparesEqual(const QPointer &lhs, std::nullptr_t) noexcept
111 { return lhs.isNull(); }
113};
115
116template<typename T>
117QPointer<T>
118qPointerFromVariant(const QVariant &variant)
119{
120 const auto wp = QtSharedPointer::weakPointerFromVariant_internal(variant);
121 return QPointer<T>{qobject_cast<T*>(QtPrivate::EnableInternalData::internalData(wp))};
122}
123
124QT_END_NAMESPACE
125
126#endif // QT_NO_QOBJECT
127
128#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
Q_CORE_EXPORT ~QEventLoopLocker()
Destroys this event loop locker object.
\inmodule QtCore
Definition qeventloop.h:17
\inmodule QtCore
Definition qcoreevent.h:50
T element_type
Definition qpointer.h:32
\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
Combined button and popup list for selecting options.
#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(* QtCleanUpFunction)()
void(* QtStartUpFunction)()
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:118
void qThreadStorage_deleteLocalData(T *t)
bool qThreadStorage_hasLocalData(const std::unique_ptr< T *, U > &data)
void qThreadStorage_deleteLocalData(T **t)