Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qbipointer_p.h
Go to the documentation of this file.
1
// Copyright (C) 2021 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant
4
5
#
ifndef
QBIPOINTER_P_H
6
#
define
QBIPOINTER_P_H
7
8
//
9
// W A R N I N G
10
// -------------
11
//
12
// This file is not part of the Qt API. It exists purely as an
13
// implementation detail. This header file may change from version to
14
// version without notice, or even be removed.
15
//
16
// We mean it.
17
//
18
19
#
include
<
QtCore
/
private
/
qglobal_p
.
h
>
20
21
#
include
<
QtCore
/
qhashfunctions
.
h
>
22
23
QT_BEGIN_NAMESPACE
24
25
namespace
QtPrivate
{
26
template
<
typename
T>
struct
QFlagPointerAlignment
27
{
28
enum
: size_t {
Value
= Q_ALIGNOF(T) };
29
};
30
template
<>
struct
QFlagPointerAlignment
<
void
>
31
{
32
enum
: size_t {
Value
= ~size_t(0) };
33
};
34
}
35
36
/*!
37
\internal
38
\class template<typename T1, typename T2> QBiPointer<T1, T2>
39
40
\short QBiPointer can be thought of as a space-optimized std::variant<T1*, T2*>
41
with a nicer API to check the active pointer. Its other main feature is that
42
it only requires sizeof(void *) space.
43
44
\note It can also store one additional flag for a user defined purpose.
45
*/
46
template
<
typename
T,
typename
T2>
47
class
QBiPointer
{
48
public
:
49
Q_NODISCARD_CTOR
constexpr
QBiPointer
()
noexcept
=
default
;
50
~
QBiPointer
()
noexcept
=
default
;
51
Q_NODISCARD_CTOR
QBiPointer
(
const
QBiPointer
&
o
)
noexcept
=
default
;
52
Q_NODISCARD_CTOR
QBiPointer
(
QBiPointer
&&
o
)
noexcept
=
default
;
53
QBiPointer
<T, T2> &
operator
=(
const
QBiPointer
<T, T2> &o)
noexcept
=
default
;
54
QBiPointer
<T, T2> &
operator
=(
QBiPointer
<T, T2> &&o)
noexcept
=
default
;
55
56
void
swap
(
QBiPointer
&other)
noexcept
{ std::swap(ptr_value, other.ptr_value); }
57
58
Q_NODISCARD_CTOR
inline
QBiPointer
(
T
*);
59
Q_NODISCARD_CTOR
inline
QBiPointer
(
T2
*);
60
61
inline
bool
isNull
()
const
;
62
inline
bool
isT1
()
const
;
63
inline
bool
isT2
()
const
;
64
65
inline
bool
flag
()
const
;
66
inline
void
setFlag
();
67
inline
void
clearFlag
();
68
inline
void
setFlagValue
(
bool
);
69
70
inline
QBiPointer
<T, T2> &
operator
=(T *);
71
inline
QBiPointer
<T, T2> &
operator
=(T2 *);
72
73
friend
inline
bool
operator
==(
QBiPointer
<T, T2> ptr1,
QBiPointer
<T, T2> ptr2)
74
{
75
if
(ptr1
.
isNull
(
)
&& ptr2
.
isNull
(
)
)
76
return
true
;
77
if
(ptr1
.
isT1
(
)
&& ptr2
.
isT1
(
)
)
78
return
ptr1
.
asT1
(
)
== ptr2
.
asT1
(
)
;
79
if
(ptr1
.
isT2
(
)
&& ptr2
.
isT2
(
)
)
80
return
ptr1
.
asT2
(
)
== ptr2
.
asT2
(
)
;
81
return
false
;
82
}
83
friend
inline
bool
operator
!=(
QBiPointer
<T, T2> ptr1,
QBiPointer
<T, T2> ptr2)
84
{
85
return
!(ptr1 == ptr2);
86
}
87
88
friend
void
swap
(
QBiPointer
&lhs,
QBiPointer
&rhs)
noexcept
{ lhs
.
swap
(
rhs
)
; }
89
90
inline
T *
asT1
()
const
;
91
inline
T2 *
asT2
()
const
;
92
93
friend
size_t
qHash
(
const
QBiPointer
<T, T2> &ptr, size_t seed = 0)
94
{
95
return
qHash(ptr
.
isNull
(
)
? quintptr(0) : ptr.ptr_value, seed);
96
}
97
98
private
:
99
quintptr ptr_value = 0;
100
101
static
const
quintptr FlagBit = 0x1;
102
static
const
quintptr Flag2Bit = 0x2;
103
static
const
quintptr FlagsMask = FlagBit | Flag2Bit;
104
};
105
106
template
<
typename
...Ts>
// can't use commas in macros
107
Q_DECLARE_TYPEINFO_BODY
(
QBiPointer
<Ts...>,
Q_PRIMITIVE_TYPE
);
108
109
template
<
typename
T,
typename
T2>
110
QBiPointer
<T, T2>::QBiPointer(T *v)
111
: ptr_value(quintptr(v))
112
{
113
Q_STATIC_ASSERT_X(
QtPrivate
::
QFlagPointerAlignment
<T>::Value >= 4,
114
"Type T does not have sufficient alignment"
);
115
Q_ASSERT((quintptr(v) & FlagsMask) == 0);
116
}
117
118
template
<
typename
T,
typename
T2>
119
QBiPointer
<T, T2>::QBiPointer(T2 *v)
120
: ptr_value(quintptr(v) | Flag2Bit)
121
{
122
Q_STATIC_ASSERT_X(
QtPrivate
::
QFlagPointerAlignment
<T2>::Value >= 4,
123
"Type T2 does not have sufficient alignment"
);
124
Q_ASSERT((quintptr(v) & FlagsMask) == 0);
125
}
126
127
template
<
typename
T,
typename
T2>
128
bool
QBiPointer
<T, T2>::
isNull
()
const
129
{
130
return
0 == (ptr_value & (~FlagsMask));
131
}
132
133
template
<
typename
T,
typename
T2>
134
bool
QBiPointer
<T, T2>::
isT1
()
const
135
{
136
return
!(ptr_value & Flag2Bit);
137
}
138
139
template
<
typename
T,
typename
T2>
140
bool
QBiPointer
<T, T2>::
isT2
()
const
141
{
142
return
ptr_value & Flag2Bit;
143
}
144
145
template
<
typename
T,
typename
T2>
146
bool
QBiPointer
<T, T2>::
flag
()
const
147
{
148
return
ptr_value & FlagBit;
149
}
150
151
template
<
typename
T,
typename
T2>
152
void
QBiPointer
<T, T2>::
setFlag
()
153
{
154
ptr_value |= FlagBit;
155
}
156
157
template
<
typename
T,
typename
T2>
158
void
QBiPointer
<T, T2>::
clearFlag
()
159
{
160
ptr_value &= ~FlagBit;
161
}
162
163
template
<
typename
T,
typename
T2>
164
void
QBiPointer
<T, T2>::
setFlagValue
(
bool
v)
165
{
166
if
(v)
setFlag
(
)
;
167
else
clearFlag
(
)
;
168
}
169
170
template
<
typename
T,
typename
T2>
171
QBiPointer
<T, T2> &
QBiPointer
<T, T2>::
operator
=(T *o)
172
{
173
Q_ASSERT((quintptr(o) & FlagsMask) == 0);
174
175
ptr_value = quintptr(o) | (ptr_value & FlagBit);
176
return
*
this
;
177
}
178
179
template
<
typename
T,
typename
T2>
180
QBiPointer
<T, T2> &
QBiPointer
<T, T2>::
operator
=(T2 *o)
181
{
182
Q_ASSERT((quintptr(o) & FlagsMask) == 0);
183
184
ptr_value = quintptr(o) | (ptr_value & FlagBit) | Flag2Bit;
185
return
*
this
;
186
}
187
188
template
<
typename
T,
typename
T2>
189
T *
QBiPointer
<T, T2>::
asT1
()
const
190
{
191
Q_ASSERT(
isT1
(
)
);
192
return
(T *)(ptr_value & ~FlagsMask);
193
}
194
195
template
<
typename
T,
typename
T2>
196
T2 *
QBiPointer
<T, T2>::
asT2
()
const
197
{
198
Q_ASSERT(
isT2
(
)
);
199
return
(T2 *)(ptr_value & ~FlagsMask);
200
}
201
202
QT_END_NAMESPACE
203
204
#
endif
// QBIPOINTER_P_H
QBiPointer
Definition
qbipointer_p.h:47
QBiPointer::setFlagValue
void setFlagValue(bool)
Definition
qbipointer_p.h:164
QBiPointer::operator!=
friend bool operator!=(QBiPointer< T, T2 > ptr1, QBiPointer< T, T2 > ptr2)
Definition
qbipointer_p.h:83
QBiPointer::~QBiPointer
~QBiPointer() noexcept=default
QBiPointer::operator=
QBiPointer< T, T2 > & operator=(T *)
Definition
qbipointer_p.h:171
QBiPointer::operator=
QBiPointer< T, T2 > & operator=(QBiPointer< T, T2 > &&o) noexcept=default
QBiPointer::clearFlag
void clearFlag()
Definition
qbipointer_p.h:158
QBiPointer::setFlag
void setFlag()
Definition
qbipointer_p.h:152
QBiPointer::asT1
T * asT1() const
Definition
qbipointer_p.h:189
QBiPointer::operator==
friend bool operator==(QBiPointer< T, T2 > ptr1, QBiPointer< T, T2 > ptr2)
Definition
qbipointer_p.h:73
QBiPointer::operator=
QBiPointer< T, T2 > & operator=(T2 *)
Definition
qbipointer_p.h:180
QBiPointer::swap
void swap(QBiPointer &other) noexcept
Definition
qbipointer_p.h:56
QBiPointer::isNull
bool isNull() const
Definition
qbipointer_p.h:128
QBiPointer::flag
bool flag() const
Definition
qbipointer_p.h:146
QBiPointer::isT1
bool isT1() const
Definition
qbipointer_p.h:134
QBiPointer::swap
friend void swap(QBiPointer &lhs, QBiPointer &rhs) noexcept
Definition
qbipointer_p.h:88
QBiPointer::qHash
friend size_t qHash(const QBiPointer< T, T2 > &ptr, size_t seed=0)
Definition
qbipointer_p.h:93
QBiPointer::asT2
T2 * asT2() const
Definition
qbipointer_p.h:196
QBiPointer::operator=
QBiPointer< T, T2 > & operator=(const QBiPointer< T, T2 > &o) noexcept=default
QBiPointer::isT2
bool isT2() const
Definition
qbipointer_p.h:140
QtPrivate
Definition
qalloc.h:29
Q_DECLARE_TYPEINFO_BODY
Q_DECLARE_TYPEINFO_BODY(QBiPointer< Ts... >, Q_PRIMITIVE_TYPE)
QtPrivate::QFlagPointerAlignment< void >
Definition
qbipointer_p.h:31
QtPrivate::QFlagPointerAlignment< void >::Value
@ Value
Definition
qbipointer_p.h:32
QtPrivate::QFlagPointerAlignment
Definition
qbipointer_p.h:27
QtPrivate::QFlagPointerAlignment::Value
@ Value
Definition
qbipointer_p.h:28
qtdeclarative
src
qml
qml
ftw
qbipointer_p.h
Generated on
for Qt by
1.18.0