Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-03 11:00:22

0001 // Copyright (C) 2021 The Qt Company Ltd.
0002 // Copyright (C) 2022 Intel Corporation.
0003 // Copyright (C) 2019 Klarälvdalens Datakonsult AB.
0004 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0005 // Qt-Security score:significant reason:default
0006 
0007 #ifndef Q_QDOC
0008 
0009 #ifndef QSHAREDPOINTER_H
0010 #error Do not include qsharedpointer_impl.h directly
0011 #endif
0012 
0013 #if 0
0014 #pragma qt_sync_skip_header_check
0015 #pragma qt_sync_stop_processing
0016 #endif
0017 
0018 #if 0
0019 // These macros are duplicated here to make syncqt not complain a about
0020 // this header, as we have a "qt_sync_stop_processing" below, which in turn
0021 // is here because this file contains a template mess and duplicates the
0022 // classes found in qsharedpointer.h
0023 QT_BEGIN_NAMESPACE
0024 QT_END_NAMESPACE
0025 #pragma qt_sync_stop_processing
0026 #endif
0027 
0028 #include <new>
0029 #include <QtCore/qatomic.h>
0030 #include <QtCore/qhashfunctions.h>
0031 #include <QtCore/qmetatype.h> // for IsPointerToTypeDerivedFromQObject
0032 #include <QtCore/qxptype_traits.h>
0033 
0034 #include <memory>
0035 
0036 QT_BEGIN_NAMESPACE
0037 
0038 class QObject;
0039 template <class T>
0040 T qobject_cast(const QObject *object);
0041 
0042 //
0043 // forward declarations
0044 //
0045 template <class T> class QWeakPointer;
0046 template <class T> class QSharedPointer;
0047 template <class T> class QEnableSharedFromThis;
0048 
0049 class QVariant;
0050 
0051 template <class X, class T>
0052 QSharedPointer<X> qSharedPointerCast(const QSharedPointer<T> &ptr);
0053 template <class X, class T>
0054 QSharedPointer<X> qSharedPointerCast(QSharedPointer<T> &&ptr);
0055 template <class X, class T>
0056 QSharedPointer<X> qSharedPointerDynamicCast(const QSharedPointer<T> &ptr);
0057 template <class X, class T>
0058 QSharedPointer<X> qSharedPointerDynamicCast(QSharedPointer<T> &&ptr);
0059 template <class X, class T>
0060 QSharedPointer<X> qSharedPointerConstCast(const QSharedPointer<T> &ptr);
0061 template <class X, class T>
0062 QSharedPointer<X> qSharedPointerConstCast(QSharedPointer<T> &&ptr);
0063 
0064 #ifndef QT_NO_QOBJECT
0065 template <class X, class T>
0066 QSharedPointer<X> qSharedPointerObjectCast(const QSharedPointer<T> &ptr);
0067 template <class X, class T>
0068 QSharedPointer<X> qSharedPointerObjectCast(QSharedPointer<T> &&ptr);
0069 #endif
0070 
0071 namespace QtPrivate {
0072 struct EnableInternalData;
0073 }
0074 
0075 namespace QtSharedPointer {
0076     template <class T> class ExternalRefCount;
0077 
0078     template <class X, class Y> QSharedPointer<X> copyAndSetPointer(X * ptr, const QSharedPointer<Y> &src);
0079     template <class X, class Y> QSharedPointer<X> movePointer(X * ptr, QSharedPointer<Y> &&src);
0080 
0081     // used in debug mode to verify the reuse of pointers
0082     Q_CORE_EXPORT void internalSafetyCheckAdd(const void *, const volatile void *);
0083     Q_CORE_EXPORT void internalSafetyCheckRemove(const void *);
0084 
0085     template <class T, typename Klass, typename RetVal>
0086     inline void executeDeleter(T *t, RetVal (Klass:: *memberDeleter)())
0087     { if (t) (t->*memberDeleter)(); }
0088     template <class T, typename Deleter>
0089     inline void executeDeleter(T *t, Deleter d)
0090     { d(t); }
0091     struct NormalDeleter {};
0092 
0093     // this uses partial template specialization
0094     template <class T> struct RemovePointer;
0095     template <class T> struct RemovePointer<T *> { typedef T Type; };
0096     template <class T> struct RemovePointer<QSharedPointer<T> > { typedef T Type; };
0097     template <class T> struct RemovePointer<QWeakPointer<T> > { typedef T Type; };
0098 
0099     // This class is the d-pointer of QSharedPointer and QWeakPointer.
0100     //
0101     // It is a reference-counted reference counter. "strongref" is the inner
0102     // reference counter, and it tracks the lifetime of the pointer itself.
0103     // "weakref" is the outer reference counter and it tracks the lifetime of
0104     // the ExternalRefCountData object.
0105     //
0106     // The deleter is stored in the destroyer member and is always a pointer to
0107     // a static function in ExternalRefCountWithCustomDeleter or in
0108     // ExternalRefCountWithContiguousData
0109     struct ExternalRefCountData
0110     {
0111         typedef void (*DestroyerFn)(ExternalRefCountData *);
0112         QBasicAtomicInt weakref;
0113         QBasicAtomicInt strongref;
0114         DestroyerFn destroyer;
0115 
0116         inline ExternalRefCountData(DestroyerFn d)
0117             : weakref{1}, strongref{1}, destroyer{d}
0118         {
0119         }
0120         inline ExternalRefCountData(Qt::Initialization) { }
0121         ~ExternalRefCountData() { Q_ASSERT(!weakref.loadRelaxed()); Q_ASSERT(strongref.loadRelaxed() <= 0); }
0122 
0123         void destroy() { destroyer(this); }
0124 
0125 #ifndef QT_NO_QOBJECT
0126         Q_CORE_EXPORT static ExternalRefCountData *getAndRef(const QObject *);
0127         QT6_ONLY(
0128         Q_CORE_EXPORT void setQObjectShared(const QObject *, bool enable);
0129         )
0130         QT6_ONLY(Q_CORE_EXPORT void checkQObjectShared(const QObject *);)
0131 #endif
0132         inline void checkQObjectShared(...) { }
0133         inline void setQObjectShared(...) { }
0134 
0135         // Normally, only subclasses of ExternalRefCountData are allocated
0136         // One exception exists in getAndRef; that uses the global operator new
0137         // to prevent a mismatch with the custom operator delete
0138         inline void *operator new(std::size_t) = delete;
0139         // placement new
0140         inline void *operator new(std::size_t, void *ptr) noexcept { return ptr; }
0141         inline void operator delete(void *ptr) { ::operator delete(ptr); }
0142         inline void operator delete(void *, void *) { }
0143     };
0144     // sizeof(ExternalRefCountData) = 12 (32-bit) / 16 (64-bit)
0145 
0146     template <class T, typename Deleter>
0147     struct CustomDeleter
0148     {
0149         Deleter deleter;
0150         T *ptr;
0151 
0152         CustomDeleter(T *p, Deleter d) : deleter(d), ptr(p) {}
0153         void execute() { executeDeleter(ptr, deleter); }
0154     };
0155     // sizeof(CustomDeleter) = sizeof(Deleter) + sizeof(void*) + padding
0156     // for Deleter = stateless functor: 8 (32-bit) / 16 (64-bit) due to padding
0157     // for Deleter = function pointer:  8 (32-bit) / 16 (64-bit)
0158     // for Deleter = PMF: 12 (32-bit) / 24 (64-bit)  (GCC)
0159 
0160     // This specialization of CustomDeleter for a deleter of type NormalDeleter
0161     // is an optimization: instead of storing a pointer to a function that does
0162     // the deleting, we simply delete the pointer ourselves.
0163     template <class T>
0164     struct CustomDeleter<T, NormalDeleter>
0165     {
0166         T *ptr;
0167 
0168         CustomDeleter(T *p, NormalDeleter) : ptr(p) {}
0169         void execute() { delete ptr; }
0170     };
0171     // sizeof(CustomDeleter specialization) = sizeof(void*)
0172 
0173     // This class extends ExternalRefCountData and implements
0174     // the static function that deletes the object. The pointer and the
0175     // custom deleter are kept in the "extra" member so we can construct
0176     // and destruct it independently of the full structure.
0177     template <class T, typename Deleter>
0178     struct ExternalRefCountWithCustomDeleter: public ExternalRefCountData
0179     {
0180         typedef ExternalRefCountWithCustomDeleter Self;
0181         typedef ExternalRefCountData BaseClass;
0182         CustomDeleter<T, Deleter> extra;
0183 
0184         static inline void deleter(ExternalRefCountData *self)
0185         {
0186             Self *realself = static_cast<Self *>(self);
0187             realself->extra.execute();
0188 
0189             // delete the deleter too
0190             realself->extra.~CustomDeleter<T, Deleter>();
0191         }
0192         static void safetyCheckDeleter(ExternalRefCountData *self)
0193         {
0194             internalSafetyCheckRemove(self);
0195             deleter(self);
0196         }
0197 
0198         static inline Self *create(T *ptr, Deleter userDeleter, DestroyerFn actualDeleter)
0199         {
0200             Self *d = static_cast<Self *>(::operator new(sizeof(Self)));
0201 
0202             // initialize the two sub-objects
0203             new (&d->extra) CustomDeleter<T, Deleter>(ptr, userDeleter);
0204             new (d) BaseClass(actualDeleter); // can't throw
0205 
0206             return d;
0207         }
0208     private:
0209         // prevent construction
0210         ExternalRefCountWithCustomDeleter() = delete;
0211         ~ExternalRefCountWithCustomDeleter() = delete;
0212         Q_DISABLE_COPY(ExternalRefCountWithCustomDeleter)
0213     };
0214 
0215     // This class extends ExternalRefCountData and adds a "T"
0216     // member. That way, when the create() function is called, we allocate
0217     // memory for both QSharedPointer's d-pointer and the actual object being
0218     // tracked.
0219     template <class T>
0220     struct ExternalRefCountWithContiguousData: public ExternalRefCountData
0221     {
0222         typedef ExternalRefCountData Parent;
0223         typedef typename std::remove_cv<T>::type NoCVType;
0224         NoCVType data;
0225 
0226         static void deleter(ExternalRefCountData *self)
0227         {
0228             ExternalRefCountWithContiguousData *that =
0229                     static_cast<ExternalRefCountWithContiguousData *>(self);
0230             that->data.~T();
0231             Q_UNUSED(that); // MSVC warns if T has a trivial destructor
0232         }
0233         static void safetyCheckDeleter(ExternalRefCountData *self)
0234         {
0235             internalSafetyCheckRemove(self);
0236             deleter(self);
0237         }
0238         static void noDeleter(ExternalRefCountData *) { }
0239 
0240         static inline ExternalRefCountData *create(NoCVType **ptr, DestroyerFn destroy)
0241         {
0242             ExternalRefCountWithContiguousData *d =
0243                 static_cast<ExternalRefCountWithContiguousData *>(::operator new(sizeof(ExternalRefCountWithContiguousData)));
0244 
0245             // initialize the d-pointer sub-object
0246             // leave d->data uninitialized
0247             new (d) Parent(destroy); // can't throw
0248 
0249             *ptr = &d->data;
0250             return d;
0251         }
0252 
0253     private:
0254         // prevent construction
0255         ExternalRefCountWithContiguousData() = delete;
0256         ~ExternalRefCountWithContiguousData() = delete;
0257         Q_DISABLE_COPY(ExternalRefCountWithContiguousData)
0258     };
0259 
0260 #ifndef QT_NO_QOBJECT
0261     Q_CORE_EXPORT QWeakPointer<QObject> weakPointerFromVariant_internal(const QVariant &variant);
0262     Q_CORE_EXPORT QSharedPointer<QObject> sharedPointerFromVariant_internal(const QVariant &variant);
0263 #endif
0264 } // namespace QtSharedPointer
0265 
0266 template <class T> class QSharedPointer
0267 {
0268     typedef QtSharedPointer::ExternalRefCountData Data;
0269     template <typename X>
0270     using IfCompatible = typename std::enable_if<std::is_convertible<X*, T*>::value, bool>::type;
0271 
0272 public:
0273     typedef T Type;
0274     typedef T element_type;
0275     typedef T value_type;
0276     typedef value_type *pointer;
0277     typedef const value_type *const_pointer;
0278     typedef value_type &reference;
0279     typedef const value_type &const_reference;
0280     typedef qptrdiff difference_type;
0281 
0282     T *data() const noexcept { return value.get(); }
0283     T *get() const noexcept { return value.get(); }
0284     bool isNull() const noexcept { return !data(); }
0285     explicit operator bool() const noexcept { return !isNull(); }
0286     bool operator !() const noexcept { return isNull(); }
0287     T &operator*() const { return *data(); }
0288     T *operator->() const noexcept { return data(); }
0289 
0290     Q_NODISCARD_CTOR
0291     constexpr QSharedPointer() noexcept : value(nullptr), d(nullptr) { }
0292     ~QSharedPointer() { deref(); }
0293 
0294     Q_NODISCARD_CTOR
0295     constexpr QSharedPointer(std::nullptr_t) noexcept : value(nullptr), d(nullptr) { }
0296 
0297     template <class X, IfCompatible<X> = true>
0298     Q_NODISCARD_CTOR
0299     inline explicit QSharedPointer(X *ptr) : value(ptr) // noexcept
0300     { internalConstruct(ptr, QtSharedPointer::NormalDeleter()); }
0301 
0302     template <class X, typename Deleter, IfCompatible<X> = true>
0303     Q_NODISCARD_CTOR
0304     inline QSharedPointer(X *ptr, Deleter deleter) : value(ptr) // throws
0305     { internalConstruct(ptr, deleter); }
0306 
0307     template <typename Deleter>
0308     Q_NODISCARD_CTOR
0309     QSharedPointer(std::nullptr_t, Deleter deleter) : value(nullptr)
0310     { internalConstruct(static_cast<T *>(nullptr), deleter); }
0311 
0312     Q_NODISCARD_CTOR
0313     QSharedPointer(const QSharedPointer &other) noexcept : value(other.value.get()), d(other.d)
0314     { if (d) ref(); }
0315     QSharedPointer &operator=(const QSharedPointer &other) noexcept
0316     {
0317         QSharedPointer copy(other);
0318         swap(copy);
0319         return *this;
0320     }
0321     Q_NODISCARD_CTOR
0322     QSharedPointer(QSharedPointer &&other) noexcept
0323         : value(other.value.get()), d(other.d)
0324     {
0325         other.d = nullptr;
0326         other.value = nullptr;
0327     }
0328     QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QSharedPointer)
0329 
0330     template <class X, IfCompatible<X> = true>
0331     Q_NODISCARD_CTOR
0332     QSharedPointer(QSharedPointer<X> &&other) noexcept
0333         : value(other.value.get()), d(other.d)
0334     {
0335         other.d = nullptr;
0336         other.value = nullptr;
0337     }
0338 
0339     template <class X, IfCompatible<X> = true>
0340     QSharedPointer &operator=(QSharedPointer<X> &&other) noexcept
0341     {
0342         QSharedPointer moved(std::move(other));
0343         swap(moved);
0344         return *this;
0345     }
0346 
0347     template <class X, IfCompatible<X> = true>
0348     Q_NODISCARD_CTOR
0349     QSharedPointer(const QSharedPointer<X> &other) noexcept : value(other.value.get()), d(other.d)
0350     { if (d) ref(); }
0351 
0352     template <class X, IfCompatible<X> = true>
0353     inline QSharedPointer &operator=(const QSharedPointer<X> &other)
0354     {
0355         QSharedPointer copy(other);
0356         swap(copy);
0357         return *this;
0358     }
0359 
0360     template <class X, IfCompatible<X> = true>
0361     Q_NODISCARD_CTOR
0362     inline QSharedPointer(const QWeakPointer<X> &other) : value(nullptr), d(nullptr)
0363     { *this = other; }
0364 
0365     template <class X, IfCompatible<X> = true>
0366     inline QSharedPointer<T> &operator=(const QWeakPointer<X> &other)
0367     { internalSet(other.d, other.value); return *this; }
0368 
0369     inline void swap(QSharedPointer &other) noexcept
0370     { this->internalSwap(other); }
0371 
0372     inline void reset() { clear(); }
0373     inline void reset(T *t)
0374     { QSharedPointer copy(t); swap(copy); }
0375     template <typename Deleter>
0376     inline void reset(T *t, Deleter deleter)
0377     { QSharedPointer copy(t, deleter); swap(copy); }
0378 
0379     template <class X>
0380     QSharedPointer<X> staticCast() const &
0381     {
0382         return qSharedPointerCast<X>(*this);
0383     }
0384 
0385     template <class X>
0386     QSharedPointer<X> staticCast() &&
0387     {
0388         return qSharedPointerCast<X>(std::move(*this));
0389     }
0390 
0391     template <class X>
0392     QSharedPointer<X> dynamicCast() const &
0393     {
0394         return qSharedPointerDynamicCast<X>(*this);
0395     }
0396 
0397     template <class X>
0398     QSharedPointer<X> dynamicCast() &&
0399     {
0400         return qSharedPointerDynamicCast<X>(std::move(*this));
0401     }
0402 
0403     template <class X>
0404     QSharedPointer<X> constCast() const &
0405     {
0406         return qSharedPointerConstCast<X>(*this);
0407     }
0408 
0409     template <class X>
0410     QSharedPointer<X> constCast() &&
0411     {
0412         return qSharedPointerConstCast<X>(std::move(*this));
0413     }
0414 
0415 #ifndef QT_NO_QOBJECT
0416     template <class X>
0417     QSharedPointer<X> objectCast() const &
0418     {
0419         return qSharedPointerObjectCast<X>(*this);
0420     }
0421 
0422     template <class X>
0423     QSharedPointer<X> objectCast() &&
0424     {
0425         return qSharedPointerObjectCast<X>(std::move(*this));
0426     }
0427 #endif
0428 
0429     inline void clear() { QSharedPointer copy; swap(copy); }
0430 
0431     [[nodiscard]] QWeakPointer<T> toWeakRef() const;
0432 
0433     template <typename... Args>
0434     [[nodiscard]] static QSharedPointer create(Args && ...arguments)
0435     {
0436         typedef QtSharedPointer::ExternalRefCountWithContiguousData<T> Private;
0437 # ifdef QT_SHAREDPOINTER_TRACK_POINTERS
0438         typename Private::DestroyerFn destroy = &Private::safetyCheckDeleter;
0439 # else
0440         typename Private::DestroyerFn destroy = &Private::deleter;
0441 # endif
0442         typename Private::DestroyerFn noDestroy = &Private::noDeleter;
0443         QSharedPointer result(Qt::Uninitialized);
0444         typename std::remove_cv<T>::type *ptr;
0445         result.d = Private::create(&ptr, noDestroy);
0446 
0447         // now initialize the data
0448         new (ptr) T(std::forward<Args>(arguments)...);
0449         result.value.reset(ptr);
0450         result.d->destroyer = destroy;
0451         result.d->setQObjectShared(result.value.get(), true);
0452 # ifdef QT_SHAREDPOINTER_TRACK_POINTERS
0453         internalSafetyCheckAdd(result.d, result.value.get());
0454 # endif
0455         result.enableSharedFromThis(result.data());
0456         return result;
0457     }
0458 
0459     template <typename X>
0460     bool owner_before(const QSharedPointer<X> &other) const noexcept
0461     { return std::less<>()(d, other.d); }
0462     template <typename X>
0463     bool owner_before(const QWeakPointer<X> &other) const noexcept
0464     { return std::less<>()(d, other.d); }
0465 
0466     template <typename X>
0467     bool owner_equal(const QSharedPointer<X> &other) const noexcept
0468     { return d == other.d; }
0469     template <typename X>
0470     bool owner_equal(const QWeakPointer<X> &other) const noexcept
0471     { return d == other.d; }
0472 
0473     size_t owner_hash() const noexcept
0474     { return std::hash<Data *>()(d); }
0475 
0476 private:
0477     template <typename X>
0478     friend bool comparesEqual(const QSharedPointer &lhs, const QSharedPointer<X> &rhs) noexcept
0479     { return lhs.data() == rhs.data(); }
0480     template <typename X>
0481     friend Qt::strong_ordering
0482     compareThreeWay(const QSharedPointer &lhs, const QSharedPointer<X> &rhs) noexcept
0483     {
0484         return Qt::compareThreeWay(lhs.value, rhs.data());
0485     }
0486     QT_DECLARE_ORDERING_OPERATORS_HELPER(STRONG, QSharedPointer<T>, QSharedPointer<X>,
0487                                          /* non-constexpr */, noexcept(true),
0488                                          template <typename X>)
0489 
0490     template <typename X>
0491     friend bool comparesEqual(const QSharedPointer &lhs, X *rhs) noexcept
0492     { return lhs.data() == rhs; }
0493     template <typename X>
0494     friend Qt::strong_ordering compareThreeWay(const QSharedPointer &lhs, X *rhs) noexcept
0495     { return Qt::compareThreeWay(lhs.value, rhs); }
0496     Q_DECLARE_STRONGLY_ORDERED(QSharedPointer, X*, template <typename X>)
0497 
0498     friend bool comparesEqual(const QSharedPointer &lhs, std::nullptr_t) noexcept
0499     { return lhs.data() == nullptr; }
0500     friend Qt::strong_ordering
0501     compareThreeWay(const QSharedPointer &lhs, std::nullptr_t) noexcept
0502     { return Qt::compareThreeWay(lhs.value, nullptr); }
0503     Q_DECLARE_STRONGLY_ORDERED(QSharedPointer, std::nullptr_t)
0504 
0505     Q_NODISCARD_CTOR
0506     explicit QSharedPointer(Qt::Initialization) {}
0507 
0508     void deref() noexcept
0509     { deref(d); }
0510     static void deref(Data *dd) noexcept
0511     {
0512         if (!dd) return;
0513         if (!dd->strongref.deref()) {
0514             dd->destroy();
0515         }
0516         if (!dd->weakref.deref())
0517             delete dd;
0518     }
0519 
0520     template <class X>
0521     inline void enableSharedFromThis(const QEnableSharedFromThis<X> *ptr)
0522     {
0523         ptr->initializeFromSharedPointer(constCast<typename std::remove_cv<T>::type>());
0524     }
0525 
0526     inline void enableSharedFromThis(...) {}
0527 
0528     template <typename X, typename Deleter>
0529     inline void internalConstruct(X *ptr, Deleter deleter)
0530     {
0531         typedef QtSharedPointer::ExternalRefCountWithCustomDeleter<X, Deleter> Private;
0532 # ifdef QT_SHAREDPOINTER_TRACK_POINTERS
0533         typename Private::DestroyerFn actualDeleter = &Private::safetyCheckDeleter;
0534 # else
0535         typename Private::DestroyerFn actualDeleter = &Private::deleter;
0536 # endif
0537         d = Private::create(ptr, deleter, actualDeleter);
0538 
0539 #ifdef QT_SHAREDPOINTER_TRACK_POINTERS
0540         internalSafetyCheckAdd(d, ptr);
0541 #endif
0542         enableSharedFromThis(ptr);
0543     }
0544 
0545     void internalSwap(QSharedPointer &other) noexcept
0546     {
0547         qt_ptr_swap(d, other.d);
0548         qt_ptr_swap(this->value, other.value);
0549     }
0550 
0551     template <class X> friend class QSharedPointer;
0552     template <class X> friend class QWeakPointer;
0553     template <class X, class Y> friend QSharedPointer<X> QtSharedPointer::copyAndSetPointer(X * ptr, const QSharedPointer<Y> &src);
0554     template <class X, class Y> friend QSharedPointer<X> QtSharedPointer::movePointer(X * ptr, QSharedPointer<Y> &&src);
0555     void ref() const noexcept { d->weakref.ref(); d->strongref.ref(); }
0556 
0557     inline void internalSet(Data *o, T *actual)
0558     {
0559         if (o) {
0560             // increase the strongref, but never up from zero
0561             // or less (-1 is used by QWeakPointer on untracked QObject)
0562             int tmp = o->strongref.loadRelaxed();
0563             while (tmp > 0) {
0564                 // try to increment from "tmp" to "tmp + 1"
0565                 if (o->strongref.testAndSetRelaxed(tmp, tmp + 1))
0566                     break;   // succeeded
0567                 tmp = o->strongref.loadRelaxed();  // failed, try again
0568             }
0569 
0570             if (tmp > 0)
0571                 o->weakref.ref();
0572             else
0573                 o = nullptr;
0574         }
0575 
0576         qt_ptr_swap(d, o);
0577         this->value.reset(actual);
0578         if (!d || d->strongref.loadRelaxed() == 0)
0579             this->value = nullptr;
0580 
0581         // dereference saved data
0582         deref(o);
0583     }
0584 
0585     Qt::totally_ordered_wrapper<Type *> value;
0586     Data *d;
0587 };
0588 
0589 template <class T>
0590 class QWeakPointer
0591 {
0592     typedef QtSharedPointer::ExternalRefCountData Data;
0593     template <typename X>
0594     using IfCompatible = std::enable_if_t<std::conjunction_v<
0595             std::negation<std::is_same<T, X>>, // don't make accidental copy/move SMFs
0596             std::is_convertible<X*, T*>
0597         >, bool>;
0598 
0599     template <typename X>
0600     using IfVirtualBase = typename std::enable_if<qxp::is_virtual_base_of_v<T, X>, bool>::type;
0601 
0602     template <typename X>
0603     using IfNotVirtualBase = typename std::enable_if<!qxp::is_virtual_base_of_v<T, X>, bool>::type;
0604 
0605 public:
0606     typedef T element_type;
0607     typedef T value_type;
0608     typedef value_type *pointer;
0609     typedef const value_type *const_pointer;
0610     typedef value_type &reference;
0611     typedef const value_type &const_reference;
0612     typedef qptrdiff difference_type;
0613 
0614     bool isNull() const noexcept { return d == nullptr || d->strongref.loadRelaxed() == 0 || value == nullptr; }
0615     explicit operator bool() const noexcept { return !isNull(); }
0616     bool operator !() const noexcept { return isNull(); }
0617 
0618     Q_NODISCARD_CTOR
0619     constexpr QWeakPointer() noexcept : d(nullptr), value(nullptr) { }
0620     inline ~QWeakPointer() { if (d && !d->weakref.deref()) delete d; }
0621 
0622     Q_NODISCARD_CTOR
0623     QWeakPointer(const QWeakPointer &other) noexcept : d(other.d), value(other.value)
0624     { if (d) d->weakref.ref(); }
0625     Q_NODISCARD_CTOR
0626     QWeakPointer(QWeakPointer &&other) noexcept
0627         : d(other.d), value(other.value)
0628     {
0629         other.d = nullptr;
0630         other.value = nullptr;
0631     }
0632     QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QWeakPointer)
0633 
0634     template <class X, IfCompatible<X> = true, IfNotVirtualBase<X> = true>
0635     Q_NODISCARD_CTOR
0636     QWeakPointer(QWeakPointer<X> &&other) noexcept
0637         : d(std::exchange(other.d, nullptr)),
0638           value(std::exchange(other.value, nullptr))
0639     {
0640     }
0641 
0642     template <class X, IfCompatible<X> = true, IfVirtualBase<X> = true>
0643     Q_NODISCARD_CTOR
0644     QWeakPointer(QWeakPointer<X> &&other) noexcept
0645         : d(other.d), value(other.toStrongRef().get()) // must go through QSharedPointer, see below
0646     {
0647         other.d = nullptr;
0648         other.value = nullptr;
0649     }
0650 
0651     template <class X, IfCompatible<X> = true>
0652     QWeakPointer &operator=(QWeakPointer<X> &&other) noexcept
0653     {
0654         QWeakPointer moved(std::move(other));
0655         swap(moved);
0656         return *this;
0657     }
0658 
0659     QWeakPointer &operator=(const QWeakPointer &other) noexcept
0660     {
0661         QWeakPointer copy(other);
0662         swap(copy);
0663         return *this;
0664     }
0665 
0666     void swap(QWeakPointer &other) noexcept
0667     {
0668         qt_ptr_swap(this->d, other.d);
0669         qt_ptr_swap(this->value, other.value);
0670     }
0671 
0672     Q_NODISCARD_CTOR
0673     inline QWeakPointer(const QSharedPointer<T> &o) : d(o.d), value(o.data())
0674     { if (d) d->weakref.ref();}
0675     inline QWeakPointer &operator=(const QSharedPointer<T> &o)
0676     {
0677         internalSet(o.d, o.value.get());
0678         return *this;
0679     }
0680 
0681     template <class X, IfCompatible<X> = true>
0682     Q_NODISCARD_CTOR
0683     inline QWeakPointer(const QWeakPointer<X> &o) : d(nullptr), value(nullptr)
0684     { *this = o; }
0685 
0686     template <class X, IfCompatible<X> = true>
0687     inline QWeakPointer &operator=(const QWeakPointer<X> &o)
0688     {
0689         // conversion between X and T could require access to the virtual table
0690         // so force the operation to go through QSharedPointer
0691         *this = o.toStrongRef();
0692         return *this;
0693     }
0694 
0695     template <class X, IfCompatible<X> = true>
0696     Q_NODISCARD_CTOR
0697     inline QWeakPointer(const QSharedPointer<X> &o) : d(nullptr), value(nullptr)
0698     { *this = o; }
0699 
0700     template <class X, IfCompatible<X> = true>
0701     inline QWeakPointer &operator=(const QSharedPointer<X> &o)
0702     {
0703         internalSet(o.d, o.data());
0704         return *this;
0705     }
0706 
0707     inline void clear() { *this = QWeakPointer(); }
0708 
0709     [[nodiscard]] QSharedPointer<T> toStrongRef() const { return QSharedPointer<T>(*this); }
0710     // std::weak_ptr compatibility:
0711     [[nodiscard]] QSharedPointer<T> lock() const { return toStrongRef(); }
0712 
0713     template <class X>
0714     bool operator==(const QWeakPointer<X> &o) const noexcept
0715     { return d == o.d && value == static_cast<const T *>(o.value); }
0716 
0717     template <class X>
0718     bool operator!=(const QWeakPointer<X> &o) const noexcept
0719     { return !(*this == o); }
0720 
0721     template <class X>
0722     bool operator==(const QSharedPointer<X> &o) const noexcept
0723     { return d == o.d; }
0724 
0725     template <class X>
0726     bool operator!=(const QSharedPointer<X> &o) const noexcept
0727     { return !(*this == o); }
0728 
0729     template <typename X>
0730     friend bool operator==(const QSharedPointer<X> &p1, const QWeakPointer &p2) noexcept
0731     { return p2 == p1; }
0732     template <typename X>
0733     friend bool operator!=(const QSharedPointer<X> &p1, const QWeakPointer &p2) noexcept
0734     { return p2 != p1; }
0735 
0736     friend bool operator==(const QWeakPointer &p, std::nullptr_t)
0737     { return p.isNull(); }
0738     friend bool operator==(std::nullptr_t, const QWeakPointer &p)
0739     { return p.isNull(); }
0740     friend bool operator!=(const QWeakPointer &p, std::nullptr_t)
0741     { return !p.isNull(); }
0742     friend bool operator!=(std::nullptr_t, const QWeakPointer &p)
0743     { return !p.isNull(); }
0744 
0745     template <typename X>
0746     bool owner_before(const QWeakPointer<X> &other) const noexcept
0747     { return std::less<>()(d, other.d); }
0748     template <typename X>
0749     bool owner_before(const QSharedPointer<X> &other) const noexcept
0750     { return std::less<>()(d, other.d); }
0751 
0752     template <typename X>
0753     bool owner_equal(const QWeakPointer<X> &other) const noexcept
0754     { return d == other.d; }
0755     template <typename X>
0756     bool owner_equal(const QSharedPointer<X> &other) const noexcept
0757     { return d == other.d; }
0758 
0759     size_t owner_hash() const noexcept
0760     { return std::hash<Data *>()(d); }
0761 
0762 private:
0763     friend struct QtPrivate::EnableInternalData;
0764     template <class X> friend class QSharedPointer;
0765     template <class X> friend class QWeakPointer;
0766     template <class X> friend class QPointer;
0767 
0768     template <class X>
0769     inline QWeakPointer &assign(X *ptr)
0770     { return *this = QWeakPointer<T>(ptr, true); }
0771 
0772 #ifndef QT_NO_QOBJECT
0773     Q_NODISCARD_CTOR
0774     QWeakPointer(T *ptr, bool)
0775         : d{ptr ? Data::getAndRef(ptr) : nullptr}, value{ptr}
0776     { }
0777 
0778     template <class X, IfCompatible<X> = true>
0779     Q_NODISCARD_CTOR
0780     inline QWeakPointer(X *ptr, bool) : d(ptr ? Data::getAndRef(ptr) : nullptr), value(ptr)
0781     { }
0782 #endif
0783 
0784     inline void internalSet(Data *o, T *actual)
0785     {
0786         if (d == o) return;
0787         if (o)
0788             o->weakref.ref();
0789         if (d && !d->weakref.deref())
0790             delete d;
0791         d = o;
0792         value = actual;
0793     }
0794 
0795     // ### TODO - QTBUG-88102: remove all users of this API; no one should ever
0796     // access a weak pointer's data but the weak pointer itself
0797     inline T *internalData() const noexcept
0798     {
0799         return d == nullptr || d->strongref.loadRelaxed() == 0 ? nullptr : value;
0800     }
0801 
0802     Data *d;
0803     T *value;
0804 };
0805 
0806 namespace QtPrivate {
0807 struct EnableInternalData {
0808     template <typename T>
0809     static T *internalData(const QWeakPointer<T> &p) noexcept { return p.internalData(); }
0810 };
0811 // hack to delay name lookup to instantiation time by making
0812 // EnableInternalData a dependent name:
0813 template <typename T>
0814 struct EnableInternalDataWrap : EnableInternalData {};
0815 }
0816 
0817 template <class T>
0818 class QEnableSharedFromThis
0819 {
0820 protected:
0821     QEnableSharedFromThis() = default;
0822     QEnableSharedFromThis(const QEnableSharedFromThis &) {}
0823     QEnableSharedFromThis &operator=(const QEnableSharedFromThis &) { return *this; }
0824 
0825 public:
0826     inline QSharedPointer<T> sharedFromThis() { return QSharedPointer<T>(weakPointer); }
0827     inline QSharedPointer<const T> sharedFromThis() const { return QSharedPointer<const T>(weakPointer); }
0828 
0829 private:
0830     template <class X> friend class QSharedPointer;
0831     template <class X>
0832     inline void initializeFromSharedPointer(const QSharedPointer<X> &ptr) const
0833     {
0834         weakPointer = ptr;
0835     }
0836 
0837     mutable QWeakPointer<T> weakPointer;
0838 };
0839 
0840 //
0841 // operator-
0842 //
0843 template <class T, class X>
0844 Q_INLINE_TEMPLATE typename QSharedPointer<T>::difference_type operator-(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2)
0845 {
0846     return ptr1.data() - ptr2.data();
0847 }
0848 template <class T, class X>
0849 Q_INLINE_TEMPLATE typename QSharedPointer<T>::difference_type operator-(const QSharedPointer<T> &ptr1, X *ptr2)
0850 {
0851     return ptr1.data() - ptr2;
0852 }
0853 template <class T, class X>
0854 Q_INLINE_TEMPLATE typename QSharedPointer<X>::difference_type operator-(T *ptr1, const QSharedPointer<X> &ptr2)
0855 {
0856     return ptr1 - ptr2.data();
0857 }
0858 
0859 //
0860 // qHash
0861 //
0862 template <class T>
0863 Q_INLINE_TEMPLATE size_t qHash(const QSharedPointer<T> &ptr, size_t seed = 0)
0864 {
0865     return qHash(ptr.data(), seed);
0866 }
0867 
0868 
0869 template <class T>
0870 Q_INLINE_TEMPLATE QWeakPointer<T> QSharedPointer<T>::toWeakRef() const
0871 {
0872     return QWeakPointer<T>(*this);
0873 }
0874 
0875 template <class T>
0876 inline void swap(QSharedPointer<T> &p1, QSharedPointer<T> &p2) noexcept
0877 { p1.swap(p2); }
0878 
0879 template <class T>
0880 inline void swap(QWeakPointer<T> &p1, QWeakPointer<T> &p2) noexcept
0881 { p1.swap(p2); }
0882 
0883 namespace QtSharedPointer {
0884 // helper functions:
0885     template <class X, class T>
0886     Q_INLINE_TEMPLATE QSharedPointer<X> copyAndSetPointer(X *ptr, const QSharedPointer<T> &src)
0887     {
0888         QSharedPointer<X> result;
0889         result.internalSet(src.d, ptr);
0890         return result;
0891     }
0892 
0893     template <class X, class T>
0894     Q_INLINE_TEMPLATE QSharedPointer<X> movePointer(X *ptr, QSharedPointer<T> &&src)
0895     {
0896         QSharedPointer<X> result;
0897         result.d = std::exchange(src.d, nullptr);
0898         result.value.reset(ptr);
0899         src.value.reset(nullptr);
0900         return result;
0901     }
0902 }
0903 
0904 // cast operators
0905 template <class X, class T>
0906 Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerCast(const QSharedPointer<T> &src)
0907 {
0908     X *ptr = static_cast<X *>(src.data()); // if you get an error in this line, the cast is invalid
0909     return QtSharedPointer::copyAndSetPointer(ptr, src);
0910 }
0911 template <class X, class T>
0912 Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerCast(QSharedPointer<T> &&src)
0913 {
0914     X *ptr = static_cast<X *>(src.data()); // if you get an error in this line, the cast is invalid
0915     return QtSharedPointer::movePointer(ptr, std::move(src));
0916 }
0917 template <class X, class T>
0918 Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerCast(const QWeakPointer<T> &src)
0919 {
0920     return qSharedPointerCast<X>(src.toStrongRef());
0921 }
0922 
0923 template <class X, class T>
0924 Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerDynamicCast(const QSharedPointer<T> &src)
0925 {
0926     X *ptr = dynamic_cast<X *>(src.data()); // if you get an error in this line, the cast is invalid
0927     if (!ptr)
0928         return QSharedPointer<X>();
0929     return QtSharedPointer::copyAndSetPointer(ptr, src);
0930 }
0931 template <class X, class T>
0932 Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerDynamicCast(QSharedPointer<T> &&src)
0933 {
0934     X *ptr = dynamic_cast<X *>(src.data()); // if you get an error in this line, the cast is invalid
0935     if (!ptr)
0936         return QSharedPointer<X>();
0937     return QtSharedPointer::movePointer(ptr, std::move(src));
0938 }
0939 template <class X, class T>
0940 Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerDynamicCast(const QWeakPointer<T> &src)
0941 {
0942     return qSharedPointerDynamicCast<X>(src.toStrongRef());
0943 }
0944 
0945 template <class X, class T>
0946 Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerConstCast(const QSharedPointer<T> &src)
0947 {
0948     X *ptr = const_cast<X *>(src.data()); // if you get an error in this line, the cast is invalid
0949     return QtSharedPointer::copyAndSetPointer(ptr, src);
0950 }
0951 template <class X, class T>
0952 Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerConstCast(QSharedPointer<T> &&src)
0953 {
0954     X *ptr = const_cast<X *>(src.data()); // if you get an error in this line, the cast is invalid
0955     return QtSharedPointer::movePointer(ptr, std::move(src));
0956 }
0957 template <class X, class T>
0958 Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerConstCast(const QWeakPointer<T> &src)
0959 {
0960     return qSharedPointerConstCast<X>(src.toStrongRef());
0961 }
0962 
0963 template <class X, class T>
0964 Q_INLINE_TEMPLATE
0965 QWeakPointer<X> qWeakPointerCast(const QSharedPointer<T> &src)
0966 {
0967     return qSharedPointerCast<X>(src).toWeakRef();
0968 }
0969 
0970 #ifndef QT_NO_QOBJECT
0971 template <class X, class T>
0972 Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerObjectCast(const QSharedPointer<T> &src)
0973 {
0974     X *ptr = qobject_cast<X *>(src.data());
0975     if (!ptr)
0976         return QSharedPointer<X>();
0977     return QtSharedPointer::copyAndSetPointer(ptr, src);
0978 }
0979 template <class X, class T>
0980 Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerObjectCast(QSharedPointer<T> &&src)
0981 {
0982     X *ptr = qobject_cast<X *>(src.data());
0983     if (!ptr)
0984         return QSharedPointer<X>();
0985     return QtSharedPointer::movePointer(ptr, std::move(src));
0986 }
0987 template <class X, class T>
0988 Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerObjectCast(const QWeakPointer<T> &src)
0989 {
0990     return qSharedPointerObjectCast<X>(src.toStrongRef());
0991 }
0992 
0993 template <class X, class T>
0994 inline QSharedPointer<typename QtSharedPointer::RemovePointer<X>::Type>
0995 qobject_cast(const QSharedPointer<T> &src)
0996 {
0997     return qSharedPointerObjectCast<typename QtSharedPointer::RemovePointer<X>::Type>(src);
0998 }
0999 template <class X, class T>
1000 inline QSharedPointer<typename QtSharedPointer::RemovePointer<X>::Type>
1001 qobject_cast(QSharedPointer<T> &&src)
1002 {
1003     return qSharedPointerObjectCast<typename QtSharedPointer::RemovePointer<X>::Type>(std::move(src));
1004 }
1005 template <class X, class T>
1006 inline QSharedPointer<typename QtSharedPointer::RemovePointer<X>::Type>
1007 qobject_cast(const QWeakPointer<T> &src)
1008 {
1009     return qSharedPointerObjectCast<typename QtSharedPointer::RemovePointer<X>::Type>(src);
1010 }
1011 
1012 /// ### TODO - QTBUG-88102: make this use toStrongRef() (once support for
1013 /// storing non-managed QObjects in QWeakPointer is removed)
1014 template<typename T>
1015 QWeakPointer<typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<T*>::Value, T>::type>
1016 qWeakPointerFromVariant(const QVariant &variant)
1017 {
1018     return QWeakPointer<T>(qobject_cast<T*>(QtPrivate::EnableInternalData::internalData(QtSharedPointer::weakPointerFromVariant_internal(variant))));
1019 }
1020 template<typename T>
1021 QSharedPointer<typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<T*>::Value, T>::type>
1022 qSharedPointerFromVariant(const QVariant &variant)
1023 {
1024     return qSharedPointerObjectCast<T>(QtSharedPointer::sharedPointerFromVariant_internal(variant));
1025 }
1026 
1027 // std::shared_ptr helpers
1028 
1029 template <typename X, class T>
1030 std::shared_ptr<X> qobject_pointer_cast(const std::shared_ptr<T> &src)
1031 {
1032     using element_type = typename std::shared_ptr<X>::element_type;
1033     if (auto ptr = qobject_cast<element_type *>(src.get()))
1034         return std::shared_ptr<X>(src, ptr);
1035     return std::shared_ptr<X>();
1036 }
1037 
1038 template <typename X, class T>
1039 std::shared_ptr<X> qobject_pointer_cast(std::shared_ptr<T> &&src)
1040 {
1041     using element_type = typename std::shared_ptr<X>::element_type;
1042     auto castResult = qobject_cast<element_type *>(src.get());
1043     if (castResult) {
1044         // C++2a's move aliasing constructor will leave src empty.
1045         // Before C++2a we don't really know if the compiler has support for it.
1046         // The move aliasing constructor is the resolution for LWG2996,
1047         // which does not impose a feature-testing macro. So: clear src.
1048         return std::shared_ptr<X>(std::exchange(src, nullptr), castResult);
1049     }
1050     return std::shared_ptr<X>();
1051 }
1052 
1053 template <typename X, class T>
1054 std::shared_ptr<X> qSharedPointerObjectCast(const std::shared_ptr<T> &src)
1055 {
1056     return qobject_pointer_cast<X>(src);
1057 }
1058 
1059 template <typename X, class T>
1060 std::shared_ptr<X> qSharedPointerObjectCast(std::shared_ptr<T> &&src)
1061 {
1062     return qobject_pointer_cast<X>(std::move(src));
1063 }
1064 
1065 #endif
1066 
1067 template<typename T> Q_DECLARE_TYPEINFO_BODY(QWeakPointer<T>, Q_RELOCATABLE_TYPE);
1068 template<typename T> Q_DECLARE_TYPEINFO_BODY(QSharedPointer<T>, Q_RELOCATABLE_TYPE);
1069 
1070 
1071 QT_END_NAMESPACE
1072 
1073 #endif