File indexing completed on 2026-09-02 09:19:06
0001
0002
0003
0004
0005 #if 0
0006 #pragma qt_sync_skip_header_check
0007 #pragma qt_sync_stop_processing
0008 #endif
0009
0010 #ifndef QSHAREDDATA_IMPL_H
0011 #define QSHAREDDATA_IMPL_H
0012
0013 #include <QtCore/qcompare.h>
0014 #include <QtCore/qglobal.h>
0015 #include <QtCore/qshareddata.h>
0016
0017 QT_BEGIN_NAMESPACE
0018
0019 namespace QtPrivate {
0020
0021 template <typename T>
0022 class QExplicitlySharedDataPointerV2
0023 {
0024 Qt::totally_ordered_wrapper<T *> d;
0025
0026 public:
0027 constexpr QExplicitlySharedDataPointerV2() noexcept : d(nullptr) {}
0028
0029 explicit QExplicitlySharedDataPointerV2(T *t) noexcept
0030 : d(t)
0031 {
0032 if (d)
0033 d->ref.ref();
0034 }
0035
0036 QExplicitlySharedDataPointerV2(T *t, QAdoptSharedDataTag) noexcept
0037 : d(t)
0038 {
0039 }
0040
0041 QExplicitlySharedDataPointerV2(const QExplicitlySharedDataPointerV2 &other) noexcept
0042 : d(other.d)
0043 {
0044 if (d)
0045 d->ref.ref();
0046 }
0047
0048 QExplicitlySharedDataPointerV2 &operator=(const QExplicitlySharedDataPointerV2 &other) noexcept
0049 {
0050 QExplicitlySharedDataPointerV2 copy(other);
0051 swap(copy);
0052 return *this;
0053 }
0054
0055 QExplicitlySharedDataPointerV2(QExplicitlySharedDataPointerV2 &&other) noexcept
0056 : d(std::exchange(other.d, nullptr))
0057 {
0058 }
0059
0060 QExplicitlySharedDataPointerV2 &operator=(QExplicitlySharedDataPointerV2 &&other) noexcept
0061 {
0062 QExplicitlySharedDataPointerV2 moved(std::move(other));
0063 swap(moved);
0064 return *this;
0065 }
0066
0067 ~QExplicitlySharedDataPointerV2()
0068 {
0069 if (d && !d->ref.deref())
0070 delete d.get();
0071 }
0072
0073 void detach()
0074 {
0075 if (!d) {
0076
0077 d.reset(new T);
0078 d->ref.ref();
0079 } else if (d->ref.loadRelaxed() != 1) {
0080
0081 QExplicitlySharedDataPointerV2 copy(new T(*d));
0082 swap(copy);
0083 }
0084 }
0085
0086 void reset(T *t = nullptr) noexcept
0087 {
0088 if (d && !d->ref.deref())
0089 delete d.get();
0090 d.reset(t);
0091 if (d)
0092 d->ref.ref();
0093 }
0094
0095 constexpr T *take() noexcept
0096 {
0097 return std::exchange(d, nullptr).get();
0098 }
0099
0100 bool isShared() const noexcept
0101 {
0102 return d && d->ref.loadRelaxed() != 1;
0103 }
0104
0105 constexpr void swap(QExplicitlySharedDataPointerV2 &other) noexcept
0106 {
0107 qt_ptr_swap(d, other.d);
0108 }
0109
0110
0111 constexpr T &operator*() { return *(d.get()); }
0112 constexpr T *operator->() { return d.get(); }
0113 constexpr const T &operator*() const { return *(d.get()); }
0114 constexpr const T *operator->() const { return d.get(); }
0115
0116 constexpr T *data() noexcept { return d.get(); }
0117 constexpr const T *data() const noexcept { return d.get(); }
0118
0119 constexpr explicit operator bool() const noexcept { return d.get(); }
0120
0121 private:
0122 constexpr friend bool comparesEqual(const QExplicitlySharedDataPointerV2 &lhs,
0123 const QExplicitlySharedDataPointerV2 &rhs) noexcept
0124 { return lhs.d == rhs.d; }
0125 constexpr friend Qt::strong_ordering
0126 compareThreeWay(const QExplicitlySharedDataPointerV2 &lhs,
0127 const QExplicitlySharedDataPointerV2 &rhs) noexcept
0128 { return Qt::compareThreeWay(lhs.d, rhs.d); }
0129 Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QExplicitlySharedDataPointerV2)
0130
0131 constexpr friend bool
0132 comparesEqual(const QExplicitlySharedDataPointerV2 &lhs, std::nullptr_t) noexcept
0133 { return lhs.d == nullptr; }
0134 constexpr friend Qt::strong_ordering
0135 compareThreeWay(const QExplicitlySharedDataPointerV2 &lhs, std::nullptr_t) noexcept
0136 { return Qt::compareThreeWay(lhs.d, nullptr); }
0137 Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QExplicitlySharedDataPointerV2, std::nullptr_t)
0138 };
0139
0140 template <typename T>
0141 constexpr void swap(QExplicitlySharedDataPointerV2<T> &lhs, QExplicitlySharedDataPointerV2<T> &rhs) noexcept
0142 {
0143 lhs.swap(rhs);
0144 }
0145
0146 }
0147
0148 QT_END_NAMESPACE
0149
0150 #endif