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
4#ifndef QSCOPEDPOINTER_H
5#define QSCOPEDPOINTER_H
6
7#include <QtCore/qglobal.h>
8
9#include <stdlib.h>
10
12
13template <typename T>
14struct QScopedPointerDeleter
15{
16 static inline void cleanup(T *pointer) noexcept
17 {
18 // Enforce a complete type.
19 // If you get a compile error here, read the section on forward declared
20 // classes in the QScopedPointer documentation.
21 typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
22 (void) sizeof(IsIncompleteType);
23
24 delete pointer;
25 }
26 void operator()(T *pointer) const noexcept
27 {
28 cleanup(pointer);
29 }
30};
31
32template <typename T>
34{
35 static inline void cleanup(T *pointer) noexcept
36 {
37 // Enforce a complete type.
38 // If you get a compile error here, read the section on forward declared
39 // classes in the QScopedPointer documentation.
40 typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
41 (void) sizeof(IsIncompleteType);
42
43 delete[] pointer;
44 }
45 void operator()(T *pointer) const noexcept
46 {
47 cleanup(pointer);
48 }
49};
50
52{
53 static inline void cleanup(void *pointer) noexcept { free(pointer); }
54 void operator()(void *pointer) const noexcept { cleanup(pointer); }
55};
56
57#ifndef QT_NO_QOBJECT
58template <typename T>
60{
61 static inline void cleanup(T *pointer) { if (pointer) pointer->deleteLater(); }
62 void operator()(T *pointer) const { cleanup(pointer); }
63};
64
65class QObject;
67#endif
68
69template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
71{
72public:
74 explicit QScopedPointer(T *p = nullptr) noexcept : d(p)
75 {
76 }
77
79 {
80 T *oldD = this->d;
81 Cleanup::cleanup(oldD);
82 }
83
84 inline T &operator*() const
85 {
86 Q_ASSERT(d);
87 return *d;
88 }
89
90 T *operator->() const noexcept
91 {
92 return d;
93 }
94
95 bool operator!() const noexcept
96 {
97 return !d;
98 }
99
100 explicit operator bool() const
101 {
102 return !isNull();
103 }
104
105 T *data() const noexcept
106 {
107 return d;
108 }
109
110 T *get() const noexcept
111 {
112 return d;
113 }
114
115 bool isNull() const noexcept
116 {
117 return !d;
118 }
119
120 void reset(T *other = nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval<T *>())))
121 {
122 if (d == other)
123 return;
124 T *oldD = std::exchange(d, other);
125 Cleanup::cleanup(oldD);
126 }
127
128#if QT_DEPRECATED_SINCE(6, 1)
129 QT_DEPRECATED_VERSION_X_6_1("Use std::unique_ptr instead, and call release().")
130 T *take() noexcept
131 {
132 T *oldD = std::exchange(d, nullptr);
133 return oldD;
134 }
135#endif
136
137#if QT_DEPRECATED_SINCE(6, 2)
138 QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedPointer.")
139 void swap(QScopedPointer<T, Cleanup> &other) noexcept
140 {
142 }
143#endif
144
145 typedef T *pointer;
146
147 friend bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs) noexcept
148 {
149 return lhs.data() == rhs.data();
150 }
151
152 friend bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs) noexcept
153 {
154 return lhs.data() != rhs.data();
155 }
156
157 friend bool operator==(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t) noexcept
158 {
159 return lhs.isNull();
160 }
161
162 friend bool operator==(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs) noexcept
163 {
164 return rhs.isNull();
165 }
166
167 friend bool operator!=(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t) noexcept
168 {
169 return !lhs.isNull();
170 }
171
172 friend bool operator!=(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs) noexcept
173 {
174 return !rhs.isNull();
175 }
176
177#if QT_DEPRECATED_SINCE(6, 2)
178 QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedPointer.")
179 friend void swap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2) noexcept
180 { p1.swap(p2); }
181#endif
182
183protected:
184 T *d;
185
186private:
188};
189
190template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
191class QScopedArrayPointer : public QScopedPointer<T, Cleanup>
192{
193 template <typename Ptr>
194 using if_same_type = typename std::enable_if<std::is_same<typename std::remove_cv<T>::type, Ptr>::value, bool>::type;
195public:
198 inline ~QScopedArrayPointer() = default;
199
200 template <typename D, if_same_type<D> = true>
204 {
205 }
206
207 T &operator[](qsizetype i)
208 {
209 return this->d[i];
210 }
211
212 const T &operator[](qsizetype i) const
213 {
214 return this->d[i];
215 }
216
217#if QT_DEPRECATED_SINCE(6, 2)
218 QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedArrayPointer.")
219 void swap(QScopedArrayPointer &other) noexcept // prevent QScopedPointer <->QScopedArrayPointer swaps
221#endif
222
223private:
224 explicit inline QScopedArrayPointer(void *)
225 {
226 // Enforce the same type.
227
228 // If you get a compile error here, make sure you declare
229 // QScopedArrayPointer with the same template type as you pass to the
230 // constructor. See also the QScopedPointer documentation.
231
232 // Storing a scalar array as a pointer to a different type is not
233 // allowed and results in undefined behavior.
234 }
235
237};
238
239#if QT_DEPRECATED_SINCE(6, 2)
240template <typename T, typename Cleanup>
241QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedArrayPointer.")
242inline void swap(QScopedArrayPointer<T, Cleanup> &lhs, QScopedArrayPointer<T, Cleanup> &rhs) noexcept
243{ lhs.swap(rhs); }
244#endif
245
246QT_END_NAMESPACE
247
248#endif // QSCOPEDPOINTER_H
\inmodule QtCore
QDataStream & operator>>(QDataStream &in, QByteArray &ba)
Reads a byte array into ba from the stream in and returns a reference to the stream.
quint16 qChecksum(QByteArrayView data, Qt::ChecksumType standard)
Definition qlist.h:80
static QLatin1StringView scriptToCode(QLocale::Script script)
Definition qlocale.cpp:254
QString toUpper(const QString &str, bool *ok) const
static const QLocalePrivate * get(const QLocale &l)
Definition qlocale_p.h:541
QString toLower(const QString &str, bool *ok) const
quint16 languageId() const
Definition qlocale_p.h:526
static QLocale::Language codeToLanguage(QStringView code, QLocale::LanguageCodeTypes codeTypes=QLocale::AnyLanguageCode) noexcept
Definition qlocale.cpp:109
const QLocaleData *const m_data
Definition qlocale_p.h:559
QLatin1StringView scriptCode() const
Definition qlocale_p.h:536
QLocale::MeasurementSystem measurementSystem() const
Definition qlocale.cpp:3556
quint16 territoryId() const
Definition qlocale_p.h:527
static QLatin1StringView territoryToCode(QLocale::Territory territory)
Definition qlocale.cpp:262
qsizetype m_index
Definition qlocale_p.h:561
static QLocale::Territory codeToTerritory(QStringView code) noexcept
Definition qlocale.cpp:210
static std::array< char, 4 > languageToCode(QLocale::Language language, QLocale::LanguageCodeTypes codeTypes=QLocale::AnyLanguageCode)
Definition qlocale.cpp:229
static QLocale::Script codeToScript(QStringView code) noexcept
Definition qlocale.cpp:204
QLocale::NumberOptions m_numberOptions
Definition qlocale_p.h:562
QLatin1StringView territoryCode() const
Definition qlocale_p.h:538
constexpr QLocalePrivate(const QLocaleData *data, qsizetype index, QLocale::NumberOptions numberOptions=QLocale::DefaultNumberOptions, int refs=0)
Definition qlocale_p.h:520
std::array< char, 4 > languageCode(QLocale::LanguageCodeTypes codeTypes=QLocale::AnyLanguageCode) const
Definition qlocale_p.h:532
static QBasicAtomicInt s_generation
Definition qlocale_p.h:564
QByteArray bcp47Name(char separator='-') const
Definition qlocale.cpp:489
QBasicAtomicInt ref
Definition qlocale_p.h:560
~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
T & operator*() const
friend bool operator==(const QScopedPointer< T, Cleanup > &lhs, std::nullptr_t) noexcept
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
qsizetype fallbackLocaleIndex() const
Definition qlocale_p.h:568
@ StringToAlternateQuotation
Definition qlocale_p.h:162
@ DateTimeToStringShort
Definition qlocale_p.h:150
@ StandaloneMonthNameLong
Definition qlocale_p.h:168
@ ListToSeparatedString
Definition qlocale_p.h:164
@ StandaloneDayNameNarrow
Definition qlocale_p.h:173
@ StandaloneMonthNameNarrow
Definition qlocale_p.h:170
@ StringToStandardQuotation
Definition qlocale_p.h:161
@ StandaloneDayNameShort
Definition qlocale_p.h:172
@ StandaloneDayNameLong
Definition qlocale_p.h:171
@ StandaloneMonthNameShort
Definition qlocale_p.h:169
virtual QLocale fallbackLocale() const
virtual QVariant query(QueryType type, QVariant &&in=QVariant()) const
Combined button and popup list for selecting options.
constexpr char ascii_space_chars[]
Definition qlocale_p.h:71
constexpr auto makeCharacterSetMatch() noexcept
Definition qlocale_p.h:80
static constexpr bool isLowerCaseAscii(char c)
static const quint16 crc_tbl[16]
QByteArray qCompress(const uchar *data, qsizetype nbytes, int compressionLevel)
ZLibOp
@ Decompression
static Q_DECL_COLD_FUNCTION const char * zlibOpAsString(ZLibOp op)
static QByteArray toCase_template(T &input, uchar(*lookup)(uchar))
static void q_fromPercentEncoding(QByteArray *ba, char percent)
int qstrnicmp(const char *str1, qsizetype len1, const char *str2, qsizetype len2)
static qsizetype lastIndexOfHelper(const char *haystack, qsizetype l, const char *needle, qsizetype ol, qsizetype from)
static constexpr bool isUpperCaseAscii(char c)
static QByteArray xxflate(ZLibOp op, QArrayDataPointer< char > out, QByteArrayView input, qxp::function_ref< int(z_stream *) const > init, qxp::function_ref< int(z_stream *, size_t) const > processChunk, qxp::function_ref< void(z_stream *) const > deinit)
static constexpr uchar asciiLower(uchar c)
static qsizetype countCharHelper(QByteArrayView haystack, char needle) noexcept
static constexpr uchar asciiUpper(uchar c)
Q_CORE_EXPORT char * qstrncpy(char *dst, const char *src, size_t len)
Q_CORE_EXPORT int qstrnicmp(const char *, const char *, size_t len)
Q_CORE_EXPORT int qstricmp(const char *, const char *)
Q_CORE_EXPORT char * qstrdup(const char *)
Q_CORE_EXPORT char * qstrcpy(char *dst, const char *src)
Q_DECL_PURE_FUNCTION Q_CORE_EXPORT const void * qmemrchr(const void *s, int needle, size_t n) noexcept
Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2)
#define __has_include(x)
#define __has_feature(x)
bool qt_splitLocaleName(QStringView name, QStringView *lang=nullptr, QStringView *script=nullptr, QStringView *cntry=nullptr) noexcept
Definition qlocale.cpp:649
Q_DECLARE_TYPEINFO(QLocaleData::GroupSizes, Q_PRIMITIVE_TYPE)
Q_DECLARE_TYPEINFO(QLocaleId, Q_PRIMITIVE_TYPE)
qsizetype qt_repeatCount(QStringView s) noexcept
Definition qlocale.cpp:762
constexpr bool ascii_isspace(uchar c) noexcept
Definition qlocale_p.h:587
#define ForEachQLocaleRange(X)
Definition qlocale_p.h:471
QString qt_readEscapedFormatString(QStringView format, qsizetype *idx)
Definition qlocale.cpp:712
StrayCharacterMode
@ TrailingJunkAllowed
@ TrailingJunkProhibited
@ WhitespacesAllowed
Q_CORE_EXPORT double qstrntod(const char *s00, qsizetype len, char const **se, bool *ok)
double qstrtod(const char *s00, char const **se, bool *ok)
QSimpleParsedNumber< double > qt_asciiToDouble(const char *num, qsizetype numLen, StrayCharacterMode strayCharMode=TrailingJunkProhibited)
QByteArray qdtoAscii(double d, QLocaleData::DoubleForm form, int precision, bool uppercase)
char * qulltoa2(char *p, qulonglong n, int base)
int wholePartSpace(double d)
QString qdtoBasicLatin(double d, QLocaleData::DoubleForm form, int precision, bool uppercase)
UcsInt unicodeForDigit(uint digit, UcsInt zero)
QString qulltoa(qulonglong l, int base, const QStringView zero)
QSimpleParsedNumber< qulonglong > qstrntoull(const char *nptr, qsizetype size, int base)
void qt_doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, char *buf, qsizetype bufSize, bool &sign, int &length, int &decpt)
QString qulltoBasicLatin(qulonglong l, int base, bool negative)
QScopedPointerObjectDeleteLater< QObject > QScopedPointerDeleteLater
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
Definition qsize.h:191
CharBuff buff
Definition qlocale_p.h:239
static constexpr int MaxRange
Definition qlocale_p.h:49
constexpr bool matches(uchar c) const noexcept
Definition qlocale_p.h:61
constexpr QCharacterSetMatch(std::string_view set) noexcept
Definition qlocale_p.h:52
QStringView viewListEntry(const char16_t *table, qsizetype index) const
Definition qlocale_p.h:429
char32_t ucsFirst(const char16_t *table) const
Definition qlocale_p.h:433
QString getData(const char16_t *table) const
Definition qlocale_p.h:415
QString getListEntry(const char16_t *table, qsizetype index) const
Definition qlocale_p.h:425
QStringView viewData(const char16_t *table) const
Definition qlocale_p.h:421
void setZero(QStringView zero)
Definition qlocale_p.h:355
bool isValid(NumberMode mode) const
Definition qlocale_p.h:375
QString positiveSign() const
Definition qlocale.cpp:1110
static float convertDoubleToFloat(double d, bool *ok)
Definition qlocale_p.h:319
QString groupSeparator() const
Definition qlocale.cpp:1069
QSimpleParsedNumber< qint64 > stringToLongLong(QStringView str, int base, QLocale::NumberOptions options) const
Definition qlocale.cpp:4831
char32_t zeroUcs() const
Definition qlocale.cpp:1089
quint8 m_grouping_least
Definition qlocale_p.h:512
QString zeroDigit() const
Definition qlocale.cpp:1084
bool numberToCLocale(QStringView s, QLocale::NumberOptions number_options, NumberMode mode, CharBuff *result) const
Definition qlocale.cpp:4609
quint8 m_grouping_higher
Definition qlocale_p.h:511
QString decimalPoint() const
Definition qlocale.cpp:1064
QString doubleToString(double d, int precision=-1, DoubleForm form=DFSignificantDigits, int width=-1, unsigned flags=NoFlags) const
Definition qlocale.cpp:4056
static Q_AUTOTEST_EXPORT bool allLocaleDataRows(bool(*check)(qsizetype, const QLocaleData &))
Definition qlocale.cpp:519
quint8 m_weekend_start
Definition qlocale_p.h:508
quint8 m_currency_digits
Definition qlocale_p.h:505
QLocaleId id() const
Definition qlocale_p.h:396
QString listSeparator() const
Definition qlocale.cpp:1079
static QSimpleParsedNumber< quint64 > bytearrayToUnsLongLong(QByteArrayView num, int base)
Definition qlocale.cpp:4878
QString percentSign() const
Definition qlocale.cpp:1074
@ BlankBeforePositive
Definition qlocale_p.h:264
@ AddTrailingZeroes
Definition qlocale_p.h:261
double stringToDouble(QStringView str, bool *ok, QLocale::NumberOptions options) const
Definition qlocale.cpp:4815
QString longLongToString(qint64 l, int precision=-1, int base=10, int width=-1, unsigned flags=NoFlags) const
Definition qlocale.cpp:4297
@ DoubleScientificMode
Definition qlocale_p.h:275
@ DoubleStandardMode
Definition qlocale_p.h:275
@ DFSignificantDigits
Definition qlocale_p.h:255
quint8 m_first_day_of_week
Definition qlocale_p.h:507
quint8 m_weekend_end
Definition qlocale_p.h:509
NumericData numericData(NumberMode mode) const
Definition qlocale.cpp:4382
quint8 m_currency_rounding
Definition qlocale_p.h:506
QString exponentSeparator() const
Definition qlocale.cpp:1115
static qsizetype findLocaleIndex(QLocaleId localeId) noexcept
Definition qlocale.cpp:579
QString negativeSign() const
Definition qlocale.cpp:1105
static const QLocaleData * c() noexcept
Definition qlocale.cpp:953
QSimpleParsedNumber< quint64 > stringToUnsLongLong(QStringView str, int base, QLocale::NumberOptions options) const
Definition qlocale.cpp:4842
QString unsLongLongToString(quint64 l, int precision=-1, int base=10, int width=-1, unsigned flags=NoFlags) const
Definition qlocale.cpp:4312
quint8 m_grouping_first
Definition qlocale_p.h:510
QLocaleId withLikelySubtagsAdded() const noexcept
Fill in blank fields of a locale ID.
Definition qlocale.cpp:321
QLocaleId withLikelySubtagsRemoved() const noexcept
Definition qlocale.cpp:410
ushort script_id
Definition qlocale_p.h:223
bool operator==(QLocaleId other) const noexcept
Definition qlocale_p.h:192
bool matchesAll() const noexcept
Definition qlocale_p.h:201
bool isValid() const noexcept
Definition qlocale_p.h:196
bool operator!=(QLocaleId other) const noexcept
Definition qlocale_p.h:194
bool acceptScriptTerritory(QLocaleId other) const noexcept
Definition qlocale_p.h:212
ushort territory_id
Definition qlocale_p.h:223
bool acceptLanguage(quint16 lang) const noexcept
Definition qlocale_p.h:206
QByteArray name(char separator='-') const
Definition qlocale.cpp:434
ushort language_id
Definition qlocale_p.h:223
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
CurrencyToStringArgument(const QVariant &v, const QString &s)
Definition qlocale_p.h:119