diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/corelib/tools/qsharedpointer.cpp | 77 | ||||
| -rw-r--r-- | src/corelib/tools/qsharedpointer_impl.h | 95 |
2 files changed, 100 insertions, 72 deletions
diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp index 9ed48338e99..217a3a4ff43 100644 --- a/src/corelib/tools/qsharedpointer.cpp +++ b/src/corelib/tools/qsharedpointer.cpp @@ -16,12 +16,6 @@ \reentrant - \compares strong - \compareswith strong QSharedPointer<X> X* std::nullptr_t - Where X and T are compatible types, which means that they are either the same or one - is a base type of the other. - \endcompareswith - The QSharedPointer is an automatic, shared pointer in C++. It behaves exactly like a normal pointer for normal purposes, including respect for constness. @@ -992,66 +986,83 @@ */ /*! - \fn template<class T, class X> bool operator==(const QSharedPointer<T> &lhs, const QSharedPointer<X> &rhs) + \fn template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2) \relates QSharedPointer - Returns \c true if \a lhs and \a rhs refer to the same pointer. + Returns \c true if \a ptr1 and \a ptr2 refer to the same pointer. -//! [qsharedpointer-different-template-parameters] - If \a rhs's template parameter is different from \a lhs's, - QSharedPointer first needs to ensure that they are compatible types. - It will attempt to perform an automatic \tt static_cast to convert - the types \tt T and \tt X to their composite pointer type. - If \a rhs's template parameter is not a base or a derived type from - \a lhs's, you will get a compiler error. -//! [qsharedpointer-different-template-parameters] + If \a ptr2's template parameter is different from \a ptr1's, + QSharedPointer will attempt to perform an automatic \tt static_cast + to ensure that the pointers being compared are equal. If \a ptr2's + template parameter is not a base or a derived type from + \a ptr1's, you will get a compiler error. */ /*! - \fn template<class T, class X> bool operator!=(const QSharedPointer<T> &lhs, const QSharedPointer<X> &rhs) + \fn template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2) \relates QSharedPointer - Returns \c true if \a lhs and \a rhs refer to distinct pointers. + Returns \c true if \a ptr1 and \a ptr2 refer to distinct pointers. - \include qsharedpointer.cpp qsharedpointer-different-template-parameters + If \a ptr2's template parameter is different from \a ptr1's, + QSharedPointer will attempt to perform an automatic \tt static_cast + to ensure that the pointers being compared are equal. If \a ptr2's + template parameter is not a base or a derived type from + \a ptr1's, you will get a compiler error. */ /*! - \fn template<class T, class X> bool operator==(const QSharedPointer<T> &lhs, const X *rhs) + \fn template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const X *ptr2) \relates QSharedPointer - Returns \c true if \a lhs and \a rhs refer to the same pointer. + Returns \c true if \a ptr1 and \a ptr2 refer to the same pointer. - \include qsharedpointer.cpp qsharedpointer-different-template-parameters + If \a ptr2's type is different from \a ptr1's, + QSharedPointer will attempt to perform an automatic \tt static_cast + to ensure that the pointers being compared are equal. If \a ptr2's + type is not a base or a derived type from this + \a ptr1's, you will get a compiler error. */ /*! - \fn template<class T, class X> bool operator!=(const QSharedPointer<T> &lhs, const X *rhs) + \fn template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const X *ptr2) \relates QSharedPointer - Returns \c true if \a lhs and \a rhs refer to distinct pointers. + Returns \c true if \a ptr1 and \a ptr2 refer to distinct pointers. - \include qsharedpointer.cpp qsharedpointer-different-template-parameters + If \a ptr2's type is different from \a ptr1's, + QSharedPointer will attempt to perform an automatic \tt static_cast + to ensure that the pointers being compared are equal. If \a ptr2's + type is not a base or a derived type from this + \a ptr1's, you will get a compiler error. */ /*! - \fn template<class T, class X> bool operator==(const T *lhs, const QSharedPointer<X> &rhs) + \fn template<class T, class X> bool operator==(const T *ptr1, const QSharedPointer<X> &ptr2) \relates QSharedPointer - Returns \c true if the pointer \a lhs is the - same pointer as that referenced by \a rhs. + Returns \c true if the pointer \a ptr1 is the + same pointer as that referenced by \a ptr2. - \include qsharedpointer.cpp qsharedpointer-different-template-parameters + If \a ptr2's template parameter is different from \a ptr1's type, + QSharedPointer will attempt to perform an automatic \tt static_cast + to ensure that the pointers being compared are equal. If \a ptr2's + template parameter is not a base or a derived type from + \a ptr1's type, you will get a compiler error. */ /*! - \fn template<class T, class X> bool operator!=(const T *lhs, const QSharedPointer<X> &rhs) + \fn template<class T, class X> bool operator!=(const T *ptr1, const QSharedPointer<X> &ptr2) \relates QSharedPointer - Returns \c true if the pointer \a lhs is not the - same pointer as that referenced by \a rhs. + Returns \c true if the pointer \a ptr1 is not the + same pointer as that referenced by \a ptr2. - \include qsharedpointer.cpp qsharedpointer-different-template-parameters + If \a ptr2's template parameter is different from \a ptr1's type, + QSharedPointer will attempt to perform an automatic \tt static_cast + to ensure that the pointers being compared are equal. If \a ptr2's + template parameter is not a base or a derived type from + \a ptr1's type, you will get a compiler error. */ /*! diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index feb01c46d86..456be91d032 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -271,8 +271,8 @@ public: typedef const value_type &const_reference; typedef qptrdiff difference_type; - T *data() const noexcept { return value.get(); } - T *get() const noexcept { return value.get(); } + T *data() const noexcept { return value; } + T *get() const noexcept { return value; } bool isNull() const noexcept { return !data(); } explicit operator bool() const noexcept { return !isNull(); } bool operator !() const noexcept { return isNull(); } @@ -302,7 +302,7 @@ public: { internalConstruct(static_cast<T *>(nullptr), deleter); } Q_NODISCARD_CTOR - QSharedPointer(const QSharedPointer &other) noexcept : value(other.value.get()), d(other.d) + QSharedPointer(const QSharedPointer &other) noexcept : value(other.value), d(other.d) { if (d) ref(); } QSharedPointer &operator=(const QSharedPointer &other) noexcept { @@ -312,7 +312,7 @@ public: } Q_NODISCARD_CTOR QSharedPointer(QSharedPointer &&other) noexcept - : value(other.value.get()), d(other.d) + : value(other.value), d(other.d) { other.d = nullptr; other.value = nullptr; @@ -322,7 +322,7 @@ public: template <class X, IfCompatible<X> = true> Q_NODISCARD_CTOR QSharedPointer(QSharedPointer<X> &&other) noexcept - : value(other.value.get()), d(other.d) + : value(other.value), d(other.d) { other.d = nullptr; other.value = nullptr; @@ -338,7 +338,7 @@ public: template <class X, IfCompatible<X> = true> Q_NODISCARD_CTOR - QSharedPointer(const QSharedPointer<X> &other) noexcept : value(other.value.get()), d(other.d) + QSharedPointer(const QSharedPointer<X> &other) noexcept : value(other.value), d(other.d) { if (d) ref(); } template <class X, IfCompatible<X> = true> @@ -414,16 +414,38 @@ public: // now initialize the data new (ptr) T(std::forward<Args>(arguments)...); - result.value.reset(ptr); + result.value = ptr; result.d->destroyer = destroy; - result.d->setQObjectShared(result.value.get(), true); + result.d->setQObjectShared(result.value, true); # ifdef QT_SHAREDPOINTER_TRACK_POINTERS - internalSafetyCheckAdd(result.d, result.value.get()); + internalSafetyCheckAdd(result.d, result.value); # endif result.enableSharedFromThis(result.data()); return result; } +#define DECLARE_COMPARE_SET(T1, A1, T2, A2) \ + friend bool operator==(T1, T2) noexcept \ + { return A1 == A2; } \ + friend bool operator!=(T1, T2) noexcept \ + { return A1 != A2; } + +#define DECLARE_TEMPLATE_COMPARE_SET(T1, A1, T2, A2) \ + template <typename X> \ + friend bool operator==(T1, T2) noexcept \ + { return A1 == A2; } \ + template <typename X> \ + friend bool operator!=(T1, T2) noexcept \ + { return A1 != A2; } + + DECLARE_TEMPLATE_COMPARE_SET(const QSharedPointer &p1, p1.data(), const QSharedPointer<X> &p2, p2.data()) + DECLARE_TEMPLATE_COMPARE_SET(const QSharedPointer &p1, p1.data(), X *ptr, ptr) + DECLARE_TEMPLATE_COMPARE_SET(X *ptr, ptr, const QSharedPointer &p2, p2.data()) + DECLARE_COMPARE_SET(const QSharedPointer &p1, p1.data(), std::nullptr_t, nullptr) + DECLARE_COMPARE_SET(std::nullptr_t, nullptr, const QSharedPointer &p2, p2.data()) +#undef DECLARE_TEMPLATE_COMPARE_SET +#undef DECLARE_COMPARE_SET + template <typename X> bool owner_before(const QSharedPointer<X> &other) const noexcept { return std::less<>()(d, other.d); } @@ -442,33 +464,6 @@ public: { return std::hash<Data *>()(d); } private: - template <typename X> - friend bool comparesEqual(const QSharedPointer &lhs, const QSharedPointer<X> &rhs) noexcept - { return lhs.data() == rhs.data(); } - template <typename X> - friend Qt::strong_ordering - compareThreeWay(const QSharedPointer &lhs, const QSharedPointer<X> &rhs) - { - return Qt::compareThreeWay(lhs.value, rhs.data()); - } - QT_DECLARE_ORDERING_OPERATORS_HELPER(STRONG, QSharedPointer<T>, QSharedPointer<X>, - /* non-constexpr */, template <typename X>) - - template <typename X> - friend bool comparesEqual(const QSharedPointer &lhs, X *rhs) noexcept - { return lhs.data() == rhs; } - template <typename X> - friend Qt::strong_ordering compareThreeWay(const QSharedPointer &lhs, X *rhs) - { return Qt::compareThreeWay(lhs.value, rhs); } - Q_DECLARE_STRONGLY_ORDERED(QSharedPointer, X*, template <typename X>) - - friend bool comparesEqual(const QSharedPointer &lhs, std::nullptr_t) noexcept - { return lhs.data() == nullptr; } - friend Qt::strong_ordering - compareThreeWay(const QSharedPointer &lhs, std::nullptr_t) noexcept - { return Qt::compareThreeWay(lhs.value, nullptr); } - Q_DECLARE_STRONGLY_ORDERED(QSharedPointer, std::nullptr_t) - Q_NODISCARD_CTOR explicit QSharedPointer(Qt::Initialization) {} @@ -540,7 +535,7 @@ private: } qt_ptr_swap(d, o); - this->value.reset(actual); + qt_ptr_swap(this->value, actual); if (!d || d->strongref.loadRelaxed() == 0) this->value = nullptr; @@ -548,7 +543,7 @@ private: deref(o); } - Qt::totally_ordered_wrapper<Type *> value; + Type *value; Data *d; }; @@ -637,7 +632,7 @@ public: { if (d) d->weakref.ref();} inline QWeakPointer &operator=(const QSharedPointer<T> &o) { - internalSet(o.d, o.value.get()); + internalSet(o.d, o.value); return *this; } @@ -815,6 +810,28 @@ Q_INLINE_TEMPLATE typename QSharedPointer<X>::difference_type operator-(T *ptr1, } // +// operator< +// +template <class T, class X> +Q_INLINE_TEMPLATE bool operator<(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2) +{ + using CT = typename std::common_type<T *, X *>::type; + return std::less<CT>()(ptr1.data(), ptr2.data()); +} +template <class T, class X> +Q_INLINE_TEMPLATE bool operator<(const QSharedPointer<T> &ptr1, X *ptr2) +{ + using CT = typename std::common_type<T *, X *>::type; + return std::less<CT>()(ptr1.data(), ptr2); +} +template <class T, class X> +Q_INLINE_TEMPLATE bool operator<(T *ptr1, const QSharedPointer<X> &ptr2) +{ + using CT = typename std::common_type<T *, X *>::type; + return std::less<CT>()(ptr1, ptr2.data()); +} + +// // qHash // template <class T> |
