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
qscopedpointer.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 QSCOPEDPOINTER_H
6#define QSCOPEDPOINTER_H
7
8#include <QtCore/qglobal.h>
9
10#include <stdlib.h>
11
12QT_BEGIN_NAMESPACE
13
14template <typename T>
15struct QScopedPointerDeleter
16{
17 static inline void cleanup(T *pointer) noexcept
18 {
19 // Enforce a complete type.
20 // If you get a compile error here, read the section on forward declared
21 // classes in the QScopedPointer documentation.
22 typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
23 (void) sizeof(IsIncompleteType);
24
25 delete pointer;
26 }
27 void operator()(T *pointer) const noexcept
28 {
29 cleanup(pointer);
30 }
31};
32
33template <typename T>
35{
36 static inline void cleanup(T *pointer) noexcept
37 {
38 // Enforce a complete type.
39 // If you get a compile error here, read the section on forward declared
40 // classes in the QScopedPointer documentation.
41 typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
42 (void) sizeof(IsIncompleteType);
43
44 delete[] pointer;
45 }
46 void operator()(T *pointer) const noexcept
47 {
48 cleanup(pointer);
49 }
50};
51
53{
54 static inline void cleanup(void *pointer) noexcept { free(pointer); }
55 void operator()(void *pointer) const noexcept { cleanup(pointer); }
56};
57
58#ifndef QT_NO_QOBJECT
59template <typename T>
61{
62 static inline void cleanup(T *pointer) { if (pointer) pointer->deleteLater(); }
63 void operator()(T *pointer) const { cleanup(pointer); }
64};
65
66class QObject;
68#endif
69
70template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
72{
73public:
74 using element_type = T;
75
77 explicit QScopedPointer(T *p = nullptr) noexcept : d(p)
78 {
79 }
80
82 {
83 T *oldD = this->d;
84 Cleanup::cleanup(oldD);
85 }
86
88 {
89 Q_ASSERT(d);
90 return *d;
91 }
92
93 T *operator->() const noexcept
94 {
95 return d;
96 }
97
98 bool operator!() const noexcept
99 {
100 return !d;
101 }
102
103 explicit operator bool() const
104 {
105 return !isNull();
106 }
107
108 T *data() const noexcept
109 {
110 return d;
111 }
112
113 T *get() const noexcept
114 {
115 return d;
116 }
117
118 bool isNull() const noexcept
119 {
120 return !d;
121 }
122
123 void reset(T *other = nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval<T *>())))
124 {
125 if (d == other)
126 return;
127 T *oldD = std::exchange(d, other);
128 Cleanup::cleanup(oldD);
129 }
130
131#if QT_DEPRECATED_SINCE(6, 1)
132 QT_DEPRECATED_VERSION_X_6_1("Use std::unique_ptr instead, and call release().")
133 T *take() noexcept
134 {
135 T *oldD = std::exchange(d, nullptr);
136 return oldD;
137 }
138#endif
139
140#if QT_DEPRECATED_SINCE(6, 2)
141 QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedPointer.")
142 void swap(QScopedPointer<T, Cleanup> &other) noexcept
143 {
145 }
146#endif
147
148 typedef T *pointer;
149
150 friend bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs) noexcept
151 {
152 return lhs.data() == rhs.data();
153 }
154
155 friend bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs) noexcept
156 {
157 return lhs.data() != rhs.data();
158 }
159
160 friend bool operator==(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t) noexcept
161 {
162 return lhs.isNull();
163 }
164
165 friend bool operator==(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs) noexcept
166 {
167 return rhs.isNull();
168 }
169
170 friend bool operator!=(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t) noexcept
171 {
172 return !lhs.isNull();
173 }
174
175 friend bool operator!=(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs) noexcept
176 {
177 return !rhs.isNull();
178 }
179
180#if QT_DEPRECATED_SINCE(6, 2)
181 QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedPointer.")
182 friend void swap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2) noexcept
183 { p1.swap(p2); }
184#endif
185
186protected:
187 T *d;
188
189private:
191};
192
193template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
194class QScopedArrayPointer : public QScopedPointer<T, Cleanup>
195{
196 template <typename Ptr>
197 using if_same_type = typename std::enable_if<std::is_same<typename std::remove_cv<T>::type, Ptr>::value, bool>::type;
198public:
201 inline ~QScopedArrayPointer() = default;
202
203 template <typename D, if_same_type<D> = true>
207 {
208 }
209
210 T &operator[](qsizetype i)
211 {
212 return this->d[i];
213 }
214
215 const T &operator[](qsizetype i) const
216 {
217 return this->d[i];
218 }
219
220#if QT_DEPRECATED_SINCE(6, 2)
221 QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedArrayPointer.")
222 void swap(QScopedArrayPointer &other) noexcept // prevent QScopedPointer <->QScopedArrayPointer swaps
224#endif
225
226private:
227 explicit inline QScopedArrayPointer(void *)
228 {
229 // Enforce the same type.
230
231 // If you get a compile error here, make sure you declare
232 // QScopedArrayPointer with the same template type as you pass to the
233 // constructor. See also the QScopedPointer documentation.
234
235 // Storing a scalar array as a pointer to a different type is not
236 // allowed and results in undefined behavior.
237 }
238
240};
241
242#if QT_DEPRECATED_SINCE(6, 2)
243template <typename T, typename Cleanup>
244QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedArrayPointer.")
245inline void swap(QScopedArrayPointer<T, Cleanup> &lhs, QScopedArrayPointer<T, Cleanup> &rhs) noexcept
246{ lhs.swap(rhs); }
247#endif
248
249QT_END_NAMESPACE
250
251#endif // QSCOPEDPOINTER_H
QPlatformFontDatabase * fontDatabase() const override
Accessor for the platform integration's fontdatabase.
QAbstractEventDispatcher * createEventDispatcher() const override
Factory function for the GUI event dispatcher.
unsigned options() const
QPlatformBackingStore * createPlatformBackingStore(QWindow *window) const override
Factory function for QPlatformBackingStore.
QMinimalIntegration(const QStringList &parameters)
QPlatformNativeInterface * nativeInterface() const override
static QMinimalIntegration * instance()
bool hasCapability(QPlatformIntegration::Capability cap) const override
QPlatformWindow * createPlatformWindow(QWindow *window) const override
Factory function for QPlatformWindow.
QRect geometry() const override
Reimplement in subclass to return the pixel geometry of the screen.
int depth() const override
Reimplement in subclass to return current depth of the screen.
QImage::Format mFormat
QImage::Format format() const override
Reimplement in subclass to return the image format which corresponds to the screen format.
~QScopedArrayPointer()=default
const T & operator[](qsizetype i) const
T & operator[](qsizetype i)
friend bool operator==(std::nullptr_t, const QScopedPointer< T, Cleanup > &rhs) noexcept
bool operator!() const noexcept
friend bool operator==(const QScopedPointer< T, Cleanup > &lhs, std::nullptr_t) noexcept
std::add_lvalue_reference_t< T > operator*() const
T * operator->() const noexcept
bool isNull() const noexcept
T * get() const noexcept
void reset(T *other=nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval< T * >())))
friend bool operator!=(std::nullptr_t, const QScopedPointer< T, Cleanup > &rhs) noexcept
friend bool operator!=(const QScopedPointer< T, Cleanup > &lhs, std::nullptr_t) noexcept
T * data() const noexcept
friend bool operator!=(const QScopedPointer< T, Cleanup > &lhs, const QScopedPointer< T, Cleanup > &rhs) noexcept
friend bool operator==(const QScopedPointer< T, Cleanup > &lhs, const QScopedPointer< T, Cleanup > &rhs) noexcept
Combined button and popup list for selecting options.
QScopedPointerObjectDeleteLater< QObject > QScopedPointerDeleteLater
static void cleanup(T *pointer) noexcept
void operator()(T *pointer) const noexcept
void operator()(T *pointer) const
static void cleanup(T *pointer)
void operator()(void *pointer) const noexcept
static void cleanup(void *pointer) noexcept