diff options
Diffstat (limited to 'src/corelib/tools/qsharedpointer_impl.h')
-rw-r--r-- | src/corelib/tools/qsharedpointer_impl.h | 116 |
1 files changed, 85 insertions, 31 deletions
diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index e9a5443121..0f769ffa86 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -305,23 +305,29 @@ public: typedef const value_type &const_reference; typedef qptrdiff difference_type; - inline T *data() const { return value; } - inline bool isNull() const { return !data(); } - inline operator RestrictedBool() const { return isNull() ? Q_NULLPTR : &QSharedPointer::value; } - inline bool operator !() const { return isNull(); } - inline T &operator*() const { return *data(); } - inline T *operator->() const { return data(); } - - QSharedPointer() Q_DECL_NOTHROW : value(Q_NULLPTR), d(Q_NULLPTR) {} + T *data() const Q_DECL_NOTHROW { return value; } + bool isNull() const Q_DECL_NOTHROW { return !data(); } + operator RestrictedBool() const Q_DECL_NOTHROW { return isNull() ? Q_NULLPTR : &QSharedPointer::value; } + bool operator !() const Q_DECL_NOTHROW { return isNull(); } + T &operator*() const { return *data(); } + T *operator->() const Q_DECL_NOTHROW { return data(); } + + Q_DECL_CONSTEXPR QSharedPointer() Q_DECL_NOTHROW : value(nullptr), d(nullptr) { } ~QSharedPointer() { deref(); } - inline explicit QSharedPointer(T *ptr) : value(ptr) // noexcept + Q_DECL_CONSTEXPR QSharedPointer(std::nullptr_t) Q_DECL_NOTHROW : value(nullptr), d(nullptr) { } + + template <class X> + inline explicit QSharedPointer(X *ptr) : value(ptr) // noexcept { internalConstruct(ptr, QtSharedPointer::NormalDeleter()); } - template <typename Deleter> - inline QSharedPointer(T *ptr, Deleter deleter) : value(ptr) // throws + template <class X, typename Deleter> + inline QSharedPointer(X *ptr, Deleter deleter) : value(ptr) // throws { internalConstruct(ptr, deleter); } + template <typename Deleter> + QSharedPointer(std::nullptr_t, Deleter) : value(nullptr), d(nullptr) { } + QSharedPointer(const QSharedPointer &other) Q_DECL_NOTHROW : value(other.value), d(other.d) { if (d) ref(); } QSharedPointer &operator=(const QSharedPointer &other) Q_DECL_NOTHROW @@ -363,7 +369,7 @@ public: #endif template <class X> - inline QSharedPointer(const QSharedPointer<X> &other) : value(other.value), d(other.d) + QSharedPointer(const QSharedPointer<X> &other) Q_DECL_NOTHROW : value(other.value), d(other.d) { if (d) ref(); } template <class X> @@ -511,15 +517,15 @@ private: inline void enableSharedFromThis(...) {} - template <typename Deleter> - inline void internalConstruct(T *ptr, Deleter deleter) + template <typename X, typename Deleter> + inline void internalConstruct(X *ptr, Deleter deleter) { if (!ptr) { d = Q_NULLPTR; return; } - typedef QtSharedPointer::ExternalRefCountWithCustomDeleter<T, Deleter> Private; + typedef QtSharedPointer::ExternalRefCountWithCustomDeleter<X, Deleter> Private; # ifdef QT_SHAREDPOINTER_TRACK_POINTERS typename Private::DestroyerFn actualDeleter = &Private::safetyCheckDeleter; # else @@ -598,10 +604,10 @@ public: typedef const value_type &const_reference; typedef qptrdiff difference_type; - inline bool isNull() const { return d == Q_NULLPTR || d->strongref.load() == 0 || value == Q_NULLPTR; } - inline operator RestrictedBool() const { return isNull() ? Q_NULLPTR : &QWeakPointer::value; } - inline bool operator !() const { return isNull(); } - inline T *data() const { return d == Q_NULLPTR || d->strongref.load() == 0 ? Q_NULLPTR : value; } + bool isNull() const Q_DECL_NOTHROW { return d == Q_NULLPTR || d->strongref.load() == 0 || value == Q_NULLPTR; } + operator RestrictedBool() const Q_DECL_NOTHROW { return isNull() ? Q_NULLPTR : &QWeakPointer::value; } + bool operator !() const Q_DECL_NOTHROW { return isNull(); } + T *data() const Q_DECL_NOTHROW { return d == Q_NULLPTR || d->strongref.load() == 0 ? Q_NULLPTR : value; } inline QWeakPointer() Q_DECL_NOTHROW : d(Q_NULLPTR), value(Q_NULLPTR) { } inline ~QWeakPointer() { if (d && !d->weakref.deref()) delete d; } @@ -668,11 +674,11 @@ public: } template <class X> - inline bool operator==(const QWeakPointer<X> &o) const + bool operator==(const QWeakPointer<X> &o) const Q_DECL_NOTHROW { return d == o.d && value == static_cast<const T *>(o.value); } template <class X> - inline bool operator!=(const QWeakPointer<X> &o) const + bool operator!=(const QWeakPointer<X> &o) const Q_DECL_NOTHROW { return !(*this == o); } template <class X> @@ -688,11 +694,11 @@ public: } template <class X> - inline bool operator==(const QSharedPointer<X> &o) const + bool operator==(const QSharedPointer<X> &o) const Q_DECL_NOTHROW { return d == o.d; } template <class X> - inline bool operator!=(const QSharedPointer<X> &o) const + bool operator!=(const QSharedPointer<X> &o) const Q_DECL_NOTHROW { return !(*this == o); } inline void clear() { *this = QWeakPointer(); } @@ -774,48 +780,96 @@ public: // operator== and operator!= // template <class T, class X> -bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2) +bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2) Q_DECL_NOTHROW { return ptr1.data() == ptr2.data(); } template <class T, class X> -bool operator!=(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2) +bool operator!=(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2) Q_DECL_NOTHROW { return ptr1.data() != ptr2.data(); } template <class T, class X> -bool operator==(const QSharedPointer<T> &ptr1, const X *ptr2) +bool operator==(const QSharedPointer<T> &ptr1, const X *ptr2) Q_DECL_NOTHROW { return ptr1.data() == ptr2; } template <class T, class X> -bool operator==(const T *ptr1, const QSharedPointer<X> &ptr2) +bool operator==(const T *ptr1, const QSharedPointer<X> &ptr2) Q_DECL_NOTHROW { return ptr1 == ptr2.data(); } template <class T, class X> -bool operator!=(const QSharedPointer<T> &ptr1, const X *ptr2) +bool operator!=(const QSharedPointer<T> &ptr1, const X *ptr2) Q_DECL_NOTHROW { return !(ptr1 == ptr2); } template <class T, class X> -bool operator!=(const T *ptr1, const QSharedPointer<X> &ptr2) +bool operator!=(const T *ptr1, const QSharedPointer<X> &ptr2) Q_DECL_NOTHROW { return !(ptr2 == ptr1); } template <class T, class X> -bool operator==(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2) +bool operator==(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2) Q_DECL_NOTHROW { return ptr2 == ptr1; } template <class T, class X> -bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2) +bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2) Q_DECL_NOTHROW { return ptr2 != ptr1; } +template<class T> +inline bool operator==(const QSharedPointer<T> &lhs, std::nullptr_t) Q_DECL_NOTHROW +{ + return lhs.isNull(); +} + +template<class T> +inline bool operator!=(const QSharedPointer<T> &lhs, std::nullptr_t) Q_DECL_NOTHROW +{ + return !lhs.isNull(); +} + +template<class T> +inline bool operator==(std::nullptr_t, const QSharedPointer<T> &rhs) Q_DECL_NOTHROW +{ + return rhs.isNull(); +} + +template<class T> +inline bool operator!=(std::nullptr_t, const QSharedPointer<T> &rhs) Q_DECL_NOTHROW +{ + return !rhs.isNull(); +} + +template<class T> +inline bool operator==(const QWeakPointer<T> &lhs, std::nullptr_t) Q_DECL_NOTHROW +{ + return lhs.isNull(); +} + +template<class T> +inline bool operator!=(const QWeakPointer<T> &lhs, std::nullptr_t) Q_DECL_NOTHROW +{ + return !lhs.isNull(); +} + +template<class T> +inline bool operator==(std::nullptr_t, const QWeakPointer<T> &rhs) Q_DECL_NOTHROW +{ + return rhs.isNull(); +} + +template<class T> +inline bool operator!=(std::nullptr_t, const QWeakPointer<T> &rhs) Q_DECL_NOTHROW +{ + return !rhs.isNull(); +} + // // operator- // |