diff options
Diffstat (limited to 'src/corelib/tools/qshareddata.h')
-rw-r--r-- | src/corelib/tools/qshareddata.h | 62 |
1 files changed, 25 insertions, 37 deletions
diff --git a/src/corelib/tools/qshareddata.h b/src/corelib/tools/qshareddata.h index 6930cb96a5..f123f8e7b9 100644 --- a/src/corelib/tools/qshareddata.h +++ b/src/corelib/tools/qshareddata.h @@ -52,17 +52,21 @@ QT_BEGIN_NAMESPACE template <class T> class QSharedDataPointer; -class Q_CORE_EXPORT QSharedData +class +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) +Q_CORE_EXPORT +#endif +QSharedData { public: mutable QAtomicInt ref; - inline QSharedData() : ref(0) { } - inline QSharedData(const QSharedData &) : ref(0) { } + inline QSharedData() noexcept : ref(0) { } + inline QSharedData(const QSharedData &) noexcept : ref(0) { } -private: // using the assignment operator would lead to corruption in the ref-counting - QSharedData &operator=(const QSharedData &); + QSharedData &operator=(const QSharedData &) = delete; + ~QSharedData() = default; }; template <class T> class QSharedDataPointer @@ -71,7 +75,7 @@ public: typedef T Type; typedef T *pointer; - inline void detach() { if (d && d->ref.load() != 1) detach_helper(); } + 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; } @@ -88,7 +92,7 @@ public: inline QSharedDataPointer() { d = nullptr; } inline ~QSharedDataPointer() { if (d && !d->ref.deref()) delete d; } - explicit QSharedDataPointer(T *data) Q_DECL_NOTHROW; + explicit QSharedDataPointer(T *data) noexcept; inline QSharedDataPointer(const QSharedDataPointer<T> &o) : d(o.d) { if (d) d->ref.ref(); } inline QSharedDataPointer<T> & operator=(const QSharedDataPointer<T> &o) { if (o.d != d) { @@ -112,19 +116,17 @@ public: } return *this; } -#ifdef Q_COMPILER_RVALUE_REFS - QSharedDataPointer(QSharedDataPointer &&o) Q_DECL_NOTHROW : d(o.d) { o.d = nullptr; } - inline QSharedDataPointer<T> &operator=(QSharedDataPointer<T> &&other) Q_DECL_NOTHROW + QSharedDataPointer(QSharedDataPointer &&o) noexcept : d(o.d) { o.d = nullptr; } + inline QSharedDataPointer<T> &operator=(QSharedDataPointer<T> &&other) noexcept { QSharedDataPointer moved(std::move(other)); swap(moved); return *this; } -#endif inline bool operator!() const { return !d; } - inline void swap(QSharedDataPointer &other) Q_DECL_NOTHROW + inline void swap(QSharedDataPointer &other) noexcept { qSwap(d, other.d); } protected: @@ -161,7 +163,7 @@ public: inline const T *constData() const { return d; } inline T *take() { T *x = d; d = nullptr; return x; } - inline void detach() { if (d && d->ref.load() != 1) detach_helper(); } + inline void detach() { if (d && d->ref.loadRelaxed() != 1) detach_helper(); } inline void reset() { @@ -181,7 +183,7 @@ public: inline QExplicitlySharedDataPointer() { d = nullptr; } inline ~QExplicitlySharedDataPointer() { if (d && !d->ref.deref()) delete d; } - explicit QExplicitlySharedDataPointer(T *data) Q_DECL_NOTHROW; + explicit QExplicitlySharedDataPointer(T *data) noexcept; inline QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<T> &o) : d(o.d) { if (d) d->ref.ref(); } template<class X> @@ -218,19 +220,17 @@ public: } return *this; } -#ifdef Q_COMPILER_RVALUE_REFS - inline QExplicitlySharedDataPointer(QExplicitlySharedDataPointer &&o) Q_DECL_NOTHROW : d(o.d) { o.d = nullptr; } - inline QExplicitlySharedDataPointer<T> &operator=(QExplicitlySharedDataPointer<T> &&other) Q_DECL_NOTHROW + inline QExplicitlySharedDataPointer(QExplicitlySharedDataPointer &&o) noexcept : d(o.d) { o.d = nullptr; } + inline QExplicitlySharedDataPointer<T> &operator=(QExplicitlySharedDataPointer<T> &&other) noexcept { QExplicitlySharedDataPointer moved(std::move(other)); swap(moved); return *this; } -#endif inline bool operator!() const { return !d; } - inline void swap(QExplicitlySharedDataPointer &other) Q_DECL_NOTHROW + inline void swap(QExplicitlySharedDataPointer &other) noexcept { qSwap(d, other.d); } protected: @@ -243,7 +243,7 @@ private: }; template <class T> -Q_INLINE_TEMPLATE QSharedDataPointer<T>::QSharedDataPointer(T *adata) Q_DECL_NOTHROW +Q_INLINE_TEMPLATE QSharedDataPointer<T>::QSharedDataPointer(T *adata) noexcept : d(adata) { if (d) d->ref.ref(); } @@ -280,7 +280,7 @@ Q_OUTOFLINE_TEMPLATE void QExplicitlySharedDataPointer<T>::detach_helper() } template <class T> -Q_INLINE_TEMPLATE QExplicitlySharedDataPointer<T>::QExplicitlySharedDataPointer(T *adata) Q_DECL_NOTHROW +Q_INLINE_TEMPLATE QExplicitlySharedDataPointer<T>::QExplicitlySharedDataPointer(T *adata) noexcept : d(adata) { if (d) d->ref.ref(); } @@ -297,32 +297,20 @@ template <class T> inline bool operator==(const QExplicitlySharedDataPointer<T> } template <class T> -Q_INLINE_TEMPLATE void qSwap(QSharedDataPointer<T> &p1, QSharedDataPointer<T> &p2) +Q_INLINE_TEMPLATE void swap(QSharedDataPointer<T> &p1, QSharedDataPointer<T> &p2) { p1.swap(p2); } template <class T> -Q_INLINE_TEMPLATE void qSwap(QExplicitlySharedDataPointer<T> &p1, QExplicitlySharedDataPointer<T> &p2) +Q_INLINE_TEMPLATE void swap(QExplicitlySharedDataPointer<T> &p1, QExplicitlySharedDataPointer<T> &p2) { p1.swap(p2); } -QT_END_NAMESPACE -namespace std { - template <class T> - Q_INLINE_TEMPLATE void swap(QT_PREPEND_NAMESPACE(QSharedDataPointer)<T> &p1, QT_PREPEND_NAMESPACE(QSharedDataPointer)<T> &p2) - { p1.swap(p2); } - - template <class T> - Q_INLINE_TEMPLATE void swap(QT_PREPEND_NAMESPACE(QExplicitlySharedDataPointer)<T> &p1, QT_PREPEND_NAMESPACE(QExplicitlySharedDataPointer)<T> &p2) - { p1.swap(p2); } -} -QT_BEGIN_NAMESPACE - template <class T> -Q_INLINE_TEMPLATE uint qHash(const QSharedDataPointer<T> &ptr, uint seed = 0) Q_DECL_NOTHROW +Q_INLINE_TEMPLATE uint qHash(const QSharedDataPointer<T> &ptr, uint seed = 0) noexcept { return qHash(ptr.data(), seed); } template <class T> -Q_INLINE_TEMPLATE uint qHash(const QExplicitlySharedDataPointer<T> &ptr, uint seed = 0) Q_DECL_NOTHROW +Q_INLINE_TEMPLATE uint qHash(const QExplicitlySharedDataPointer<T> &ptr, uint seed = 0) noexcept { return qHash(ptr.data(), seed); } |