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
qshareddata.h
Go to the documentation of this file.
1// Copyright (C) 2020 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 QSHAREDDATA_H
5#define QSHAREDDATA_H
6
7#include <QtCore/qglobal.h>
8#include <QtCore/qatomic.h>
9#include <QtCore/qcompare.h>
10#include <QtCore/qhashfunctions.h>
11
12
14
15
16template <class T> class QSharedDataPointer;
17
19{
20public:
21 mutable QAtomicInt ref;
22
23 QSharedData() noexcept : ref(0) { }
24 QSharedData(const QSharedData &) noexcept : ref(0) { }
25
26 // using the assignment operator would lead to corruption in the ref-counting
27 QSharedData &operator=(const QSharedData &) = delete;
28 ~QSharedData() = default;
29};
30
31struct QAdoptSharedDataTag { explicit constexpr QAdoptSharedDataTag() = default; };
32
33template <typename T>
35{
36public:
37 typedef T Type;
38 typedef T *pointer;
39
40 void detach() { if (d && d->ref.loadRelaxed() != 1) detach_helper(); }
41 T &operator*() { detach(); return *(d.get()); }
42 const T &operator*() const { return *(d.get()); }
43 T *operator->() { detach(); return d.get(); }
44 const T *operator->() const noexcept { return d.get(); }
45 operator T *() { detach(); return d.get(); }
46 operator const T *() const noexcept { return d.get(); }
47 T *data() { detach(); return d.get(); }
48 T *get() { detach(); return d.get(); }
49 const T *data() const noexcept { return d.get(); }
50 const T *get() const noexcept { return d.get(); }
51 const T *constData() const noexcept { return d.get(); }
52 T *take() noexcept { return std::exchange(d, nullptr).get(); }
53
55 QSharedDataPointer() noexcept : d(nullptr) { }
56 ~QSharedDataPointer() { if (d && !d->ref.deref()) delete d.get(); }
57
59 explicit QSharedDataPointer(T *data) noexcept : d(data)
60 { if (d) d->ref.ref(); }
66 { if (d) d->ref.ref(); }
67
68 void reset(T *ptr = nullptr) noexcept
69 {
70 if (ptr != d.get()) {
71 if (ptr)
72 ptr->ref.ref();
73 T *old = std::exchange(d, Qt::totally_ordered_wrapper(ptr)).get();
74 if (old && !old->ref.deref())
75 delete old;
76 }
77 }
78
80 {
81 reset(o.d.get());
82 return *this;
83 }
84 inline QSharedDataPointer &operator=(T *o) noexcept
85 {
86 reset(o);
87 return *this;
88 }
90 QSharedDataPointer(QSharedDataPointer &&o) noexcept : d(std::exchange(o.d, nullptr)) {}
92
93 operator bool () const noexcept { return d != nullptr; }
94 bool operator!() const noexcept { return d == nullptr; }
95
97 { qt_ptr_swap(d, other.d); }
98
99protected:
101
102private:
103 friend bool comparesEqual(const QSharedDataPointer &lhs, const QSharedDataPointer &rhs) noexcept
104 { return lhs.d == rhs.d; }
105 friend Qt::strong_ordering
107 { return Qt::compareThreeWay(lhs.d, rhs.d); }
109
110 friend bool comparesEqual(const QSharedDataPointer &lhs, const T *rhs) noexcept
111 { return lhs.d == rhs; }
112 friend Qt::strong_ordering
113 compareThreeWay(const QSharedDataPointer &lhs, const T *rhs) noexcept
114 { return Qt::compareThreeWay(lhs.d, rhs); }
116
117 friend bool comparesEqual(const QSharedDataPointer &lhs, std::nullptr_t) noexcept
118 { return lhs.d == nullptr; }
119 friend Qt::strong_ordering
121 { return Qt::compareThreeWay(lhs.d, nullptr); }
123
124 void detach_helper();
125
126 Qt::totally_ordered_wrapper<T *> d;
127};
128
129template <typename T>
131{
132public:
133 typedef T Type;
134 typedef T *pointer;
135
136 T &operator*() const { return *(d.get()); }
137 T *operator->() noexcept { return d.get(); }
138 T *operator->() const noexcept { return d.get(); }
139 explicit operator T *() { return d.get(); }
140 explicit operator const T *() const noexcept { return d.get(); }
141 T *data() const noexcept { return d.get(); }
142 T *get() const noexcept { return d.get(); }
143 const T *constData() const noexcept { return d.get(); }
144 T *take() noexcept { return std::exchange(d, nullptr).get(); }
145
146 void detach() { if (d && d->ref.loadRelaxed() != 1) detach_helper(); }
147
149 QExplicitlySharedDataPointer() noexcept : d(nullptr) { }
150 ~QExplicitlySharedDataPointer() { if (d && !d->ref.deref()) delete d.get(); }
151
153 explicit QExplicitlySharedDataPointer(T *data) noexcept : d(data)
154 { if (d) d->ref.ref(); }
160 { if (d) d->ref.ref(); }
161
162 template<typename X>
165#ifdef QT_ENABLE_QEXPLICITLYSHAREDDATAPOINTER_STATICCAST
166#error This macro has been removed in Qt 6.9.
167#endif
168 : d(o.data())
169 { if (d) d->ref.ref(); }
170
171 void reset(T *ptr = nullptr) noexcept
172 {
173 if (ptr != d) {
174 if (ptr)
175 ptr->ref.ref();
176 T *old = std::exchange(d, Qt::totally_ordered_wrapper(ptr)).get();
177 if (old && !old->ref.deref())
178 delete old;
179 }
180 }
181
183 {
184 reset(o.d.get());
185 return *this;
186 }
188 {
189 reset(o);
190 return *this;
191 }
195
196 operator bool () const noexcept { return d != nullptr; }
197 bool operator!() const noexcept { return d == nullptr; }
198
201
202protected:
204
205private:
207 const QExplicitlySharedDataPointer &rhs) noexcept
208 { return lhs.d == rhs.d; }
209 friend Qt::strong_ordering
211 const QExplicitlySharedDataPointer &rhs) noexcept
212 { return Qt::compareThreeWay(lhs.d, rhs.d); }
214
215 friend bool comparesEqual(const QExplicitlySharedDataPointer &lhs, const T *rhs) noexcept
216 { return lhs.d == rhs; }
217 friend Qt::strong_ordering
219 { return Qt::compareThreeWay(lhs.d, rhs); }
221
223 { return lhs.d == nullptr; }
224 friend Qt::strong_ordering
226 { return Qt::compareThreeWay(lhs.d, nullptr); }
228
229 void detach_helper();
230
231 Qt::totally_ordered_wrapper<T *> d;
232};
233
234// Declared here and as Q_OUTOFLINE_TEMPLATE to work-around MSVC bug causing missing symbols at link time.
235template <typename T>
236Q_INLINE_TEMPLATE T *QSharedDataPointer<T>::clone()
237{
238 return new T(*d);
239}
240
241template <typename T>
242Q_OUTOFLINE_TEMPLATE void QSharedDataPointer<T>::detach_helper()
243{
244 T *x = clone();
245 x->ref.ref();
246 if (!d.get()->ref.deref())
247 delete d.get();
248 d.reset(x);
249}
250
251template <typename T>
252Q_INLINE_TEMPLATE T *QExplicitlySharedDataPointer<T>::clone()
253{
254 return new T(*d.get());
255}
256
257template <typename T>
258Q_OUTOFLINE_TEMPLATE void QExplicitlySharedDataPointer<T>::detach_helper()
259{
260 T *x = clone();
261 x->ref.ref();
262 if (!d->ref.deref())
263 delete d.get();
264 d.reset(x);
265}
266
267template <typename T>
268void swap(QSharedDataPointer<T> &p1, QSharedDataPointer<T> &p2) noexcept
269{ p1.swap(p2); }
270
271template <typename T>
273{ p1.swap(p2); }
274
275template <typename T>
276size_t qHash(const QSharedDataPointer<T> &ptr, size_t seed = 0) noexcept
277{
278 return qHash(ptr.data(), seed);
279}
280template <typename T>
281size_t qHash(const QExplicitlySharedDataPointer<T> &ptr, size_t seed = 0) noexcept
282{
283 return qHash(ptr.data(), seed);
284}
285
288
289#define QT_DECLARE_QSDP_SPECIALIZATION_DTOR(Class)
290 template<> QSharedDataPointer<Class>::~QSharedDataPointer();
291
292#define QT_DECLARE_QSDP_SPECIALIZATION_DTOR_WITH_EXPORT(Class, ExportMacro)
293 template<> ExportMacro QSharedDataPointer<Class>::~QSharedDataPointer();
294
295#define QT_DEFINE_QSDP_SPECIALIZATION_DTOR(Class)
296 template<> QSharedDataPointer<Class>::~QSharedDataPointer()
297 {
298 if (d && !d->ref.deref())
299 delete d.get();
300 }
301
302#define QT_DECLARE_QESDP_SPECIALIZATION_DTOR(Class)
303 template<> QExplicitlySharedDataPointer<Class>::~QExplicitlySharedDataPointer();
304
305#define QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(Class, ExportMacro)
306 template<> ExportMacro QExplicitlySharedDataPointer<Class>::~QExplicitlySharedDataPointer();
307
308#define QT_DEFINE_QESDP_SPECIALIZATION_DTOR(Class)
309 template<> QExplicitlySharedDataPointer<Class>::~QExplicitlySharedDataPointer()
310 {
311 if (d && !d->ref.deref())
312 delete d.get();
313 }
314
315QT_END_NAMESPACE
316
317#endif // QSHAREDDATA_H
\inmodule QtCore
Definition qatomic.h:112
constexpr QAtomicInt(int value=0) noexcept
Constructs a QAtomicInt with the given value.
Definition qatomic.h:117
\macro Q_ATOMIC_INTnn_IS_SUPPORTED
Definition qatomic.h:123
QAtomicPointer(const QAtomicPointer< T > &other) noexcept
Constructs a copy of other.
Definition qatomic.h:127
QAtomicPointer< T > & operator=(const QAtomicPointer< T > &other) noexcept
Assigns other to this QAtomicPointer and returns a reference to this QAtomicPointer.
Definition qatomic.h:133
constexpr QAtomicPointer(T *value=nullptr) noexcept
Constructs a QAtomicPointer with the given value.
Definition qatomic.h:125
const T * constData() const noexcept
Returns a const pointer to the shared data object.
T * get() const noexcept
~QExplicitlySharedDataPointer()
Decrements the reference count of the shared data object.
void reset(T *ptr=nullptr) noexcept
operator const T *() const noexcept
T * operator->() noexcept
Provides access to the shared data object's members.
T * operator->() const noexcept
Provides const access to the shared data object's members.
T Type
This is the type of the shared data object.
QExplicitlySharedDataPointer & operator=(const QExplicitlySharedDataPointer &o) noexcept
Sets the {d pointer} of this to the {d pointer} of o and increments the reference count of the shared...
T * data() const noexcept
Returns a pointer to the shared data object.
friend Qt::strong_ordering compareThreeWay(const QExplicitlySharedDataPointer &lhs, const QExplicitlySharedDataPointer &rhs) noexcept
void detach()
If the shared data object's reference count is greater than 1, this function creates a deep copy of t...
T & operator*() const
Provides access to the shared data object's members.
friend bool comparesEqual(const QExplicitlySharedDataPointer &lhs, const QExplicitlySharedDataPointer &rhs) noexcept
QExplicitlySharedDataPointer & operator=(T *o) noexcept
Sets the {d pointer} of this to o and increments {o}'s reference count.
QFileInfoPrivate(const QFileSystemEntry &file, const QFileSystemMetaData &data, std::unique_ptr< QAbstractFileEngine > engine)
Definition qfileinfo_p.h:97
bool const isDefaultConstructed
QFileInfoPrivate(const QFileSystemEntry &file, const QFileSystemMetaData &data)
Definition qfileinfo_p.h:82
Ret checkAttribute(QFileSystemMetaData::MetaDataFlags fsFlags, FSLambda fsLambda, EngineLambda engineLambda) const
QFileSystemMetaData metaData
QDateTime & getFileTime(QFile::FileTime) const
std::unique_ptr< QAbstractFileEngine > const fileEngine
QDateTime fileTimes[4]
uint getFileFlags(QAbstractFileEngine::FileFlags) const
Definition qfileinfo.cpp:99
void clearFlags() const
void setCachedFlag(uint c) const
QString getFileOwner(QAbstractFileEngine::FileOwner own) const
Definition qfileinfo.cpp:75
QString fileOwners[2]
bool getCachedFlag(uint c) const
QFileInfoPrivate(const QFileInfoPrivate &copy)
Definition qfileinfo_p.h:56
QFileSystemEntry fileEntry
static QFileInfoPrivate * get(QFileInfo *fi)
Definition qfileinfo_p.h:48
QString getFileName(QAbstractFileEngine::FileName) const
Definition qfileinfo.cpp:17
QString fileNames[QAbstractFileEngine::NFileNames]
QFileInfoPrivate(const QString &file)
Definition qfileinfo_p.h:69
Ret checkAttribute(Ret defaultValue, QFileSystemMetaData::MetaDataFlags fsFlags, FSLambda fsLambda, EngineLambda engineLambda) const
T * take() noexcept
Definition qshareddata.h:52
T & operator*()
Provides access to the shared data object's members.
Definition qshareddata.h:41
T Type
This is the type of the shared data object.
Definition qshareddata.h:37
const T * get() const noexcept
Definition qshareddata.h:50
T * data()
Returns a pointer to the shared data object.
Definition qshareddata.h:47
Q_NODISCARD_CTOR QSharedDataPointer() noexcept
Constructs a QSharedDataPointer initialized with \nullptr as {d pointer}.
Definition qshareddata.h:55
~QSharedDataPointer()
Decrements the reference count of the shared data object.
Definition qshareddata.h:56
QSharedDataPointer & operator=(const QSharedDataPointer &o) noexcept
Sets the {d pointer} of this to the {d pointer} of o and increments the reference count of the shared...
Definition qshareddata.h:79
void reset(T *ptr=nullptr) noexcept
Definition qshareddata.h:68
operator const T *() const noexcept
Returns a pointer to the shared data object.
Definition qshareddata.h:46
friend Qt::strong_ordering compareThreeWay(const QSharedDataPointer &lhs, const QSharedDataPointer &rhs) noexcept
friend bool comparesEqual(const QSharedDataPointer &lhs, const QSharedDataPointer &rhs) noexcept
const T * data() const noexcept
Returns a pointer to the shared data object.
Definition qshareddata.h:49
operator T*()
Returns a pointer to the shared data object.
Definition qshareddata.h:45
const T * operator->() const noexcept
Provides const access to the shared data object's members.
Definition qshareddata.h:44
void detach()
If the shared data object's reference count is greater than 1, this function creates a deep copy of t...
Definition qshareddata.h:40
const T & operator*() const
Provides const access to the shared data object's members.
Definition qshareddata.h:42
const T * constData() const noexcept
Returns a const pointer to the shared data object.
Definition qshareddata.h:51
QSharedDataPointer & operator=(T *o) noexcept
Sets the {d pointer} og this to o and increments {o}'s reference count.
Definition qshareddata.h:84
T * operator->()
Provides access to the shared data object's members.
Definition qshareddata.h:43
\inmodule QtCore
Definition qshareddata.h:19
QSharedData() noexcept
Constructs a QSharedData object with a reference count of 0.
Definition qshareddata.h:23
~QSharedData()=default
QAtomicInt ref
Definition qshareddata.h:21
QSharedData & operator=(const QSharedData &)=delete
QSharedData(const QSharedData &) noexcept
Constructs a QSharedData object with reference count 0.
Definition qshareddata.h:24
Combined button and popup list for selecting options.
void qAtomicDetach(T *&d)
This is a helper for the detach method of implicitly shared classes.
Definition qatomic.h:199
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs)
Q_DECLARE_TYPEINFO_BODY(QExplicitlySharedDataPointer< T >, Q_RELOCATABLE_TYPE)
void swap(QExplicitlySharedDataPointer< T > &p1, QExplicitlySharedDataPointer< T > &p2) noexcept
void swap(QSharedDataPointer< T > &p1, QSharedDataPointer< T > &p2) noexcept
Q_DECLARE_TYPEINFO_BODY(QSharedDataPointer< T >, Q_RELOCATABLE_TYPE)
size_t qHash(const QSharedDataPointer< T > &ptr, size_t seed=0) noexcept
size_t qHash(const QExplicitlySharedDataPointer< T > &ptr, size_t seed=0) noexcept
\inmodule QtCore \threadsafe
Definition qshareddata.h:31