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_impl.h
Go to the documentation of this file.
1// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
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#if 0
6#pragma qt_sync_skip_header_check
7#pragma qt_sync_stop_processing
8#endif
9
10#ifndef QSHAREDDATA_IMPL_H
11#define QSHAREDDATA_IMPL_H
12
13#include <QtCore/qcompare.h>
14#include <QtCore/qglobal.h>
15#include <QtCore/qshareddata.h>
16
17QT_BEGIN_NAMESPACE
18
19namespace QtPrivate {
20
21template <typename T>
23{
24 Qt::totally_ordered_wrapper<T *> d;
25
26public:
27 constexpr QExplicitlySharedDataPointerV2() noexcept : d(nullptr) {}
28
29 explicit QExplicitlySharedDataPointerV2(T *t) noexcept
30 : d(t)
31 {
32 if (d)
33 d->ref.ref();
34 }
35
37 : d(t)
38 {
39 }
40
42 : d(other.d)
43 {
44 if (d)
45 d->ref.ref();
46 }
47
49 {
51 swap(copy);
52 return *this;
53 }
54
59
61 {
62 QExplicitlySharedDataPointerV2 moved(std::move(other));
63 swap(moved);
64 return *this;
65 }
66
68 {
69 if (d && !d->ref.deref())
70 delete d.get();
71 }
72
73 void detach()
74 {
75 if (!d) {
76 // should this codepath be here on in all user's detach()?
77 d.reset(new T);
78 d->ref.ref();
79 } else if (d->ref.loadRelaxed() != 1) {
80 // TODO: qAtomicDetach here...?
81 QExplicitlySharedDataPointerV2 copy(new T(*d));
82 swap(copy);
83 }
84 }
85
86 void reset(T *t = nullptr) noexcept
87 {
88 if (d && !d->ref.deref())
89 delete d.get();
90 d.reset(t);
91 if (d)
92 d->ref.ref();
93 }
94
95 constexpr T *take() noexcept
96 {
97 return std::exchange(d, nullptr).get();
98 }
99
100 bool isShared() const noexcept
101 {
102 return d && d->ref.loadRelaxed() != 1;
103 }
104
105 constexpr void swap(QExplicitlySharedDataPointerV2 &other) noexcept
106 {
107 qt_ptr_swap(d, other.d);
108 }
109
110 // important change from QExplicitlySharedDataPointer: deep const
111 constexpr T &operator*() { return *(d.get()); }
112 constexpr T *operator->() { return d.get(); }
113 constexpr const T &operator*() const { return *(d.get()); }
114 constexpr const T *operator->() const { return d.get(); }
115
116 constexpr T *data() noexcept { return d.get(); }
117 constexpr const T *data() const noexcept { return d.get(); }
118
119 constexpr explicit operator bool() const noexcept { return d.get(); }
120
121private:
122 constexpr friend bool comparesEqual(const QExplicitlySharedDataPointerV2 &lhs,
123 const QExplicitlySharedDataPointerV2 &rhs) noexcept
124 { return lhs.d == rhs.d; }
125 constexpr friend Qt::strong_ordering
127 const QExplicitlySharedDataPointerV2 &rhs) noexcept
128 { return Qt::compareThreeWay(lhs.d, rhs.d); }
130
131 constexpr friend bool
133 { return lhs.d == nullptr; }
134 constexpr friend Qt::strong_ordering
138};
139
140template <typename T>
142{
143 lhs.swap(rhs);
144}
145
146} // namespace QtPrivate
147
148QT_END_NAMESPACE
149
150#endif // QSHAREDDATA_IMPL_H
QExplicitlySharedDataPointerV2(const QExplicitlySharedDataPointerV2 &other) noexcept
constexpr void swap(QExplicitlySharedDataPointerV2 &other) noexcept
constexpr const T * data() const noexcept
QExplicitlySharedDataPointerV2 & operator=(const QExplicitlySharedDataPointerV2 &other) noexcept
QExplicitlySharedDataPointerV2(T *t, QAdoptSharedDataTag) noexcept
QExplicitlySharedDataPointerV2 & operator=(QExplicitlySharedDataPointerV2 &&other) noexcept
constexpr friend Qt::strong_ordering compareThreeWay(const QExplicitlySharedDataPointerV2 &lhs, const QExplicitlySharedDataPointerV2 &rhs) noexcept
QExplicitlySharedDataPointerV2(QExplicitlySharedDataPointerV2 &&other) noexcept
constexpr friend bool comparesEqual(const QExplicitlySharedDataPointerV2 &lhs, const QExplicitlySharedDataPointerV2 &rhs) noexcept
constexpr void swap(QExplicitlySharedDataPointerV2< T > &lhs, QExplicitlySharedDataPointerV2< T > &rhs) noexcept