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
qendian_p.h
Go to the documentation of this file.
1// Copyright (C) 2020 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 reason:default
4
5#ifndef QENDIAN_P_H
6#define QENDIAN_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/qendian.h>
20#include <QtCore/private/qglobal_p.h>
21
22QT_BEGIN_NAMESPACE
23
24enum class QSpecialIntegerBitfieldInitializer {};
25constexpr inline QSpecialIntegerBitfieldInitializer QSpecialIntegerBitfieldZero{};
26
27template<class S>
29{
30public:
32
33 constexpr QSpecialIntegerStorage() = default;
34 constexpr QSpecialIntegerStorage(QSpecialIntegerBitfieldInitializer) : val(0) {}
35 constexpr QSpecialIntegerStorage(UnsignedStorageType initial) : val(initial) {}
36
38};
39
40template<class S, int pos, int width, class T = typename S::StorageType>
42
43template<class S, int pos, int width, class T = typename S::StorageType>
45{
47public:
49 using Type = T;
51
52 operator Type() const noexcept
53 {
54 if constexpr (std::is_signed_v<Type>) {
55 UnsignedType i = S::fromSpecial(m_storage->val);
56 i <<= (sizeof(Type) * 8) - width - pos;
57 Type t = Type(i);
58 t >>= (sizeof(Type) * 8) - width;
59 return t;
60 }
61 return (S::fromSpecial(m_storage->val) & mask()) >> pos;
62 }
63
64 bool operator!() const noexcept { return !(m_storage->val & S::toSpecial(mask())); }
65
66 static constexpr UnsignedType mask() noexcept
67 {
68 if constexpr (width == sizeof(UnsignedType) * 8) {
69 static_assert(pos == 0);
70 return ~UnsignedType(0);
71 } else {
72 return ((UnsignedType(1) << width) - 1) << pos;
73 }
74 }
75
76private:
77 template<class Storage, typename... Accessors>
79 friend class QSpecialIntegerAccessor<S, pos, width, T>;
80
81 explicit QSpecialIntegerConstAccessor(Storage *storage) : m_storage(storage) {}
82
83 friend bool operator==(const QSpecialIntegerConstAccessor<S, pos, width, T> &i,
84 const QSpecialIntegerConstAccessor<S, pos, width, T> &j) noexcept
85 {
86 return ((i.m_storage->val ^ j.m_storage->val) & S::toSpecial(mask())) == 0;
87 }
88
89 friend bool operator!=(const QSpecialIntegerConstAccessor<S, pos, width, T> &i,
90 const QSpecialIntegerConstAccessor<S, pos, width, T> &j) noexcept
91 {
92 return ((i.m_storage->val ^ j.m_storage->val) & S::toSpecial(mask())) != 0;
93 }
94
95 Storage *m_storage;
96};
97
98template<class S, int pos, int width, class T>
100{
102public:
105 using Type = T;
107
109 {
110 UnsignedType i = S::fromSpecial(m_storage->val);
111 i &= ~Const::mask();
112 i |= (UnsignedType(t) << pos) & Const::mask();
113 m_storage->val = S::toSpecial(i);
114 return *this;
115 }
116
118
119private:
120 template<class Storage, typename... Accessors>
122
123 explicit QSpecialIntegerAccessor(Storage *storage) : m_storage(storage) {}
124
125 Storage *m_storage;
126};
127
128template<class S, typename... Accessors>
130{
131public:
132 constexpr QSpecialIntegerBitfieldUnion() = default;
133 constexpr QSpecialIntegerBitfieldUnion(QSpecialIntegerBitfieldInitializer initial)
134 : storage(initial)
135 {}
136
138 typename QSpecialIntegerStorage<S>::UnsignedStorageType initial)
139 : storage(initial)
140 {}
141
142 template<typename A>
143 void set(typename A::Type value)
144 {
145 member<A>() = value;
146 }
147
148 template<typename A>
149 typename A::Type get() const
150 {
151 return member<A>();
152 }
153
155 {
156 return storage.val;
157 }
158
159private:
160 template<typename A>
161 static constexpr bool isAccessor = std::disjunction_v<std::is_same<A, Accessors>...>;
162
163 template<typename A>
164 A member()
165 {
166 static_assert(isAccessor<A>);
167 return A(&storage);
168 }
169
170 template<typename A>
171 typename A::Const member() const
172 {
173 static_assert(isAccessor<A>);
174 return typename A::Const(&storage);
175 }
176
177 QSpecialIntegerStorage<S> storage;
178};
179
180template<typename T, typename... Accessors>
181using QLEIntegerBitfieldUnion
182 = QSpecialIntegerBitfieldUnion<QLittleEndianStorageType<T>, Accessors...>;
183
184template<typename T, typename... Accessors>
185using QBEIntegerBitfieldUnion
186 = QSpecialIntegerBitfieldUnion<QBigEndianStorageType<T>, Accessors...>;
188template<typename... Accessors>
189using qint32_le_bitfield_union = QLEIntegerBitfieldUnion<int, Accessors...>;
190template<typename... Accessors>
191using quint32_le_bitfield_union = QLEIntegerBitfieldUnion<uint, Accessors...>;
192template<typename... Accessors>
193using qint32_be_bitfield_union = QBEIntegerBitfieldUnion<int, Accessors...>;
194template<typename... Accessors>
195using quint32_be_bitfield_union = QBEIntegerBitfieldUnion<uint, Accessors...>;
197template<int pos, int width, typename T = int>
198using qint32_le_bitfield_member
199 = QSpecialIntegerAccessor<QLittleEndianStorageType<int>, pos, width, T>;
200template<int pos, int width, typename T = uint>
201using quint32_le_bitfield_member
202 = QSpecialIntegerAccessor<QLittleEndianStorageType<uint>, pos, width, T>;
203template<int pos, int width, typename T = int>
204using qint32_be_bitfield_member
205 = QSpecialIntegerAccessor<QBigEndianStorageType<int>, pos, width, T>;
206template<int pos, int width, typename T = uint>
207using quint32_be_bitfield_member
208 = QSpecialIntegerAccessor<QBigEndianStorageType<uint>, pos, width, T>;
209
210QT_END_NAMESPACE
211
212#endif // QENDIAN_P_H
QSpecialIntegerAccessor & operator=(Type t)
Definition qendian_p.h:108
QSpecialIntegerStorage< S > Storage
Definition qendian_p.h:104
void set(typename A::Type value)
Definition qendian_p.h:143
constexpr QSpecialIntegerBitfieldUnion(QSpecialIntegerBitfieldInitializer initial)
Definition qendian_p.h:133
QSpecialIntegerStorage< S >::UnsignedStorageType data() const
Definition qendian_p.h:154
constexpr QSpecialIntegerBitfieldUnion(typename QSpecialIntegerStorage< S >::UnsignedStorageType initial)
Definition qendian_p.h:137
constexpr QSpecialIntegerBitfieldUnion()=default
friend bool operator!=(const QSpecialIntegerConstAccessor< S, pos, width, T > &i, const QSpecialIntegerConstAccessor< S, pos, width, T > &j) noexcept
Definition qendian_p.h:89
operator Type() const noexcept
Definition qendian_p.h:52
friend bool operator==(const QSpecialIntegerConstAccessor< S, pos, width, T > &i, const QSpecialIntegerConstAccessor< S, pos, width, T > &j) noexcept
Definition qendian_p.h:83
static constexpr UnsignedType mask() noexcept
Definition qendian_p.h:66
bool operator!() const noexcept
Definition qendian_p.h:64
constexpr QSpecialIntegerStorage(QSpecialIntegerBitfieldInitializer)
Definition qendian_p.h:34
constexpr QSpecialIntegerStorage(UnsignedStorageType initial)
Definition qendian_p.h:35
constexpr QSpecialIntegerStorage()=default
UnsignedStorageType val
Definition qendian_p.h:37
constexpr QSpecialIntegerBitfieldInitializer QSpecialIntegerBitfieldZero
Definition qendian_p.h:25