44 #ifndef QSHAREDPOINTER_H 45 #error Do not include qsharedpointer_impl.h directly 58 #pragma qt_sync_stop_processing 62 #include <QtCore/qatomic.h> 63 #include <QtCore/qobject.h> 77 # define QSHAREDPOINTER_VERIFY_AUTO_CAST(T, X) qt_noop() 80 template<
typename T>
inline void qt_sharedpointer_cast_check(T *) { }
81 # define QSHAREDPOINTER_VERIFY_AUTO_CAST(T, X) \ 82 qt_sharedpointer_cast_check<T>(static_cast<X *>(0)) 91 template <
class X,
class T>
93 template <
class X,
class T>
95 template <
class X,
class T>
99 template <
class X,
class T>
104 template <
class T>
class InternalRefCount;
105 template <
class T>
class ExternalRefCount;
110 Q_CORE_EXPORT void internalSafetyCheckAdd2(
const void *,
const volatile void *);
113 template <
class T,
typename Klass,
typename RetVal>
114 inline void executeDeleter(T *t, RetVal (Klass:: *memberDeleter)())
115 { (t->*memberDeleter)(); }
116 template <
class T,
typename Deleter>
117 inline void executeDeleter(T *t, Deleter
d)
119 template <
class T>
inline void normalDeleter(T *t) {
delete t; }
122 template <
class T>
struct RemovePointer;
123 template <
class T>
struct RemovePointer<T *> {
typedef T
Type; };
125 template <
class T>
struct RemovePointer<
QWeakPointer<T> > {
typedef T
Type; };
133 #ifndef Q_CC_NOKIAX86 134 typedef T *Basic:: *RestrictedBool;
138 typedef T element_type;
139 typedef T value_type;
140 typedef value_type *pointer;
141 typedef const value_type *const_pointer;
142 typedef value_type &reference;
143 typedef const value_type &const_reference;
146 inline T *
data()
const {
return value; }
147 inline bool isNull()
const {
return !
data(); }
148 #ifndef Q_CC_NOKIAX86 149 inline operator RestrictedBool()
const {
return isNull() ? 0 : &Basic::value; }
151 inline operator bool()
const {
return isNull() ? 0 : &Basic::value; }
153 inline bool operator !()
const {
return isNull(); }
155 inline T *operator->()
const {
return data(); }
158 inline Basic(T *
ptr = 0) : value(
ptr) { }
162 inline void internalConstruct(T *
ptr)
167 #if defined(Q_NO_TEMPLATE_FRIENDS) 170 template <
class X>
friend class QT_PREPEND_NAMESPACE(QWeakPointer);
182 struct ExternalRefCountData
187 inline ExternalRefCountData()
193 virtual inline ~ExternalRefCountData() {
Q_ASSERT(!weakref);
Q_ASSERT(strongref <= 0); }
198 virtual inline bool destroy() {
return false; }
200 #ifndef QT_NO_QOBJECT 204 inline void setQObjectShared(...) { }
210 struct ExternalRefCountWithDestroyFn:
public ExternalRefCountData
212 typedef void (*DestroyerFn)(ExternalRefCountData *);
213 DestroyerFn destroyer;
215 inline ExternalRefCountWithDestroyFn(DestroyerFn
d)
219 inline bool destroy() { destroyer(
this);
return true; }
220 inline void operator delete(
void *
ptr) { ::operator
delete(
ptr); }
221 inline void operator delete(
void *,
void *) { }
228 template <
class T,
typename Deleter>
229 struct ExternalRefCountWithCustomDeleter:
public ExternalRefCountWithDestroyFn
231 typedef ExternalRefCountWithCustomDeleter Self;
232 typedef ExternalRefCountWithDestroyFn BaseClass;
239 inline CustomDeleter(T *p, Deleter
d) : deleter(d), ptr(p) {}
246 static inline void deleter(ExternalRefCountData *
self)
248 Self *realself =
static_cast<Self *
>(
self);
249 executeDeleter(realself->extra.ptr, realself->extra.deleter);
252 realself->extra.~CustomDeleter();
254 static void safetyCheckDeleter(ExternalRefCountData *
self)
256 internalSafetyCheckRemove2(
self);
260 static inline Self *
create(T *
ptr, Deleter userDeleter)
262 # ifdef QT_SHAREDPOINTER_TRACK_POINTERS 263 DestroyerFn destroy = &safetyCheckDeleter;
265 DestroyerFn destroy = &deleter;
267 Self *
d =
static_cast<Self *
>(::operator
new(
sizeof(Self)));
270 new (&
d->extra) CustomDeleter(
ptr, userDeleter);
271 new (
d) BaseClass(destroy);
277 ExternalRefCountWithCustomDeleter();
278 ~ExternalRefCountWithCustomDeleter();
286 struct ExternalRefCountWithContiguousData:
public ExternalRefCountWithDestroyFn
288 typedef ExternalRefCountWithDestroyFn Parent;
291 static void deleter(ExternalRefCountData *
self)
293 ExternalRefCountWithContiguousData *that =
294 static_cast<ExternalRefCountWithContiguousData *
>(
self);
297 static void safetyCheckDeleter(ExternalRefCountData *
self)
299 internalSafetyCheckRemove2(
self);
303 static inline ExternalRefCountData *
create(T **
ptr)
305 # ifdef QT_SHAREDPOINTER_TRACK_POINTERS 306 DestroyerFn destroy = &safetyCheckDeleter;
308 DestroyerFn destroy = &deleter;
310 ExternalRefCountWithContiguousData *
d =
311 static_cast<ExternalRefCountWithContiguousData *
>(::operator
new(
sizeof(ExternalRefCountWithContiguousData)));
315 new (
d) Parent(destroy);
323 ExternalRefCountWithContiguousData();
324 ~ExternalRefCountWithContiguousData();
330 class ExternalRefCount:
public Basic<T>
333 typedef ExternalRefCountData
Data;
336 { deref(
d, this->value); }
337 static inline void deref(Data *
d, T *value)
340 if (!d->strongref.deref()) {
344 if (!d->weakref.deref())
348 inline void internalConstruct(T *
ptr)
350 #ifdef QT_SHAREDPOINTER_TRACK_POINTERS 351 internalConstruct<void (*)(T *)>(
ptr, normalDeleter);
357 internalFinishConstruction(ptr);
361 template <
typename Deleter>
362 inline void internalConstruct(T *ptr, Deleter deleter)
368 internalFinishConstruction(ptr);
371 inline void internalCreate()
375 Basic<T>::internalConstruct(ptr);
378 inline void internalFinishConstruction(T *ptr)
380 Basic<T>::internalConstruct(ptr);
381 #ifdef QT_SHAREDPOINTER_TRACK_POINTERS 382 if (ptr) internalSafetyCheckAdd2(d, ptr);
384 if (ptr) d->setQObjectShared(ptr,
true);
387 inline ExternalRefCount() : d(0) { }
391 { internalConstruct(ptr); }
392 template <
typename Deleter>
393 inline ExternalRefCount(T *ptr, Deleter deleter) : Basic<T>(
Qt::
Uninitialized)
394 { internalConstruct(ptr, deleter); }
396 inline ExternalRefCount(
const ExternalRefCount<T> &other) : Basic<T>(other), d(other.d)
399 inline ExternalRefCount(
const ExternalRefCount<X> &other) : Basic<T>(other.value), d(other.d)
401 inline ~ExternalRefCount() { deref(); }
404 inline void internalCopy(
const ExternalRefCount<X> &other)
407 T *actual = other.value;
411 qSwap(this->value, actual);
415 inline void internalSwap(ExternalRefCount &other)
418 qSwap(this->value, other.value);
421 #if defined(Q_NO_TEMPLATE_FRIENDS) 424 template <
class X>
friend class ExternalRefCount;
425 template <
class X>
friend class QT_PREPEND_NAMESPACE(QWeakPointer);
428 inline void ref()
const { d->weakref.ref(); d->strongref.ref(); }
430 inline void internalSet(Data *o, T *actual)
435 register int tmp = o->strongref;
438 if (o->strongref.testAndSetRelaxed(tmp, tmp + 1))
450 qSwap(this->value, actual);
451 if (!d || d->strongref == 0)
461 template<
class X> ExternalRefCount(
const InternalRefCount<X> &);
468 typedef typename QtSharedPointer::ExternalRefCount<T> BaseClass;
476 template <
typename Deleter>
483 BaseClass::internalCopy(other);
486 #ifdef Q_COMPILER_RVALUE_REFS 501 QSHAREDPOINTER_VERIFY_AUTO_CAST(T, X);
502 BaseClass::internalCopy(other);
508 { this->d = 0; *
this = other; }
512 { BaseClass::internalSet(other.d, other.value);
return *
this; }
520 return qSharedPointerCast<X, T>(*this);
526 return qSharedPointerDynamicCast<X, T>(*this);
532 return qSharedPointerConstCast<X, T>(*this);
535 #ifndef QT_NO_QOBJECT 539 return qSharedPointerObjectCast<X, T>(*this);
554 result.internalCreate();
557 new (result.data()) T();
558 result.internalFinishConstruction(result.data());
566 #ifndef Q_CC_NOKIAX86 569 typedef QtSharedPointer::ExternalRefCountData
Data;
572 typedef T element_type;
573 typedef T value_type;
574 typedef value_type *pointer;
575 typedef const value_type *const_pointer;
576 typedef value_type &reference;
577 typedef const value_type &const_reference;
580 inline bool isNull()
const {
return d == 0 || d->strongref == 0 || value == 0; }
581 #ifndef Q_CC_NOKIAX86 582 inline operator RestrictedBool()
const {
return isNull() ? 0 : &QWeakPointer::value; }
584 inline operator bool()
const {
return isNull() ? 0 : &QWeakPointer::value; }
586 inline bool operator !()
const {
return isNull(); }
587 inline T *
data()
const {
return d == 0 || d->strongref == 0 ? 0 : value; }
590 inline ~
QWeakPointer() {
if (d && !d->weakref.deref())
delete d; }
592 #ifndef QT_NO_QOBJECT 595 inline QWeakPointer(X *ptr) : d(ptr ? Data::getAndRef(ptr) : 0), value(ptr)
603 {
if (d) d->weakref.ref(); }
606 internalSet(o.d, o.value);
611 {
if (d) d->weakref.ref();}
614 internalSet(o.d, o.value);
633 {
return d == o.d && value ==
static_cast<const T *
>(o.value); }
637 {
return !(*
this == o); }
646 QSHAREDPOINTER_VERIFY_AUTO_CAST(T, X);
647 internalSet(o.d, o.
data());
657 {
return !(*
this == o); }
663 #if defined(QWEAKPOINTER_ENABLE_ARROW) 664 inline T *operator->()
const {
return data(); }
669 #if defined(Q_NO_TEMPLATE_FRIENDS) 675 inline void internalSet(Data *o, T *actual)
680 if (d && !d->weakref.deref())
693 template <
class T,
class X>
698 template <
class T,
class X>
704 template <
class T,
class X>
707 return ptr1.
data() == ptr2;
709 template <
class T,
class X>
712 return ptr1 == ptr2.
data();
714 template <
class T,
class X>
717 return !(ptr1 == ptr2);
719 template <
class T,
class X>
722 return !(ptr2 == ptr1);
725 template <
class T,
class X>
730 template <
class T,
class X>
739 template <
class T,
class X>
744 template <
class T,
class X>
747 return ptr1.
data() - ptr2;
749 template <
class T,
class X>
752 return ptr1 - ptr2.
data();
758 template <
class T,
class X>
763 template <
class T,
class X>
766 return ptr1.
data() < ptr2;
768 template <
class T,
class X>
771 return ptr1 < ptr2.
data();
809 template <
class X,
class T>
813 result.internalSet(src.d, ptr);
819 template <
class X,
class T>
822 register X *ptr =
static_cast<X *
>(src.
data());
823 return QtSharedPointer::copyAndSetPointer(ptr, src);
825 template <
class X,
class T>
828 return qSharedPointerCast<X, T>(src.
toStrongRef());
831 template <
class X,
class T>
834 register X *ptr =
dynamic_cast<X *
>(src.
data());
837 return QtSharedPointer::copyAndSetPointer(ptr, src);
839 template <
class X,
class T>
842 return qSharedPointerDynamicCast<X, T>(src.
toStrongRef());
845 template <
class X,
class T>
848 register X *ptr =
const_cast<X *
>(src.
data());
849 return QtSharedPointer::copyAndSetPointer(ptr, src);
851 template <
class X,
class T>
854 return qSharedPointerConstCast<X, T>(src.
toStrongRef());
857 template <
class X,
class T>
861 return qSharedPointerCast<X, T>(src).toWeakRef();
864 #ifndef QT_NO_QOBJECT 865 template <
class X,
class T>
869 return QtSharedPointer::copyAndSetPointer(ptr, src);
871 template <
class X,
class T>
874 return qSharedPointerObjectCast<X>(src.
toStrongRef());
877 template <
class X,
class T>
883 template <
class X,
class T>
T qobject_cast(QObject *object)
uint qHash(const QProcEnvKey &key)
timeval operator-(const timeval &t1, const timeval &t2)
QSharedPointer< X > qSharedPointerConstCast(const QSharedPointer< T > &other)
Returns a shared pointer to the pointer held by other, cast to type X.
The QSharedPointer class holds a strong reference to a shared pointer.
QSharedPointer< T > toStrongRef() const
Promotes this weak reference to a strong one and returns a QSharedPointer object holding that referen...
#define QT_END_NAMESPACE
This macro expands to.
QSharedPointer< X > qSharedPointerObjectCast(const QSharedPointer< T > &src)
static Expression::Ptr create(Expression *const expr, const YYLTYPE &sourceLocator, const ParserContext *const parseInfo)
static void clear(QVariant::Private *d)
QWeakPointer< T > toWeakRef() const
Returns a weak reference object that shares the pointer referenced by this object.
bool operator!=(QBool b1, bool b2)
The QObject class is the base class of all Qt objects.
QWeakPointer< X > qWeakPointerCast(const QWeakPointer< T > &src)
QSharedPointer< X > qSharedPointerConstCast(const QSharedPointer< T > &src)
QSharedPointer< X > qSharedPointerDynamicCast(const QSharedPointer< T > &other)
Returns a shared pointer to the pointer held by other, using a dynamic cast to type X to obtain an in...
#define QT_BEGIN_NAMESPACE
This macro expands to.
QIntegerForSizeof< void * >::Signed qptrdiff
#define QT_PREPEND_NAMESPACE(name)
This macro qualifies identifier with the full namespace.
#define Q_INLINE_TEMPLATE
static const char * data(const QByteArray &arr)
timeval operator*(const timeval &t1, int mul)
const T * ptr(const T &t)
QSharedPointer< X > qSharedPointerCast(const QSharedPointer< T > &other)
void qSwap(T &value1, T &value2)
QSharedPointer< X > qSharedPointerDynamicCast(const QSharedPointer< T > &src)
T * data() const
Returns the value of the pointer referenced by this object.
#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS)
Q_INLINE_TEMPLATE void swap(::QScopedPointer< T, Cleanup > &p1, ::QScopedPointer< T, Cleanup > &p2)
QSharedPointer< X > qSharedPointerObjectCast(const QSharedPointer< T > &other)
The qSharedPointerObjectCast function is for casting a shared pointer.
QSharedPointer< X > qSharedPointerCast(const QSharedPointer< T > &other)
Returns a shared pointer to the pointer held by other, cast to type X.
bool operator==(QBool b1, bool b2)
static bool isNull(const QVariant::Private *d)
The QWeakPointer class holds a weak reference to a shared pointer.