44 #ifndef QSHAREDPOINTER_H
45 #error Do not include qsharedpointer_impl.h directly
49 #pragma qt_sync_skip_header_check
50 #pragma qt_sync_stop_processing
60 #pragma qt_sync_stop_processing
64 #include <QtCore/qatomic.h>
65 #include <QtCore/qobject.h>
66 #include <QtCore/qhashfunctions.h>
81 template <
class X,
class T>
83 template <
class X,
class T>
85 template <
class X,
class T>
89 template <
class X,
class T>
94 struct EnableInternalData;
106 template <
class T,
typename Klass,
typename RetVal>
108 {
if (
t) (
t->*memberDeleter)(); }
109 template <
class T,
typename Deleter>
148 #ifndef QT_NO_QOBJECT
159 inline void *
operator new(std::size_t) =
delete;
161 inline void *
operator new(std::size_t,
void *ptr) noexcept {
return ptr; }
162 inline void operator delete(
void *ptr) { ::operator
delete(ptr); }
163 inline void operator delete(
void *,
void *) { }
167 template <
class T,
typename Deleter>
198 template <
class T,
typename Deleter>
207 Self *realself =
static_cast<Self *
>(
self);
208 realself->
extra.execute();
211 realself->
extra.~CustomDeleter<
T, Deleter>();
281 #ifndef QT_NO_QOBJECT
290 template <
typename X>
316 template <
class X, IfCompatible<X> = true>
320 template <
class X,
typename Deleter, IfCompatible<X> = true>
324 template <
typename Deleter>
340 other.value =
nullptr;
344 template <
class X, IfCompatible<X> = true>
349 other.value =
nullptr;
352 template <
class X, IfCompatible<X> = true>
360 template <
class X, IfCompatible<X> = true>
364 template <
class X, IfCompatible<X> = true>
372 template <
class X, IfCompatible<X> = true>
376 template <
class X, IfCompatible<X> = true>
386 template <
typename Deleter>
393 return qSharedPointerCast<X, T>(*
this);
399 return qSharedPointerDynamicCast<X, T>(*
this);
405 return qSharedPointerConstCast<X, T>(*
this);
408 #ifndef QT_NO_QOBJECT
412 return qSharedPointerObjectCast<X, T>(*
this);
420 template <
typename...
Args>
424 # ifdef QT_SHAREDPOINTER_TRACK_POINTERS
425 typename Private::DestroyerFn
destroy = &Private::safetyCheckDeleter;
427 typename Private::DestroyerFn
destroy = &Private::deleter;
429 typename Private::DestroyerFn noDestroy = &Private::noDeleter;
435 new (ptr)
T(std::forward<Args>(
arguments)...);
439 # ifdef QT_SHAREDPOINTER_TRACK_POINTERS
446 #define DECLARE_COMPARE_SET(T1, A1, T2, A2) \
447 friend bool operator==(T1, T2) noexcept \
448 { return A1 == A2; } \
449 friend bool operator!=(T1, T2) noexcept \
452 #define DECLARE_TEMPLATE_COMPARE_SET(T1, A1, T2, A2) \
453 template <typename X> \
454 friend bool operator==(T1, T2) noexcept \
455 { return A1 == A2; } \
456 template <typename X> \
457 friend bool operator!=(T1, T2) noexcept \
465 #undef DECLARE_TEMPLATE_COMPARE_SET
466 #undef DECLARE_COMPARE_SET
476 if (!dd->strongref.deref()) {
479 if (!dd->weakref.deref())
491 template <
typename X,
typename Deleter>
495 # ifdef QT_SHAREDPOINTER_TRACK_POINTERS
496 typename Private::DestroyerFn actualDeleter = &Private::safetyCheckDeleter;
498 typename Private::DestroyerFn actualDeleter = &Private::deleter;
502 #ifdef QT_SHAREDPOINTER_TRACK_POINTERS
525 int tmp =
o->strongref.loadRelaxed();
528 if (
o->strongref.testAndSetRelaxed(tmp, tmp + 1))
530 tmp =
o->strongref.loadRelaxed();
536 o->checkQObjectShared(actual);
542 qt_ptr_swap(this->
value, actual);
544 this->
value =
nullptr;
558 template <
typename X>
583 other.value =
nullptr;
587 template <
class X, IfCompatible<X> = true>
592 other.value =
nullptr;
595 template <
class X, IfCompatible<X> = true>
612 qt_ptr_swap(this->d,
other.d);
620 internalSet(
o.d,
o.value);
624 template <
class X, IfCompatible<X> = true>
628 template <
class X, IfCompatible<X> = true>
633 *
this =
o.toStrongRef();
637 template <
class X, IfCompatible<X> = true>
641 template <
class X, IfCompatible<X> = true>
644 internalSet(
o.d,
o.data());
656 {
return d ==
o.d &&
value ==
static_cast<const T *
>(
o.value); }
660 {
return !(*
this ==
o); }
668 {
return !(*
this ==
o); }
670 template <
typename X>
673 template <
typename X>
678 {
return p.isNull(); }
680 {
return p.isNull(); }
682 {
return !
p.isNull(); }
684 {
return !
p.isNull(); }
696 #ifndef QT_NO_QOBJECT
697 template <
class X, IfCompatible<X> = true>
702 inline void internalSet(
Data *
o,
T *actual)
707 if (
d && !
d->weakref.deref())
715 inline T *internalData()
const noexcept
717 return d ==
nullptr ||
d->strongref.loadRelaxed() == 0 ?
nullptr :
value;
726 template <
typename T>
731 template <
typename T>
761 template <
class T,
class X>
766 template <
class T,
class X>
769 return ptr1.
data() - ptr2;
771 template <
class T,
class X>
774 return ptr1 - ptr2.
data();
780 template <
class T,
class X>
784 return std::less<CT>()(ptr1.
data(), ptr2.
data());
786 template <
class T,
class X>
790 return std::less<CT>()(ptr1.
data(), ptr2);
792 template <
class T,
class X>
796 return std::less<CT>()(ptr1, ptr2.
data());
825 template <
class X,
class T>
835 template <
class X,
class T>
838 X *ptr =
static_cast<X *
>(
src.data());
841 template <
class X,
class T>
844 return qSharedPointerCast<X, T>(
src.toStrongRef());
847 template <
class X,
class T>
850 X *ptr =
dynamic_cast<X *
>(
src.data());
855 template <
class X,
class T>
858 return qSharedPointerDynamicCast<X, T>(
src.toStrongRef());
861 template <
class X,
class T>
864 X *ptr =
const_cast<X *
>(
src.data());
867 template <
class X,
class T>
870 return qSharedPointerConstCast<X, T>(
src.toStrongRef());
873 template <
class X,
class T>
877 return qSharedPointerCast<X, T>(
src).toWeakRef();
880 #ifndef QT_NO_QOBJECT
881 template <
class X,
class T>
884 X *ptr = qobject_cast<X *>(
src.data());
887 template <
class X,
class T>
890 return qSharedPointerObjectCast<X>(
src.toStrongRef());
893 template <
class X,
class T>
899 template <
class X,
class T>
923 template <
typename X,
class T>
926 using element_type =
typename std::shared_ptr<X>::element_type;
927 return std::shared_ptr<X>(
src, qobject_cast<element_type *>(
src.get()));
930 template <
typename X,
class T>
933 using element_type =
typename std::shared_ptr<X>::element_type;
934 auto castResult = qobject_cast<element_type *>(
src.get());
940 return std::shared_ptr<X>(std::exchange(
src,
nullptr), castResult);
942 return std::shared_ptr<X>();
945 template <
typename X,
class T>
948 return qobject_pointer_cast<X>(
src);
951 template <
typename X,
class T>
954 return qobject_pointer_cast<X>(std::move(
src));
void storeRelaxed(T newValue) noexcept
T loadRelaxed() const noexcept
bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
A base class that allows obtaining a QSharedPointer for an object already managed by a shared pointer...
QSharedPointer< T > sharedFromThis()
QEnableSharedFromThis(const QEnableSharedFromThis &)
QEnableSharedFromThis & operator=(const QEnableSharedFromThis &)
QEnableSharedFromThis()=default
QSharedPointer< const T > sharedFromThis() const
template< typename Enum > size_t qHash(QFlags< Enum > flags, size_t seed=0) noexcept
QMargins operator-(const QMargins &m1, const QMargins &m2)
The QObject class is the base class of all Qt objects.
template< class T > T qobject_cast(const QObject *object)
void swap(QPixmap &other) noexcept
The QPointer class is a template class that provides guarded pointers to QObject.
The QSharedPointer class holds a strong reference to a shared pointer.
template< class X, class T > std::shared_ptr< X > qobject_pointer_cast(const std::shared_ptr< T > &src)
QSharedPointer(const QWeakPointer< X > &other)
QSharedPointer(X *ptr, Deleter deleter)
bool isNull() const noexcept
static void deref(Data *dd) noexcept
QSharedPointer & operator=(const QSharedPointer< X > &other)
QSharedPointer(const QSharedPointer &other) noexcept
QSharedPointer< X > constCast() const
DECLARE_TEMPLATE_COMPARE_SET(const QSharedPointer &p1, p1.data(), const QSharedPointer< X > &p2, p2.data()) private
bool operator!() const noexcept
void enableSharedFromThis(const QEnableSharedFromThis< X > *ptr)
constexpr QSharedPointer() noexcept
friend class QSharedPointer
template< class X > template< class T > QSharedPointer< X > qSharedPointerObjectCast(const QSharedPointer< T > &src)
The qSharedPointerObjectCast function is for casting a shared pointer.
QWeakPointer< T > toWeakRef() const
QSharedPointer< X > objectCast() const
T * data() const noexcept
void reset(T *t, Deleter deleter)
void internalSet(Data *o, T *actual)
QSharedPointer< X > dynamicCast() const
QSharedPointer(QSharedPointer &&other) noexcept
void enableSharedFromThis(...)
const value_type & const_reference
QSharedPointer & operator=(QSharedPointer< X > &&other) noexcept
QSharedPointer(std::nullptr_t, Deleter deleter)
QSharedPointer< T > & operator=(const QWeakPointer< X > &other)
QSharedPointer(QSharedPointer< X > &&other) noexcept
void internalConstruct(X *ptr, Deleter deleter)
const value_type * const_pointer
void internalSwap(QSharedPointer &other) noexcept
constexpr QSharedPointer(std::nullptr_t) noexcept
static QSharedPointer create(Args &&...arguments)
template< class X > template< class T > QSharedPointer< X > qSharedPointerCast(const QSharedPointer< T > &other)
QSharedPointer & operator=(const QSharedPointer &other) noexcept
QSharedPointer< X > staticCast() const
T * operator->() const noexcept
void swap(QSharedPointer &other) noexcept
void ref() const noexcept
template< class X > template< class T > QSharedPointer< X > qSharedPointerConstCast(const QSharedPointer< T > &src)
template< class X > template< class T > QSharedPointer< X > qSharedPointerDynamicCast(const QSharedPointer< T > &src)
QSharedPointer(const QSharedPointer< X > &other) noexcept
The QVariant class acts like a union for the most common Qt data types.
The QWeakPointer class holds a weak reference to a shared pointer.
friend bool operator==(const QWeakPointer &p, std::nullptr_t)
const value_type & const_reference
friend bool operator!=(const QWeakPointer &p, std::nullptr_t)
constexpr QWeakPointer() noexcept
QWeakPointer(QWeakPointer< X > &&other) noexcept
QWeakPointer(const QWeakPointer &other) noexcept
friend bool operator==(const QSharedPointer< X > &p1, const QWeakPointer &p2) noexcept
QWeakPointer(QWeakPointer &&other) noexcept
QWeakPointer & operator=(const QWeakPointer< X > &o)
QWeakPointer(const QWeakPointer< X > &o)
QWeakPointer & operator=(QWeakPointer< X > &&other) noexcept
QWeakPointer & operator=(const QWeakPointer &other) noexcept
friend bool operator==(std::nullptr_t, const QWeakPointer &p)
friend bool operator!=(const QSharedPointer< X > &p1, const QWeakPointer &p2) noexcept
bool operator==(const QSharedPointer< X > &o) const noexcept
QWeakPointer(const QSharedPointer< X > &o)
QSharedPointer< T > toStrongRef() const
QWeakPointer & operator=(const QSharedPointer< T > &o)
bool operator!() const noexcept
QWeakPointer & operator=(const QSharedPointer< X > &o)
bool operator==(const QWeakPointer< X > &o) const noexcept
QSharedPointer< T > lock() const
bool operator!=(const QWeakPointer< X > &o) const noexcept
const value_type * const_pointer
bool isNull() const noexcept
friend class QWeakPointer
template< class X > template< class T > QWeakPointer< X > qWeakPointerCast(const QWeakPointer< T > &src)
bool operator!=(const QSharedPointer< X > &o) const noexcept
friend bool operator!=(std::nullptr_t, const QWeakPointer &p)
QWeakPointer(const QSharedPointer< T > &o)
void swap(QWeakPointer &other) noexcept
QList< QVariant > arguments
auto it unsigned count const
constexpr Initialization Uninitialized
Q_CORE_EXPORT QWeakPointer< QObject > weakPointerFromVariant_internal(const QVariant &variant)
Q_CORE_EXPORT void internalSafetyCheckRemove(const void *)
QSharedPointer< X > copyAndSetPointer(X *ptr, const QSharedPointer< Y > &src)
void executeDeleter(T *t, RetVal(Klass::*memberDeleter)())
Q_CORE_EXPORT void internalSafetyCheckAdd(const void *, const volatile void *)
Q_CORE_EXPORT QSharedPointer< QObject > sharedPointerFromVariant_internal(const QVariant &variant)
EGLOutputLayerEXT EGLint EGLAttrib value
#define Q_DISABLE_COPY(Class)
#define QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(Class)
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
QWeakPointer< typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< T * >::Value, T >::type > qWeakPointerFromVariant(const QVariant &variant)
Q_DECLARE_TYPEINFO_BODY(QWeakPointer< T >, Q_RELOCATABLE_TYPE)
QSharedPointer< typename std::enable_if< QtPrivate::IsPointerToTypeDerivedFromQObject< T * >::Value, T >::type > qSharedPointerFromVariant(const QVariant &variant)
void swap(QSharedPointer< T > &p1, QSharedPointer< T > &p2) noexcept
#define DECLARE_COMPARE_SET(T1, A1, T2, A2)
HB_EXTERN hb_font_get_glyph_func_t void hb_destroy_func_t destroy
static T * internalData(const QWeakPointer< T > &p) noexcept
CustomDeleter(T *p, NormalDeleter)
CustomDeleter(T *p, Deleter d)
QBasicAtomicInt strongref
void(* DestroyerFn)(ExternalRefCountData *)
ExternalRefCountData(Qt::Initialization)
Q_CORE_EXPORT void setQObjectShared(const QObject *, bool enable)
Q_CORE_EXPORT void checkQObjectShared(const QObject *)
void checkQObjectShared(...)
ExternalRefCountData(DestroyerFn d)
void setQObjectShared(...)
static Q_CORE_EXPORT ExternalRefCountData * getAndRef(const QObject *)
static void deleter(ExternalRefCountData *self)
static void noDeleter(ExternalRefCountData *)
ExternalRefCountData Parent
std::remove_cv< T >::type NoCVType
static ExternalRefCountData * create(NoCVType **ptr, DestroyerFn destroy)
static void safetyCheckDeleter(ExternalRefCountData *self)
ExternalRefCountData BaseClass
CustomDeleter< T, Deleter > extra
static void safetyCheckDeleter(ExternalRefCountData *self)
static Self * create(T *ptr, Deleter userDeleter, DestroyerFn actualDeleter)
static void deleter(ExternalRefCountData *self)
ExternalRefCountWithCustomDeleter Self