1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QTAGGEDPOINTER_H
41#define QTAGGEDPOINTER_H
42
43#include <QtCore/qglobal.h>
44#include <QtCore/qalgorithms.h>
45#include <QtCore/qmath.h>
46#include <QtCore/qtypeinfo.h>
47
48QT_BEGIN_NAMESPACE
49
50namespace QtPrivate {
51 constexpr quint8 nextByteSize(quint8 bits) { return (bits + 7) / 8; }
52
53 template <typename T>
54 struct TagInfo
55 {
56 static constexpr size_t alignment = alignof(T);
57 static_assert((alignment & (alignment - 1)) == 0,
58 "Alignment of template parameter must be power of two");
59
60 static constexpr quint8 tagBits = QtPrivate::qConstexprCountTrailingZeroBits(alignment);
61 static_assert(tagBits > 0,
62 "Alignment of template parameter does not allow any tags");
63
64 static constexpr size_t tagSize = QtPrivate::qConstexprNextPowerOfTwo(nextByteSize(tagBits));
65 static_assert(tagSize < sizeof(quintptr),
66 "Alignment of template parameter allows tags masking away pointer");
67
68 using TagType = typename QIntegerForSize<tagSize>::Unsigned;
69 };
70}
71
72template <typename T, typename Tag = typename QtPrivate::TagInfo<T>::TagType>
73class QTaggedPointer
74{
75public:
76 using Type = T;
77 using TagType = Tag;
78
79 static constexpr quintptr tagMask() { return QtPrivate::TagInfo<T>::alignment - 1; }
80 static constexpr quintptr pointerMask() { return ~tagMask(); }
81
82 constexpr QTaggedPointer() noexcept : d(0) {}
83 constexpr QTaggedPointer(std::nullptr_t) noexcept : QTaggedPointer() {}
84
85 explicit QTaggedPointer(T *pointer, Tag tag = Tag()) noexcept
86 : d(quintptr(pointer))
87 {
88 static_assert(sizeof(Type*) == sizeof(QTaggedPointer));
89
90 Q_ASSERT_X((quintptr(pointer) & tagMask()) == 0,
91 "QTaggedPointer<T, Tag>", "Pointer is not aligned");
92
93 setTag(tag);
94 }
95
96 Type &operator*() const noexcept
97 {
98 Q_ASSERT(data());
99 return *data();
100 }
101
102 Type *operator->() const noexcept
103 {
104 return data();
105 }
106
107 explicit operator bool() const noexcept
108 {
109 return !isNull();
110 }
111
112 QTaggedPointer &operator=(T *other) noexcept
113 {
114 d = reinterpret_cast<quintptr>(other) | (d & tagMask());
115 return *this;
116 }
117
118 static constexpr Tag maximumTag() noexcept
119 {
120 return TagType(typename QtPrivate::TagInfo<T>::TagType(tagMask()));
121 }
122
123 void setTag(Tag tag)
124 {
125 Q_ASSERT_X((static_cast<typename QtPrivate::TagInfo<T>::TagType>(tag) & pointerMask()) == 0,
126 "QTaggedPointer<T, Tag>::setTag", "Tag is larger than allowed by number of available tag bits");
127
128 d = (d & pointerMask()) | (static_cast<typename QtPrivate::TagInfo<T>::TagType>(tag) & tagMask());
129 }
130
131 Tag tag() const noexcept
132 {
133 return TagType(typename QtPrivate::TagInfo<T>::TagType(d & tagMask()));
134 }
135
136 T* data() const noexcept
137 {
138 return reinterpret_cast<T*>(d & pointerMask());
139 }
140
141 bool isNull() const noexcept
142 {
143 return !data();
144 }
145
146 void swap(QTaggedPointer &other) noexcept
147 {
148 qSwap(d, other.d);
149 }
150
151 friend inline bool operator==(QTaggedPointer lhs, QTaggedPointer rhs) noexcept
152 {
153 return lhs.data() == rhs.data();
154 }
155
156 friend inline bool operator!=(QTaggedPointer lhs, QTaggedPointer rhs) noexcept
157 {
158 return lhs.data() != rhs.data();
159 }
160
161 friend inline bool operator==(QTaggedPointer lhs, std::nullptr_t) noexcept
162 {
163 return lhs.isNull();
164 }
165
166 friend inline bool operator==(std::nullptr_t, QTaggedPointer rhs) noexcept
167 {
168 return rhs.isNull();
169 }
170
171 friend inline bool operator!=(QTaggedPointer lhs, std::nullptr_t) noexcept
172 {
173 return !lhs.isNull();
174 }
175
176 friend inline bool operator!=(std::nullptr_t, QTaggedPointer rhs) noexcept
177 {
178 return !rhs.isNull();
179 }
180
181 friend inline bool operator!(QTaggedPointer ptr) noexcept
182 {
183 return !ptr.data();
184 }
185
186 friend inline void swap(QTaggedPointer &p1, QTaggedPointer &p2) noexcept
187 {
188 p1.swap(p2);
189 }
190
191protected:
192 quintptr d;
193};
194
195template <typename T, typename Tag>
196constexpr inline std::size_t qHash(QTaggedPointer<T, Tag> p, std::size_t seed = 0) noexcept
197{ return qHash(p.data(), seed); }
198
199template <typename T, typename Tag>
200class QTypeInfo<QTaggedPointer<T, Tag>>
201 : public QTypeInfoMerger<QTaggedPointer<T, Tag>, quintptr> {};
202
203QT_END_NAMESPACE
204
205#endif // QTAGGEDPOINTER_H
206