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:
33 QPointer() noexcept = default;
35 constexpr QPointer(std::nullptr_t) noexcept : QPointer{} {}
36 Q_WEAK_OVERLOAD
37 Q_NODISCARD_CTOR
38 inline QPointer(T *p) : wp(p, true) { }
39 // compiler-generated copy/move ctor/assignment operators are fine!
40 // compiler-generated dtor is fine!
41
42 template <typename X, if_convertible<X> = true>
44 QPointer(QPointer<X> &&other) noexcept
45 : wp(std::exchange(other.wp, nullptr).internalData(), true) {}
46 template <typename X, if_convertible<X> = true>
48 QPointer(const QPointer<X> &other) noexcept
49 : wp(other.wp.internalData(), true) {}
50
51 template <typename X, if_convertible<X> = true>
52 QPointer &operator=(const QPointer<X> &other) noexcept
53 {
54 QPointer(other).swap(*this);
55 return *this;
56 }
57
58 template <typename X, if_convertible<X> = true>
60 {
61 QPointer(std::move(other)).swap(*this);
62 return *this;
63 }
64
65#ifdef Q_QDOC
66 // Stop qdoc from complaining about missing function
67 ~QPointer();
68#endif
69
70 inline void swap(QPointer &other) noexcept { wp.swap(other.wp); }
71
72 inline QPointer<T> &operator=(T* p)
73 { wp.assign(static_cast<QObjectType*>(p)); return *this; }
74
75 T* data() const noexcept
76 { return static_cast<T*>(wp.internalData()); }
77 T* get() const noexcept
78 { return data(); }
79 T* operator->() const noexcept
80 { return data(); }
81 T& operator*() const noexcept
82 { return *data(); }
83 operator T*() const noexcept
84 { return data(); }
85
86 bool isNull() const noexcept
87 { return wp.isNull(); }
88 explicit operator bool() const noexcept { return !isNull(); }
89
90 void clear() noexcept
91 { wp.clear(); }
92
93 friend void swap(QPointer &lhs, QPointer &rhs) noexcept
94 { lhs.swap(rhs); }
95
96private:
97 template <typename X>
98 friend bool comparesEqual(const QPointer &lhs, const QPointer<X> &rhs) noexcept
99 { return lhs.data() == rhs.data(); }
101 noexcept(true), template <typename X>)
102
103 template <typename X>
104 friend bool comparesEqual(const QPointer &lhs, X *rhs) noexcept
105 { return lhs.data() == rhs; }
106 Q_DECLARE_EQUALITY_COMPARABLE(QPointer, X*, template <typename X>)
107
108 friend bool comparesEqual(const QPointer &lhs, std::nullptr_t) noexcept
109 { return lhs.isNull(); }
111};
113
114template<typename T>
115QPointer<T>
116qPointerFromVariant(const QVariant &variant)
117{
118 const auto wp = QtSharedPointer::weakPointerFromVariant_internal(variant);
119 return QPointer<T>{qobject_cast<T*>(QtPrivate::EnableInternalData::internalData(wp))};
120}
121
122QT_END_NAMESPACE
123
124#endif // QT_NO_QOBJECT
125
126#endif // QPOINTER_H
\inmodule QtCore
Definition qcoreevent.h:391
\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.
\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
\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:373
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:116
void qThreadStorage_deleteLocalData(T *t)
bool qThreadStorage_hasLocalData(const std::unique_ptr< T *, U > &data)
void qThreadStorage_deleteLocalData(T **t)