Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#ifndef QBIPOINTER_P_H
5#define QBIPOINTER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qglobal_p.h>
19
20#include <QtCore/qhashfunctions.h>
21
23
24namespace QtPrivate {
25template <typename T> struct QFlagPointerAlignment
26{
27 enum : size_t { Value = Q_ALIGNOF(T) };
28};
29template <> struct QFlagPointerAlignment<void>
30{
31 enum : size_t { Value = ~size_t(0) };
32};
33}
34
45template<typename T, typename T2>
47public:
48 Q_NODISCARD_CTOR constexpr QBiPointer() noexcept = default;
49 ~QBiPointer() noexcept = default;
50 Q_NODISCARD_CTOR QBiPointer(const QBiPointer &o) noexcept = default;
51 Q_NODISCARD_CTOR QBiPointer(QBiPointer &&o) noexcept = default;
52 QBiPointer<T, T2> &operator=(const QBiPointer<T, T2> &o) noexcept = default;
53 QBiPointer<T, T2> &operator=(QBiPointer<T, T2> &&o) noexcept = default;
54
55 void swap(QBiPointer &other) noexcept { std::swap(ptr_value, other.ptr_value); }
56
59
60 inline bool isNull() const;
61 inline bool isT1() const;
62 inline bool isT2() const;
63
64 inline bool flag() const;
65 inline void setFlag();
66 inline void clearFlag();
67 inline void setFlagValue(bool);
68
69 inline QBiPointer<T, T2> &operator=(T *);
70 inline QBiPointer<T, T2> &operator=(T2 *);
71
72 friend inline bool operator==(QBiPointer<T, T2> ptr1, QBiPointer<T, T2> ptr2)
73 {
74 if (ptr1.isNull() && ptr2.isNull())
75 return true;
76 if (ptr1.isT1() && ptr2.isT1())
77 return ptr1.asT1() == ptr2.asT1();
78 if (ptr1.isT2() && ptr2.isT2())
79 return ptr1.asT2() == ptr2.asT2();
80 return false;
81 }
82 friend inline bool operator!=(QBiPointer<T, T2> ptr1, QBiPointer<T, T2> ptr2)
83 {
84 return !(ptr1 == ptr2);
85 }
86
87 friend void swap(QBiPointer &lhs, QBiPointer &rhs) noexcept { lhs.swap(rhs); }
88
89 inline T *asT1() const;
90 inline T2 *asT2() const;
91
92 friend size_t qHash(const QBiPointer<T, T2> &ptr, size_t seed = 0)
93 {
94 return qHash(ptr.isNull() ? quintptr(0) : ptr.ptr_value, seed);
95 }
96
97private:
98 quintptr ptr_value = 0;
99
100 static const quintptr FlagBit = 0x1;
101 static const quintptr Flag2Bit = 0x2;
102 static const quintptr FlagsMask = FlagBit | Flag2Bit;
103};
104
105template <typename...Ts> // can't use commas in macros
107
108template<typename T, typename T2>
110: ptr_value(quintptr(v))
111{
113 "Type T does not have sufficient alignment");
114 Q_ASSERT((quintptr(v) & FlagsMask) == 0);
115}
116
117template<typename T, typename T2>
119: ptr_value(quintptr(v) | Flag2Bit)
120{
122 "Type T2 does not have sufficient alignment");
123 Q_ASSERT((quintptr(v) & FlagsMask) == 0);
124}
125
126template<typename T, typename T2>
128{
129 return 0 == (ptr_value & (~FlagsMask));
130}
131
132template<typename T, typename T2>
134{
135 return !(ptr_value & Flag2Bit);
136}
137
138template<typename T, typename T2>
140{
141 return ptr_value & Flag2Bit;
142}
143
144template<typename T, typename T2>
146{
147 return ptr_value & FlagBit;
148}
149
150template<typename T, typename T2>
152{
153 ptr_value |= FlagBit;
154}
155
156template<typename T, typename T2>
158{
159 ptr_value &= ~FlagBit;
160}
161
162template<typename T, typename T2>
164{
165 if (v) setFlag();
166 else clearFlag();
167}
168
169template<typename T, typename T2>
170QBiPointer<T, T2> &QBiPointer<T, T2>::operator=(T *o)
171{
172 Q_ASSERT((quintptr(o) & FlagsMask) == 0);
173
174 ptr_value = quintptr(o) | (ptr_value & FlagBit);
175 return *this;
176}
177
178template<typename T, typename T2>
179QBiPointer<T, T2> &QBiPointer<T, T2>::operator=(T2 *o)
180{
181 Q_ASSERT((quintptr(o) & FlagsMask) == 0);
182
183 ptr_value = quintptr(o) | (ptr_value & FlagBit) | Flag2Bit;
184 return *this;
185}
186
187template<typename T, typename T2>
189{
190 Q_ASSERT(isT1());
191 return (T *)(ptr_value & ~FlagsMask);
192}
193
194template<typename T, typename T2>
196{
197 Q_ASSERT(isT2());
198 return (T2 *)(ptr_value & ~FlagsMask);
199}
200
202
203#endif // QBIPOINTER_P_H
void setFlagValue(bool)
friend bool operator!=(QBiPointer< T, T2 > ptr1, QBiPointer< T, T2 > ptr2)
QBiPointer< T, T2 > & operator=(T *)
void clearFlag()
void setFlag()
T * asT1() const
friend bool operator==(QBiPointer< T, T2 > ptr1, QBiPointer< T, T2 > ptr2)
QBiPointer< T, T2 > & operator=(T2 *)
bool isNull() const
bool flag() const
Q_NODISCARD_CTOR QBiPointer(T2 *)
bool isT1() const
friend void swap(QBiPointer &lhs, QBiPointer &rhs) noexcept
Q_NODISCARD_CTOR constexpr QBiPointer() noexcept=default
friend size_t qHash(const QBiPointer< T, T2 > &ptr, size_t seed=0)
Q_NODISCARD_CTOR QBiPointer(T *)
T2 * asT2() const
QBiPointer< T, T2 > & operator=(const QBiPointer< T, T2 > &o) noexcept=default
bool isT2() const
Combined button and popup list for selecting options.
\macro QT_NO_KEYWORDS >
#define Q_STATIC_ASSERT_X(Condition, Message)
Definition qassert.h:111
#define Q_NODISCARD_CTOR
#define Q_ALIGNOF(x)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
static ControlElement< T > * ptr(QWidget *widget)
GLsizei const GLfloat * v
[13]
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:157
#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS)
Definition qtypeinfo.h:163
size_t quintptr
Definition qtypes.h:167
QSharedPointer< T > other(t)
[5]