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
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
14
15class QVariant;
16
17template <class T>
19{
20 static_assert(!std::is_pointer<T>::value, "QPointer's template type must not be a pointer type");
21
22 template <typename X>
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:
32 QPointer() noexcept = default;
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>
43 QPointer(QPointer<X> &&other) noexcept
44 : wp(std::exchange(other.wp, nullptr).internalData(), true) {}
45 template <typename X, if_convertible<X> = true>
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(*this);
54 return *this;
55 }
56
57 template <typename X, if_convertible<X> = true>
59 {
60 QPointer(std::move(other)).swap(*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(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(); }
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(); }
110};
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(wp))};
119}
120
121QT_END_NAMESPACE
122
123#endif // QT_NO_QOBJECT
124
125#endif // QPOINTER_H
\inmodule QtCore
Definition qcoreevent.h:390
\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:59
Q_CORE_EXPORT ~QEventLoopLocker()
Destroys this event loop locker object.
\inmodule QtCore
Definition qeventloop.h:16
\inmodule QtCore
Definition qcoreevent.h:49
\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:372
#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)
QString qTrId(const char *id, int n)
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:16
#define Q_DECL_EVENT_COMMON(Class)
Definition qcoreevent.h:23
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
Q_DECLARE_TYPEINFO_BODY(QPointer< T >, Q_RELOCATABLE_TYPE)
QPointer< T > qPointerFromVariant(const QVariant &variant)
Definition qpointer.h:115
void qThreadStorage_deleteLocalData(T *t)
bool qThreadStorage_hasLocalData(const std::unique_ptr< T *, U > &data)
void qThreadStorage_deleteLocalData(T **t)