From 5ffa5808f8b5cdc49242414006fd7b68cb2a1c12 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 14 May 2020 17:51:41 +0200 Subject: QTaggedPointer: some API cleanups - don't refer to the class with template arguments in the body of the class (incl. one where we got the template args wrong) - make namespace-level swap a non-member friend - make default ctor constexpr and non-explicit (default ctors should never be explicit) - make ctor from std::nullptr_t constexpr - remove op= from std::nullptr (will be handled by op=(T*) anyway) - pass QTaggedPointer by value (it's a Trivial Type, so can be passed in registers) - fix missing QTypeInfo for non-default Tag types - remove unused include limits.h - make qHash-able (why is the tag ignored?) Change-Id: Idee1d685ac365c988698a8637fd5df3accfc1396 Reviewed-by: Thiago Macieira --- src/corelib/tools/qtaggedpointer.h | 50 +++++++++++++++++------------------ src/corelib/tools/qtaggedpointer.qdoc | 30 ++++++++++++++++----- 2 files changed, 49 insertions(+), 31 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/tools/qtaggedpointer.h b/src/corelib/tools/qtaggedpointer.h index 0c48fa6e4f..67209ffc08 100644 --- a/src/corelib/tools/qtaggedpointer.h +++ b/src/corelib/tools/qtaggedpointer.h @@ -43,8 +43,7 @@ #include #include #include - -#include +#include QT_BEGIN_NAMESPACE @@ -80,10 +79,13 @@ public: static constexpr quintptr tagMask() { return QtPrivate::TagInfo::alignment - 1; } static constexpr quintptr pointerMask() { return ~tagMask(); } - explicit QTaggedPointer(T *pointer = nullptr, Tag tag = Tag()) noexcept + constexpr QTaggedPointer() noexcept : d(0) {} + constexpr QTaggedPointer(std::nullptr_t) noexcept : QTaggedPointer() {} + + explicit QTaggedPointer(T *pointer, Tag tag = Tag()) noexcept : d(quintptr(pointer)) { - Q_STATIC_ASSERT(sizeof(Type*) == sizeof(QTaggedPointer)); + Q_STATIC_ASSERT(sizeof(Type*) == sizeof(QTaggedPointer)); Q_ASSERT_X((quintptr(pointer) & tagMask()) == 0, "QTaggedPointer", "Pointer is not aligned"); @@ -107,18 +109,12 @@ public: return !isNull(); } - QTaggedPointer &operator=(T *other) noexcept + QTaggedPointer &operator=(T *other) noexcept { d = reinterpret_cast(other) | (d & tagMask()); return *this; } - QTaggedPointer &operator=(std::nullptr_t) noexcept - { - d &= tagMask(); - return *this; - } - static constexpr Tag maximumTag() noexcept { return TagType(typename QtPrivate::TagInfo::TagType(tagMask())); @@ -147,58 +143,62 @@ public: return !data(); } - void swap(QTaggedPointer &other) noexcept + void swap(QTaggedPointer &other) noexcept { qSwap(d, other.d); } - friend inline bool operator==(const QTaggedPointer &lhs, const QTaggedPointer &rhs) noexcept + friend inline bool operator==(QTaggedPointer lhs, QTaggedPointer rhs) noexcept { return lhs.data() == rhs.data(); } - friend inline bool operator!=(const QTaggedPointer &lhs, const QTaggedPointer &rhs) noexcept + friend inline bool operator!=(QTaggedPointer lhs, QTaggedPointer rhs) noexcept { return lhs.data() != rhs.data(); } - friend inline bool operator==(const QTaggedPointer &lhs, std::nullptr_t) noexcept + friend inline bool operator==(QTaggedPointer lhs, std::nullptr_t) noexcept { return lhs.isNull(); } - friend inline bool operator==(std::nullptr_t, const QTaggedPointer &rhs) noexcept + friend inline bool operator==(std::nullptr_t, QTaggedPointer rhs) noexcept { return rhs.isNull(); } - friend inline bool operator!=(const QTaggedPointer &lhs, std::nullptr_t) noexcept + friend inline bool operator!=(QTaggedPointer lhs, std::nullptr_t) noexcept { return !lhs.isNull(); } - friend inline bool operator!=(std::nullptr_t, const QTaggedPointer &rhs) noexcept + friend inline bool operator!=(std::nullptr_t, QTaggedPointer rhs) noexcept { return !rhs.isNull(); } - friend inline bool operator!(const QTaggedPointer &ptr) noexcept + friend inline bool operator!(QTaggedPointer ptr) noexcept { return !ptr.data(); } + friend inline void swap(QTaggedPointer &p1, QTaggedPointer &p2) noexcept + { + p1.swap(p2); + } + protected: quintptr d; }; -template -Q_DECLARE_TYPEINFO_BODY(QTaggedPointer, Q_MOVABLE_TYPE); +template +constexpr inline std::size_t qHash(QTaggedPointer p, std::size_t seed = 0) noexcept +{ return qHash(p.data(), seed); } template -inline void swap(QTaggedPointer &p1, QTaggedPointer &p2) noexcept -{ - p1.swap(p2); -} +class QTypeInfo> + : public QTypeInfoMerger, quintptr> {}; QT_END_NAMESPACE diff --git a/src/corelib/tools/qtaggedpointer.qdoc b/src/corelib/tools/qtaggedpointer.qdoc index 5b335e8739..303b89232c 100644 --- a/src/corelib/tools/qtaggedpointer.qdoc +++ b/src/corelib/tools/qtaggedpointer.qdoc @@ -92,6 +92,18 @@ */ +/*! + \fn template QTaggedPointer::QTaggedPointer() + + Creates a tagged pointer that contains nullptr and stores no tag. +*/ + +/*! + \fn template QTaggedPointer::QTaggedPointer(std::nullptr_t) + + Creates a tagged pointer that contains nullptr and stores no tag. +*/ + /*! \fn template explicit QTaggedPointer::QTaggedPointer(T *pointer = nullptr, Tag tag = Tag()) noexcept @@ -159,7 +171,7 @@ */ /*! - \fn template inline bool operator==(const QTaggedPointer &lhs, const QTaggedPointer &rhs) noexcept + \fn template inline bool operator==(QTaggedPointer lhs, QTaggedPointer rhs) noexcept \relates QTaggedPointer Returns \c true if \a lhs is equal to \a rhs; otherwise returns \c false. @@ -168,7 +180,7 @@ */ /*! - \fn template inline bool operator!=(const QTaggedPointer &lhs, const QTaggedPointer &rhs) noexcept + \fn template inline bool operator!=(QTaggedPointer lhs, QTaggedPointer rhs) noexcept \relates QTaggedPointer Returns \c true if \a lhs is not equal to \a rhs; otherwise returns \c false. @@ -177,30 +189,36 @@ */ /*! - \fn template inline bool operator==(const QTaggedPointer &lhs, std::nullptr_t) noexcept + \fn template inline bool operator==(QTaggedPointer lhs, std::nullptr_t) noexcept \relates QTaggedPointer Returns \c true if \a lhs refers to \c nullptr. */ /*! - \fn template inline bool operator==(std::nullptr_t, const QTaggedPointer &rhs) noexcept + \fn template inline bool operator==(std::nullptr_t, QTaggedPointer rhs) noexcept \relates QTaggedPointer Returns \c true if \a rhs refers to \c nullptr. */ /*! - \fn template inline bool operator!=(const QTaggedPointer &lhs, std::nullptr_t) noexcept + \fn template inline bool operator!=(QTaggedPointerlhs, std::nullptr_t) noexcept \relates QTaggedPointer Returns \c true if \a lhs refers to a valid (i.e. non-null) pointer. */ /*! - \fn template inline bool operator!=(std::nullptr_t, const QTaggedPointer &rhs) noexcept + \fn template inline bool operator!=(std::nullptr_t, QTaggedPointer rhs) noexcept \relates QTaggedPointer Returns \c true if \a rhs refers to a valid (i.e. non-null) pointer. */ +/*! + \fn template qHash(QTaggedPointer key, std::size_t seed) + \relates QTaggedPointer + + Returns the hash value for the \a key, using \a seed to seed the calculation. +*/ -- cgit v1.2.3