From 733ae8a04b4785961b85befc1c4ea6ecc7936a75 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Wed, 26 Feb 2020 16:48:13 +0100 Subject: Make it possible to use QTaggedPointer within classes A common pattern in declarative is to use the unused bits in linked list next pointers for additional information storage. The "next" pointer is typically then a tagged pointer of the containing class, which is not fully defined yet. Therefore alignof() can't be used at tagged pointer instantiation time. This patch delays the calls to alignment, etc. until the corresponding functions are used, as in principle the tagged pointer is just a quintptr and no additional information should be needed until operating on it. Change-Id: I87a3578ee921d471e1b60ed5903b549ef0610b97 Reviewed-by: Ulf Hermann --- src/corelib/tools/qtaggedpointer.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qtaggedpointer.h b/src/corelib/tools/qtaggedpointer.h index 7d631a5e0c..6fa1e67acb 100644 --- a/src/corelib/tools/qtaggedpointer.h +++ b/src/corelib/tools/qtaggedpointer.h @@ -73,10 +73,8 @@ namespace QtPrivate { template ::TagType> class QTaggedPointer { - static constexpr quintptr tagMask = QtPrivate::TagInfo::alignment - 1; - static constexpr quintptr pointerMask = ~tagMask; - - using TagInternalType = typename QtPrivate::TagInfo::TagType; + static constexpr quintptr tagMask() { return QtPrivate::TagInfo::alignment - 1; } + static constexpr quintptr pointerMask() { return ~tagMask(); } public: using Type = T; @@ -87,7 +85,7 @@ public: { Q_STATIC_ASSERT(sizeof(Type*) == sizeof(QTaggedPointer)); - Q_ASSERT_X((quintptr(pointer) & tagMask) == 0, + Q_ASSERT_X((quintptr(pointer) & tagMask()) == 0, "QTaggedPointer", "Pointer is not aligned"); setTag(tag); @@ -111,37 +109,37 @@ public: QTaggedPointer &operator=(T *other) noexcept { - d = reinterpret_cast(other) | (d & tagMask); + d = reinterpret_cast(other) | (d & tagMask()); return *this; } QTaggedPointer &operator=(std::nullptr_t) noexcept { - d &= tagMask; + d &= tagMask(); return *this; } static constexpr TagType maximumTag() noexcept { - return TagType(TagInternalType(tagMask)); + return TagType(typename QtPrivate::TagInfo::TagType(tagMask())); } void setTag(TagType tag) { - Q_ASSERT_X((static_cast(tag) & pointerMask) == 0, + Q_ASSERT_X((static_cast::TagType>(tag) & pointerMask()) == 0, "QTaggedPointer::setTag", "Tag is larger than allowed by number of available tag bits"); - d = (d & pointerMask) | (static_cast(tag) & tagMask); + d = (d & pointerMask()) | (static_cast::TagType>(tag) & tagMask()); } TagType tag() const noexcept { - return TagType(TagInternalType(d & tagMask)); + return TagType(typename QtPrivate::TagInfo::TagType(d & tagMask())); } Type* pointer() const noexcept { - return reinterpret_cast(d & pointerMask); + return reinterpret_cast(d & pointerMask()); } bool isNull() const noexcept -- cgit v1.2.3