Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:07:36

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/qglobal.h>
0013 #include <QtCore/qshareddata.h>
0014 
0015 QT_BEGIN_NAMESPACE
0016 
0017 namespace QtPrivate {
0018 
0019 template <typename T>
0020 class QExplicitlySharedDataPointerV2
0021 {
0022     T *d;
0023 
0024 public:
0025     constexpr QExplicitlySharedDataPointerV2() noexcept : d(nullptr) {}
0026 
0027     explicit QExplicitlySharedDataPointerV2(T *t) noexcept
0028         : d(t)
0029     {
0030         if (d)
0031             d->ref.ref();
0032     }
0033 
0034     QExplicitlySharedDataPointerV2(T *t, QAdoptSharedDataTag) noexcept
0035         : d(t)
0036     {
0037     }
0038 
0039     QExplicitlySharedDataPointerV2(const QExplicitlySharedDataPointerV2 &other) noexcept
0040         : d(other.d)
0041     {
0042         if (d)
0043             d->ref.ref();
0044     }
0045 
0046     QExplicitlySharedDataPointerV2 &operator=(const QExplicitlySharedDataPointerV2 &other) noexcept
0047     {
0048         QExplicitlySharedDataPointerV2 copy(other);
0049         swap(copy);
0050         return *this;
0051     }
0052 
0053     QExplicitlySharedDataPointerV2(QExplicitlySharedDataPointerV2 &&other) noexcept
0054         : d(std::exchange(other.d, nullptr))
0055     {
0056     }
0057 
0058     QExplicitlySharedDataPointerV2 &operator=(QExplicitlySharedDataPointerV2 &&other) noexcept
0059     {
0060         QExplicitlySharedDataPointerV2 moved(std::move(other));
0061         swap(moved);
0062         return *this;
0063     }
0064 
0065     ~QExplicitlySharedDataPointerV2()
0066     {
0067         if (d && !d->ref.deref())
0068             delete d;
0069     }
0070 
0071     void detach()
0072     {
0073         if (!d) {
0074             // should this codepath be here on in all user's detach()?
0075             d = new T;
0076             d->ref.ref();
0077         } else if (d->ref.loadRelaxed() != 1) {
0078             // TODO: qAtomicDetach here...?
0079             QExplicitlySharedDataPointerV2 copy(new T(*d));
0080             swap(copy);
0081         }
0082     }
0083 
0084     void reset(T *t = nullptr) noexcept
0085     {
0086         if (d && !d->ref.deref())
0087             delete d;
0088         d = t;
0089         if (d)
0090             d->ref.ref();
0091     }
0092 
0093     constexpr T *take() noexcept
0094     {
0095         return std::exchange(d, nullptr);
0096     }
0097 
0098     bool isShared() const noexcept
0099     {
0100         return d && d->ref.loadRelaxed() != 1;
0101     }
0102 
0103     constexpr void swap(QExplicitlySharedDataPointerV2 &other) noexcept
0104     {
0105         qt_ptr_swap(d, other.d);
0106     }
0107 
0108     // important change from QExplicitlySharedDataPointer: deep const
0109     constexpr T &operator*() { return *d; }
0110     constexpr T *operator->() { return d; }
0111     constexpr const T &operator*() const { return *d; }
0112     constexpr const T *operator->() const { return d; }
0113 
0114     constexpr T *data() noexcept { return d; }
0115     constexpr const T *data() const noexcept { return d; }
0116 
0117     constexpr explicit operator bool() const noexcept { return d; }
0118 
0119     constexpr friend bool operator==(const QExplicitlySharedDataPointerV2 &lhs,
0120                                      const QExplicitlySharedDataPointerV2 &rhs) noexcept
0121     {
0122         return lhs.d == rhs.d;
0123     }
0124 
0125     constexpr friend bool operator!=(const QExplicitlySharedDataPointerV2 &lhs,
0126                                      const QExplicitlySharedDataPointerV2 &rhs) noexcept
0127     {
0128         return lhs.d != rhs.d;
0129     }
0130 };
0131 
0132 template <typename T>
0133 constexpr void swap(QExplicitlySharedDataPointerV2<T> &lhs, QExplicitlySharedDataPointerV2<T> &rhs) noexcept
0134 {
0135     lhs.swap(rhs);
0136 }
0137 
0138 } // namespace QtPrivate
0139 
0140 QT_END_NAMESPACE
0141 
0142 #endif // QSHAREDDATA_IMPL_H