Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
23QT_BEGIN_NAMESPACE
24
25namespace QtPrivate {
26template <typename T> struct QFlagPointerAlignment
27{
28 enum : size_t { Value = Q_ALIGNOF(T) };
29};
30template <> 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 */
46template<typename T, typename T2>
48public:
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
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
98private:
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
106template <typename...Ts> // can't use commas in macros
108
109template<typename T, typename T2>
110QBiPointer<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
118template<typename T, typename T2>
119QBiPointer<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
127template<typename T, typename T2>
128bool QBiPointer<T, T2>::isNull() const
129{
130 return 0 == (ptr_value & (~FlagsMask));
131}
132
133template<typename T, typename T2>
134bool QBiPointer<T, T2>::isT1() const
135{
136 return !(ptr_value & Flag2Bit);
137}
138
139template<typename T, typename T2>
140bool QBiPointer<T, T2>::isT2() const
141{
142 return ptr_value & Flag2Bit;
143}
144
145template<typename T, typename T2>
146bool QBiPointer<T, T2>::flag() const
147{
148 return ptr_value & FlagBit;
149}
150
151template<typename T, typename T2>
152void QBiPointer<T, T2>::setFlag()
153{
154 ptr_value |= FlagBit;
155}
156
157template<typename T, typename T2>
158void QBiPointer<T, T2>::clearFlag()
159{
160 ptr_value &= ~FlagBit;
161}
162
163template<typename T, typename T2>
164void QBiPointer<T, T2>::setFlagValue(bool v)
165{
166 if (v) setFlag();
167 else clearFlag();
168}
169
170template<typename T, typename T2>
171QBiPointer<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
179template<typename T, typename T2>
180QBiPointer<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
188template<typename T, typename T2>
189T *QBiPointer<T, T2>::asT1() const
190{
191 Q_ASSERT(isT1());
192 return (T *)(ptr_value & ~FlagsMask);
193}
194
195template<typename T, typename T2>
196T2 *QBiPointer<T, T2>::asT2() const
197{
198 Q_ASSERT(isT2());
199 return (T2 *)(ptr_value & ~FlagsMask);
200}
201
202QT_END_NAMESPACE
203
204#endif // QBIPOINTER_P_H
void setFlagValue(bool)
friend bool operator!=(QBiPointer< T, T2 > ptr1, QBiPointer< T, T2 > ptr2)
~QBiPointer() noexcept=default
QBiPointer< T, T2 > & operator=(T *)
QBiPointer< T, T2 > & operator=(QBiPointer< T, T2 > &&o) noexcept=default
void clearFlag()
void setFlag()
T * asT1() const
friend bool operator==(QBiPointer< T, T2 > ptr1, QBiPointer< T, T2 > ptr2)
QBiPointer< T, T2 > & operator=(T2 *)
void swap(QBiPointer &other) noexcept
bool isNull() const
bool flag() const
bool isT1() const
friend void swap(QBiPointer &lhs, QBiPointer &rhs) noexcept
friend size_t qHash(const QBiPointer< T, T2 > &ptr, size_t seed=0)
T2 * asT2() const
QBiPointer< T, T2 > & operator=(const QBiPointer< T, T2 > &o) noexcept=default
bool isT2() const
Q_DECLARE_TYPEINFO_BODY(QBiPointer< Ts... >, Q_PRIMITIVE_TYPE)