summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qtaggedpointer.qdoc
blob: 36e20ecff0383b95290bd0ba43dff011382f0f1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \class QTaggedPointer
    \internal
    \inmodule QtCore
    \brief The QTaggedPointer class provides a low-level wrapper around raw
    pointers that makes it possible to store additional information in otherwise unused bits.

    \ingroup tools
    \reentrant


    Data structures in C++ tend to have a natural alignment in memory, based on
    the alignment requirements of the members fields. For example a structure
    containing an integer tends to be aligned to a 4-byte boundary in memory. That
    means a pointer holding the address of an instance will always have the lower
    two bits set to zero. QTaggedPointer makes it possible to store information
    in these bits, such as a user provided enum. This is called a tag. The API allows
    reading and writing the tag. When asked for the raw pointer, it will always return
    a valid address with the bits used for the tag set to zero.

    This pattern may be useful when creating low-level data structures that
    need to be as dense as possible.

    The first template parameter is the type the tagged pointer should point
    to. The second template parameter is the type of the tag. If not specified it will
    default to an unsigned integral. A more powerful pattern though is to define a
    flag enum and use that as a tag type:

    \code
    struct LinkedListItem
    {
        enum MyFlag {
            NoOption           = 0x0,
            SomeOption         = 0x1,
            ThirdWay           = 0x2
        };
        Q_DECLARE_FLAGS(MyFlags, MyFlag)

        QTaggedPointer<LinkedListItem, MyFlag> next;

        ...
    };

    Q_DECLARE_OPERATORS_FOR_FLAGS(LinkedListItem::MyFlags)

    LinkedListItem &listItem = ...
    listItem.next = new LinkedListItem;
    listItem.next.setTag(LinkedListItem::SomeOption | LinkedListItem::ThirdWay);
    \endcode

    \note QTaggedPointer does not provide ownership. You are responsible for deleting
    the data the pointer points to.
*/

/*!
    \typedef QTaggedPointer::Type

    Typedef for T.
*/

/*!
    \typedef QTaggedPointer::TagType

    Typedef for Tag.
*/


/*!
    \fn template <typename T, typename Tag> QTaggedPointer<T, Tag>::QTaggedPointer()

    Creates a tagged pointer that contains nullptr and stores no tag.
*/

/*!
    \fn template <typename T, typename Tag> QTaggedPointer<T, Tag>::QTaggedPointer(std::nullptr_t)

    Creates a tagged pointer that contains nullptr and stores no tag.
*/

/*!
    \fn template <typename T, typename Tag> explicit QTaggedPointer<T, Tag>::QTaggedPointer(T *pointer = nullptr, Tag tag = Tag()) noexcept

    Creates a tagged pointer that points to \a pointer and stores the specified \a tag.
*/

/*!
    \fn template <typename T, typename Tag> T &QTaggedPointer<T, Tag>::operator*() const noexcept

    Provides access to the pointer's members.
*/

/*!
    \fn template <typename T, typename Tag> T *QTaggedPointer<T, Tag>::operator->() const noexcept

    Provides access to the pointer's members.
*/

/*!
    \fn template <typename T, typename Tag> bool QTaggedPointer<T, Tag>::operator!(QTaggedPointer<T, Tag> pointer) noexcept

    Returns \c true if \a pointer is \nullptr.
*/

/*!
    \fn template <typename T, typename Tag> explicit QTaggedPointer<T, Tag>::operator bool() const noexcept

    Returns \c true if the pointer is \e not null.
*/

/*!
    \fn template <typename T, typename Tag> QTaggedPointer<T, Tag> &QTaggedPointer<T, Tag>::operator=(T *other) noexcept

    Sets the pointer of this to \a other. The tag remains unchanged.
*/

/*!
    \fn template <typename T, typename Tag> void QTaggedPointer<T, Tag>::setTag(Tag tag)

    Sets the tag value to the specified \a tag. The pointer remains unchanged.
*/

/*!
    \fn template <typename T, typename Tag> Tag QTaggedPointer<T, Tag>::tag() const noexcept

    Returns the tag stored in the tagged pointer.
*/

/*!
    \fn template <typename T, typename Tag> T *QTaggedPointer<T, Tag>::data() const noexcept

    Returns the pointer stored in the tagged pointer.
*/

/*!
    \fn template <typename T, typename Tag> bool QTaggedPointer<T, Tag>::isNull() const noexcept

    Returns \c true if the pointer is \nullptr; otherwise returns \c false.
*/

/*!
    \fn template <typename T, typename Tag> void QTaggedPointer<T, Tag>::swap(QTaggedPointer<T, Tag> &other) noexcept

    Swaps this instance's pointer and tag with the pointer and tag in \a other.
*/

/*!
    \fn template <typename T, typename Tag> bool QTaggedPointer<T, Tag>::operator==(QTaggedPointer<T, Tag> lhs, QTaggedPointer<T, Tag> rhs) noexcept

    Returns \c true if \a lhs is equal to \a rhs; otherwise returns \c false.

    Two tagged pointers are considered equal if they point to the same object. Their tags are not compared.
*/

/*!
    \fn template <typename T, typename Tag> bool QTaggedPointer<T, Tag>::operator!=(QTaggedPointer<T, Tag> lhs, QTaggedPointer<T, Tag> rhs) noexcept

    Returns \c true if \a lhs is not equal to \a rhs; otherwise returns \c false.

    Two tagged pointers are considered equal if they point to the same object. Their tags are not compared.
*/

/*!
    \fn template <typename T, typename Tag> bool QTaggedPointer<T, Tag>::operator==(QTaggedPointer<T, Tag> lhs, std::nullptr_t) noexcept

    Returns \c true if \a lhs refers to \c nullptr.
*/

/*!
    \fn template <typename T, typename Tag> bool QTaggedPointer<T, Tag>::operator==(std::nullptr_t, QTaggedPointer<T, Tag> rhs) noexcept

    Returns \c true if \a rhs refers to \c nullptr.
*/

/*!
    \fn template <typename T, typename Tag> bool QTaggedPointer<T, Tag>::operator!=(QTaggedPointer<T, Tag> lhs, std::nullptr_t) noexcept

    Returns \c true if \a lhs refers to a valid (i.e. non-null) pointer.
*/

/*!
    \fn template <typename T, typename Tag> bool QTaggedPointer<T, Tag>::operator!=(std::nullptr_t, QTaggedPointer<T, Tag> rhs) noexcept

    Returns \c true if \a rhs refers to a valid (i.e. non-null) pointer.
*/

/*!
    \fn template <typename T, typename Tag> qHash(QTaggedPointer<T, Tag> key, std::size_t seed)
    \relates QTaggedPointer

    Returns the hash value for the \a key, using \a seed to seed the calculation.
*/