From 0e85647ccf45641f8243c44b93271e55984680f9 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Sun, 11 Oct 2020 17:49:37 +0200 Subject: [PATCH] General cleanup of qshareddata.h Update the code to something more modern and make the two types more consistent. Change-Id: I524d33fea158e2ba7079fe836164eec03c45649b Reviewed-by: Giuseppe D'Angelo --- src/corelib/tools/qshareddata.cpp | 21 ++- src/corelib/tools/qshareddata.h | 246 ++++++++++++++---------------- 2 files changed, 134 insertions(+), 133 deletions(-) diff --git a/src/corelib/tools/qshareddata.cpp b/src/corelib/tools/qshareddata.cpp index 187bfa7a5d..899a94b65a 100644 --- a/src/corelib/tools/qshareddata.cpp +++ b/src/corelib/tools/qshareddata.cpp @@ -309,6 +309,15 @@ QT_BEGIN_NAMESPACE \sa data() */ +/*! \fn template void QSharedDataPointer::reset(T *ptr = nullptr) + \since 6.0 + + Sets the \e{d pointer} of \e this to \a ptr and increments \a{ptr}'s reference + count if \a ptr is not \nullptr. + The reference count of the old shared data object is decremented, + and the object deleted if the reference count reaches 0. + */ + /*! \fn template void QSharedDataPointer::swap(QSharedDataPointer &other) Swap this instance's shared data pointer with the shared data pointer in \a other. @@ -590,11 +599,13 @@ QT_BEGIN_NAMESPACE 0, the old shared data object is deleted. */ -/*! \fn template void QExplicitlySharedDataPointer::reset() - Resets \e this to be null - i.e., this function sets the - \e{d pointer} of \e this to \nullptr, but first it decrements - the reference count of the shared data object and deletes - the shared data object if the reference count became 0. +/*! \fn template void QExplicitlySharedDataPointer::reset(T *ptr = nullptr) + \since 6.0 + + Sets the \e{d pointer} of \e this to \a ptr and increments \a{ptr}'s reference + count if \a ptr is not \nullptr. + The reference count of the old shared data object is decremented, + and the object deleted if the reference count reaches 0. */ /*! \fn template T *QExplicitlySharedDataPointer::take() diff --git a/src/corelib/tools/qshareddata.h b/src/corelib/tools/qshareddata.h index a6638bfaf9..809e8171e3 100644 --- a/src/corelib/tools/qshareddata.h +++ b/src/corelib/tools/qshareddata.h @@ -54,76 +54,85 @@ class QSharedData public: mutable QAtomicInt ref; - inline QSharedData() noexcept : ref(0) { } - inline QSharedData(const QSharedData &) noexcept : ref(0) { } + QSharedData() noexcept : ref(0) { } + QSharedData(const QSharedData &) noexcept : ref(0) { } // using the assignment operator would lead to corruption in the ref-counting QSharedData &operator=(const QSharedData &) = delete; ~QSharedData() = default; }; -template class QSharedDataPointer +template +class QSharedDataPointer { public: typedef T Type; typedef T *pointer; - inline void detach() { if (d && d->ref.loadRelaxed() != 1) detach_helper(); } - inline T &operator*() { detach(); return *d; } - inline const T &operator*() const { return *d; } - inline T *operator->() { detach(); return d; } - inline const T *operator->() const noexcept { return d; } - inline operator T *() { detach(); return d; } - inline operator const T *() const noexcept { return d; } - inline T *data() { detach(); return d; } - inline T *get() { detach(); return d; } - inline const T *data() const noexcept { return d; } - inline const T *get() const noexcept { return d; } - inline const T *constData() const noexcept { return d; } + void detach() { if (d && d->ref.loadRelaxed() != 1) detach_helper(); } + T &operator*() { detach(); return *d; } + const T &operator*() const { return *d; } + T *operator->() { detach(); return d; } + const T *operator->() const noexcept { return d; } + operator T *() { detach(); return d; } + operator const T *() const noexcept { return d; } + T *data() { detach(); return d; } + T *get() { detach(); return d; } + const T *data() const noexcept { return d; } + const T *get() const noexcept { return d; } + const T *constData() const noexcept { return d; } - inline bool operator==(const QSharedDataPointer &other) const noexcept { return d == other.d; } - inline bool operator!=(const QSharedDataPointer &other) const noexcept { return d != other.d; } + QSharedDataPointer() noexcept : d(nullptr) { } + ~QSharedDataPointer() { if (d && !d->ref.deref()) delete d; } - inline QSharedDataPointer() noexcept : d(nullptr) { } - inline ~QSharedDataPointer() { if (d && !d->ref.deref()) delete d; } - - explicit inline QSharedDataPointer(T *data) noexcept : d(data) + explicit QSharedDataPointer(T *data) noexcept : d(data) { if (d) d->ref.ref(); } - inline QSharedDataPointer(const QSharedDataPointer &o) noexcept : d(o.d) + QSharedDataPointer(const QSharedDataPointer &o) noexcept : d(o.d) { if (d) d->ref.ref(); } - inline QSharedDataPointer &operator=(const QSharedDataPointer &o) noexcept + + void reset(T *ptr = nullptr) noexcept { - if (o.d != d) { - if (o.d) - o.d->ref.ref(); - T *old = d; - d = o.d; + if (ptr != d) { + if (ptr) + ptr->ref.ref(); + T *old = qExchange(d, ptr); if (old && !old->ref.deref()) delete old; } + } + + QSharedDataPointer &operator=(const QSharedDataPointer &o) noexcept + { + reset(o.d); return *this; } inline QSharedDataPointer &operator=(T *o) noexcept { - if (o != d) { - if (o) - o->ref.ref(); - T *old = d; - d = o; - if (old && !old->ref.deref()) - delete old; - } + reset(o); return *this; } QSharedDataPointer(QSharedDataPointer &&o) noexcept : d(qExchange(o.d, nullptr)) {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QSharedDataPointer) - inline operator bool () const noexcept { return d != nullptr; } - inline bool operator!() const noexcept { return !d; } + operator bool () const noexcept { return d != nullptr; } + bool operator!() const noexcept { return d == nullptr; } - inline void swap(QSharedDataPointer &other) noexcept + void swap(QSharedDataPointer &other) noexcept { qSwap(d, other.d); } + friend bool operator==(const QSharedDataPointer &p1, const QSharedDataPointer &p2) noexcept + { return p1.d == p2.d; } + friend bool operator!=(const QSharedDataPointer &p1, const QSharedDataPointer &p2) noexcept + { return p1.d != p2.d; } + friend bool operator==(const QSharedDataPointer &p1, std::nullptr_t) noexcept + { return !p1; } + friend bool operator!=(const QSharedDataPointer &p1, std::nullptr_t) noexcept + { return p1; } + friend bool operator==(std::nullptr_t, const QSharedDataPointer &p2) noexcept + { return !p2; } + friend bool operator!=(std::nullptr_t, const QSharedDataPointer &p2) noexcept + { return p2; } + protected: T *clone(); @@ -133,101 +142,93 @@ private: T *d; }; -template inline bool operator==(std::nullptr_t p1, const QSharedDataPointer &p2) noexcept -{ - Q_UNUSED(p1); - return !p2; -} - -template inline bool operator==(const QSharedDataPointer &p1, std::nullptr_t p2) noexcept -{ - Q_UNUSED(p2); - return !p1; -} - -template class QExplicitlySharedDataPointer +template +class QExplicitlySharedDataPointer { public: typedef T Type; typedef T *pointer; - inline T &operator*() const { return *d; } - inline T *operator->() noexcept { return d; } - inline T *operator->() const noexcept { return d; } - inline T *data() const noexcept { return d; } - inline T *get() const noexcept { return d; } - inline const T *constData() const noexcept { return d; } - inline T *take() noexcept { T *x = d; d = nullptr; return x; } + T &operator*() const { return *d; } + T *operator->() noexcept { return d; } + T *operator->() const noexcept { return d; } + explicit operator T *() { return d; } + explicit operator const T *() const noexcept { return d; } + T *data() const noexcept { return d; } + T *get() const noexcept { return d; } + const T *constData() const noexcept { return d; } + T *take() noexcept { T *x = d; d = nullptr; return x; } - inline void detach() { if (d && d->ref.loadRelaxed() != 1) detach_helper(); } + void detach() { if (d && d->ref.loadRelaxed() != 1) detach_helper(); } - inline void reset() noexcept - { - if(d && !d->ref.deref()) - delete d; + QExplicitlySharedDataPointer() noexcept : d(nullptr) { } + ~QExplicitlySharedDataPointer() { if (d && !d->ref.deref()) delete d; } - d = nullptr; - } - - inline operator bool () const noexcept { return d != nullptr; } - - inline bool operator==(const QExplicitlySharedDataPointer &other) const noexcept { return d == other.d; } - inline bool operator!=(const QExplicitlySharedDataPointer &other) const noexcept { return d != other.d; } - inline bool operator==(const T *ptr) const noexcept { return d == ptr; } - inline bool operator!=(const T *ptr) const noexcept { return d != ptr; } - - inline QExplicitlySharedDataPointer() noexcept : d(nullptr) { } - inline ~QExplicitlySharedDataPointer() { if (d && !d->ref.deref()) delete d; } - - explicit inline QExplicitlySharedDataPointer(T *data) noexcept : d(data) + explicit QExplicitlySharedDataPointer(T *data) noexcept : d(data) { if (d) d->ref.ref(); } - inline QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer &o) noexcept : d(o.d) + QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer &o) noexcept : d(o.d) { if (d) d->ref.ref(); } - template - inline QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer &o) noexcept + template + QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer &o) noexcept #ifdef QT_ENABLE_QEXPLICITLYSHAREDDATAPOINTER_STATICCAST : d(static_cast(o.data())) #else : d(o.data()) #endif + { if (d) d->ref.ref(); } + + void reset(T *ptr = nullptr) noexcept { - if(d) - d->ref.ref(); + if (ptr != d) { + if (ptr) + ptr->ref.ref(); + T *old = qExchange(d, ptr); + if (old && !old->ref.deref()) + delete old; + } } - inline QExplicitlySharedDataPointer &operator=(const QExplicitlySharedDataPointer &o) noexcept + QExplicitlySharedDataPointer &operator=(const QExplicitlySharedDataPointer &o) noexcept { - if (o.d != d) { - if (o.d) - o.d->ref.ref(); - T *old = d; - d = o.d; - if (old && !old->ref.deref()) - delete old; - } + reset(o.d); return *this; } - inline QExplicitlySharedDataPointer &operator=(T *o) noexcept + QExplicitlySharedDataPointer &operator=(T *o) noexcept { - if (o != d) { - if (o) - o->ref.ref(); - T *old = d; - d = o; - if (old && !old->ref.deref()) - delete old; - } + reset(o); return *this; } - inline QExplicitlySharedDataPointer(QExplicitlySharedDataPointer &&o) noexcept : d(qExchange(o.d, nullptr)) {} + QExplicitlySharedDataPointer(QExplicitlySharedDataPointer &&o) noexcept : d(qExchange(o.d, nullptr)) {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QExplicitlySharedDataPointer) - inline bool operator!() const noexcept { return !d; } + operator bool () const noexcept { return d != nullptr; } + bool operator!() const noexcept { return d == nullptr; } - inline void swap(QExplicitlySharedDataPointer &other) noexcept + void swap(QExplicitlySharedDataPointer &other) noexcept { qSwap(d, other.d); } + friend bool operator==(const QExplicitlySharedDataPointer &p1, const QExplicitlySharedDataPointer &p2) noexcept + { return p1.d == p2.d; } + friend bool operator!=(const QExplicitlySharedDataPointer &p1, const QExplicitlySharedDataPointer &p2) noexcept + { return p1.d != p2.d; } + friend bool operator==(const QExplicitlySharedDataPointer &p1, const T *ptr) noexcept + { return p1.d == ptr; } + friend bool operator!=(const QExplicitlySharedDataPointer &p1, const T *ptr) noexcept + { return p1.d != ptr; } + friend bool operator==(const T *ptr, const QExplicitlySharedDataPointer &p2) noexcept + { return ptr == p2.d; } + friend bool operator!=(const T *ptr, const QExplicitlySharedDataPointer &p2) noexcept + { return ptr != p2.d; } + friend bool operator==(const QExplicitlySharedDataPointer &p1, std::nullptr_t) noexcept + { return !p1; } + friend bool operator!=(const QExplicitlySharedDataPointer &p1, std::nullptr_t) noexcept + { return p1; } + friend bool operator==(std::nullptr_t, const QExplicitlySharedDataPointer &p2) noexcept + { return !p2; } + friend bool operator!=(std::nullptr_t, const QExplicitlySharedDataPointer &p2) noexcept + { return p2; } + protected: T *clone(); @@ -237,13 +238,14 @@ private: T *d; }; -template +// Declared here and as Q_OUTOFLINE_TEMPLATE to work-around MSVC bug causing missing symbols at link time. +template Q_INLINE_TEMPLATE T *QSharedDataPointer::clone() { return new T(*d); } -template +template Q_OUTOFLINE_TEMPLATE void QSharedDataPointer::detach_helper() { T *x = clone(); @@ -253,13 +255,13 @@ Q_OUTOFLINE_TEMPLATE void QSharedDataPointer::detach_helper() d = x; } -template +template Q_INLINE_TEMPLATE T *QExplicitlySharedDataPointer::clone() { return new T(*d); } -template +template Q_OUTOFLINE_TEMPLATE void QExplicitlySharedDataPointer::detach_helper() { T *x = clone(); @@ -269,33 +271,21 @@ Q_OUTOFLINE_TEMPLATE void QExplicitlySharedDataPointer::detach_helper() d = x; } -template inline bool operator==(std::nullptr_t p1, const QExplicitlySharedDataPointer &p2) noexcept -{ - Q_UNUSED(p1); - return !p2; -} - -template inline bool operator==(const QExplicitlySharedDataPointer &p1, std::nullptr_t p2) noexcept -{ - Q_UNUSED(p2); - return !p1; -} - -template -Q_INLINE_TEMPLATE void swap(QSharedDataPointer &p1, QSharedDataPointer &p2) +template +void swap(QSharedDataPointer &p1, QSharedDataPointer &p2) noexcept { p1.swap(p2); } -template -Q_INLINE_TEMPLATE void swap(QExplicitlySharedDataPointer &p1, QExplicitlySharedDataPointer &p2) +template +void swap(QExplicitlySharedDataPointer &p1, QExplicitlySharedDataPointer &p2) noexcept { p1.swap(p2); } -template -Q_INLINE_TEMPLATE size_t qHash(const QSharedDataPointer &ptr, size_t seed = 0) noexcept +template +size_t qHash(const QSharedDataPointer &ptr, size_t seed = 0) noexcept { return qHash(ptr.data(), seed); } -template -Q_INLINE_TEMPLATE size_t qHash(const QExplicitlySharedDataPointer &ptr, size_t seed = 0) noexcept +template +size_t qHash(const QExplicitlySharedDataPointer &ptr, size_t seed = 0) noexcept { return qHash(ptr.data(), seed); }