Qt 6.x
The Qt SDK
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/qsharedpointer.h>
8#include <QtCore/qtypeinfo.h>
9
10#ifndef QT_NO_QOBJECT
11
13
14class QVariant;
15
16template <class T>
18{
19 static_assert(!std::is_pointer<T>::value, "QPointer's template type must not be a pointer type");
20
21 template <typename X>
22 using if_convertible = std::enable_if_t<std::is_convertible_v<X*, T*>, bool>;
23 template <typename X>
24 friend class QPointer;
25
26 using QObjectType =
27 typename std::conditional<std::is_const<T>::value, const QObject, QObject>::type;
29public:
31 QPointer() = default;
33 inline QPointer(T *p) : wp(p, true) { }
34 // compiler-generated copy/move ctor/assignment operators are fine!
35 // compiler-generated dtor is fine!
36
37 template <typename X, if_convertible<X> = true>
40 : wp(std::exchange(other.wp, nullptr).internalData(), true) {}
41 template <typename X, if_convertible<X> = true>
43 QPointer(const QPointer<X> &other) noexcept
44 : wp(other.wp.internalData(), true) {}
45
46#ifdef Q_QDOC
47 // Stop qdoc from complaining about missing function
48 ~QPointer();
49#endif
50
51 inline void swap(QPointer &other) noexcept { wp.swap(other.wp); }
52
54 { wp.assign(static_cast<QObjectType*>(p)); return *this; }
55
56 inline T* data() const
57 { return static_cast<T*>(wp.internalData()); }
58 inline T* get() const
59 { return data(); }
60 inline T* operator->() const
61 { return data(); }
62 inline T& operator*() const
63 { return *data(); }
64 inline operator T*() const
65 { return data(); }
66
67 inline bool isNull() const
68 { return wp.isNull(); }
69
70 inline void clear()
71 { wp.clear(); }
72
73#define DECLARE_COMPARE_SET(T1, A1, T2, A2) \
74 friend bool operator==(T1, T2) \
75 { return A1 == A2; } \
76 friend bool operator!=(T1, T2) \
77 { return A1 != A2; }
78
79#define DECLARE_TEMPLATE_COMPARE_SET(T1, A1, T2, A2) \
80 template <typename X> \
81 friend bool operator==(T1, T2) noexcept \
82 { return A1 == A2; } \
83 template <typename X> \
84 friend bool operator!=(T1, T2) noexcept \
85 { return A1 != A2; }
86
87 DECLARE_TEMPLATE_COMPARE_SET(const QPointer &p1, p1.data(), const QPointer<X> &p2, p2.data())
90 DECLARE_COMPARE_SET(const QPointer &p1, p1.data(), std::nullptr_t, nullptr)
91 DECLARE_COMPARE_SET(std::nullptr_t, nullptr, const QPointer &p2, p2.data())
92#undef DECLARE_COMPARE_SET
93#undef DECLARE_TEMPLATE_COMPARE_SET
94};
96
97template<typename T>
100{
102 return QPointer<T>{qobject_cast<T*>(QtPrivate::EnableInternalData::internalData(wp))};
103}
104
105template <class T>
106inline void swap(QPointer<T> &p1, QPointer<T> &p2) noexcept
107{ p1.swap(p2); }
108
110
111#endif // QT_NO_QOBJECT
112
113#endif // QPOINTER_H
\inmodule QtCore
Definition qobject.h:90
void swap(QPixmap &other) noexcept
Definition qpixmap.h:43
\inmodule QtCore
Definition qpointer.h:18
QPointer< T > & operator=(T *p)
Assignment operator.
Definition qpointer.h:53
void swap(QPointer &other) noexcept
Definition qpointer.h:51
T & operator*() const
Dereference operator; implements pointer semantics.
Definition qpointer.h:62
void clear()
Definition qpointer.h:70
Q_NODISCARD_CTOR QPointer()=default
Q_NODISCARD_CTOR QPointer(T *p)
Constructs a guarded pointer that points to the same object that p points to.
Definition qpointer.h:33
T * get() const
Definition qpointer.h:58
Q_NODISCARD_CTOR QPointer(const QPointer< X > &other) noexcept
Definition qpointer.h:43
T * data() const
Definition qpointer.h:56
T * operator->() const
Overloaded arrow operator; implements pointer semantics.
Definition qpointer.h:60
Q_NODISCARD_CTOR QPointer(QPointer< X > &&other) noexcept
Definition qpointer.h:39
bool isNull() const
Returns true if the referenced object has been destroyed or if there is no referenced object; otherwi...
Definition qpointer.h:67
\inmodule QtCore
Definition qvariant.h:64
\inmodule QtCore
void clear()
Clears this QWeakPointer object, dropping the reference that it may have had to the pointer.
bool isNull() const noexcept
Returns true if this object refers to \nullptr.
void swap(QWeakPointer &other) noexcept
QPixmap p2
QPixmap p1
[0]
Combined button and popup list for selecting options.
Q_CORE_EXPORT QWeakPointer< QObject > weakPointerFromVariant_internal(const QVariant &variant)
#define Q_NODISCARD_CTOR
static ControlElement< T > * ptr(QWidget *widget)
GLenum type
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat GLfloat p
[1]
#define DECLARE_TEMPLATE_COMPARE_SET(T1, A1, T2, A2)
Definition qpointer.h:79
QPointer< T > qPointerFromVariant(const QVariant &variant)
Definition qpointer.h:99
#define DECLARE_COMPARE_SET(T1, A1, T2, A2)
Definition qpointer.h:73
#define X(name)
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:145
#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS)
Definition qtypeinfo.h:150
QVariant variant
[1]
QSharedPointer< T > other(t)
[5]
this swap(other)
static T * internalData(const QWeakPointer< T > &p) noexcept