diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2020-02-26 16:48:13 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2020-03-17 17:55:27 +0100 |
commit | 733ae8a04b4785961b85befc1c4ea6ecc7936a75 (patch) | |
tree | 8c244e8b4e2ad63c643584f64703912b6e456819 /tests/auto/corelib/tools/qtaggedpointer | |
parent | 749e37efc5dd9eaa693c4f86253c48c37737aa09 (diff) |
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 <ulf.hermann@qt.io>
Diffstat (limited to 'tests/auto/corelib/tools/qtaggedpointer')
-rw-r--r-- | tests/auto/corelib/tools/qtaggedpointer/tst_qtaggedpointer.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qtaggedpointer/tst_qtaggedpointer.cpp b/tests/auto/corelib/tools/qtaggedpointer/tst_qtaggedpointer.cpp index 36308d61bc..d98a53098f 100644 --- a/tests/auto/corelib/tools/qtaggedpointer/tst_qtaggedpointer.cpp +++ b/tests/auto/corelib/tools/qtaggedpointer/tst_qtaggedpointer.cpp @@ -43,6 +43,7 @@ private Q_SLOTS: void tag(); void objectMember(); void customTagType(); + void taggedLinkedList(); }; void tst_QTaggedPointer::construction() @@ -396,5 +397,32 @@ void tst_QTaggedPointer::customTagType() QCOMPARE(p.tag(), Bar::FirstTag | Bar::SecondTag); } +// Compile-only test to ensure it's possible to use tagged pointers +// with incomplete types. +struct LinkedListItem +{ + enum Tag { + NoTag = 0, + FirstTag = 1 + }; + Q_DECLARE_FLAGS(Tags, Tag) + + QTaggedPointer<LinkedListItem, Tag> next; + + ~LinkedListItem() + { + delete next.pointer(); + } +}; + +Q_DECLARE_OPERATORS_FOR_FLAGS(LinkedListItem::Tags) + +void tst_QTaggedPointer::taggedLinkedList() +{ + QScopedPointer<LinkedListItem> lli(new LinkedListItem); + lli->next = QTaggedPointer<LinkedListItem, LinkedListItem::Tag>(new LinkedListItem); + lli->next.setTag(LinkedListItem::FirstTag); +} + QTEST_MAIN(tst_QTaggedPointer) #include "tst_qtaggedpointer.moc" |