Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:26:45

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