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
88 void clear() noexcept
89 { wp.clear(); }
90
91 friend void swap(QPointer &lhs, QPointer &rhs) noexcept
92 { lhs.swap(rhs); }
93
94private:
95 template <typename X>
96 friend bool comparesEqual(const QPointer &lhs, const QPointer<X> &rhs) noexcept
97 { return lhs.data() == rhs.data(); }
99 noexcept(true), template <typename X>)
100
101 template <typename X>
102 friend bool comparesEqual(const QPointer &lhs, X *rhs) noexcept
103 { return lhs.data() == rhs; }
104 Q_DECLARE_EQUALITY_COMPARABLE(QPointer, X*, template <typename X>)
105
106 friend bool comparesEqual(const QPointer &lhs, std::nullptr_t) noexcept
107 { return lhs.data() == nullptr; }
109};
111
112template<typename T>
113QPointer<T>
114qPointerFromVariant(const QVariant &variant)
115{
116 const auto wp = QtSharedPointer::weakPointerFromVariant_internal(variant);
117 return QPointer<T>{qobject_cast<T*>(QtPrivate::EnableInternalData::internalData(wp))};
118}
119
120QT_END_NAMESPACE
121
122#endif // QT_NO_QOBJECT
123
124#endif // QPOINTER_H
\inmodule QtCore
Definition qcoreevent.h:385
\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:46
\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:367
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)
static QString qAppFileName()
QList< QtStartUpFunction > QStartUpFuncList
void qt_startup_hook()
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)
#define qApp
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 Q_EVENT_DISABLE_COPY(Class)
Definition qcoreevent.h:14
#define Q_DECL_EVENT_COMMON(Class)
Definition qcoreevent.h:21
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
Q_DECLARE_TYPEINFO_BODY(QPointer< T >, Q_RELOCATABLE_TYPE)
QPointer< T > qPointerFromVariant(const QVariant &variant)
Definition qpointer.h:114
bool qThreadStorage_hasLocalData(const QScopedPointer< T *, U > &data)
void qThreadStorage_deleteLocalData(T *t)
void qThreadStorage_deleteLocalData(T **t)