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
qenumindexedarray_p.h
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
3
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5
#
ifndef
QENUMINDEXEDARRAY_P_H
6
#
define
QENUMINDEXEDARRAY_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
/
qtclasshelpermacros
.
h
>
20
#
include
<
QtCore
/
qttypetraits
.
h
>
21
#
include
<
QtCore
/
private
/
qglobal_p
.
h
>
22
23
#
include
<
array
>
24
#
include
<
QtCore
/
q20utility
.
h
>
25
#
include
<
QtCore
/
q20type_traits
.
h
>
26
27
QT_BEGIN_NAMESPACE
28
29
QT_ENABLE_P0846_SEMANTICS_FOR(get)
30
31
/*
32
This is a variant of `std::array`, differing in that it uses a custom
33
type to index into the array.
34
35
The class is useful as a container indexed by some enumeration type,
36
since, apart from the value() method, it doesn't allow passing
37
non-enum types as indexes to its functions.
38
39
There are two modes to use this class: QEnumIndexedArray takes the value
40
type, the enum type, and the size as three separate template arguments.
41
For convenience, the QEnumIndexedArrayFor alias deduces both the
42
enum-type and the maximum value with a single template parameter.
43
44
A simple example using Qt:
45
46
struct CursorData {
47
~~~~
48
};
49
constexpr auto NumQtCursorShapes = Qt::CursorShape(Qt::CustomCursor + 1);
50
using CursorIndexedArray = QEnumIndexedArrayFor<
51
std::unique_ptr<CursorData>,
52
NumQtCursorShapes
53
>;
54
CursorIndexedArray cursorData;
55
56
~~~~
57
auto& cross = cursorData[Qt::CrossCursor]; // OK
58
auto& ibeam = cursorData[4]; // Qt::IBeamCursor; ERROR: no op[] taking int
59
60
For best results, the enumeration should be zero-based, contiguous, and
61
closed.
62
*/
63
64
class
QEnumIndexedArrayBase
65
{
66
protected
:
67
QEnumIndexedArrayBase
() =
default
;
68
QT_DECLARE_RO5_SMF_AS_DEFAULTED
(
QEnumIndexedArrayBase
)
69
70
/* not constexpr! */
static
void
unsorted
()
71
{ Q_ASSERT_X(
false
,
"QEnumIndexedArray"
,
"initializer is not sorted"
); }
72
};
73
74
template
<
typename
T,
typename
E, std::size_t N>
75
struct
QEnumIndexedArray
:
private
QEnumIndexedArrayBase
76
{
77
private
:
78
// re-export the template arguments (T is value_type below):
79
using
Enum = E;
80
static
constexpr
inline
std
::size_t Size = N;
81
82
static
constexpr
auto
map(Enum e)
noexcept
{
return
static_cast
<
std
::size_t>(e); }
83
using
Base =
std
::array<T, Size>;
84
85
template
<
typename
S
>
86
using
if_cvref_self
=
std
::
enable_if_t
<
87
std
::
is_same_v
<
q20
::
remove_cvref_t
<
S
>,
QEnumIndexedArray
>
88
,
bool
>;
89
90
template
<
std
::size_t...Is>
91
static
constexpr
Base extract2nd(
std
::array<
std
::pair<Enum, T>, Size> &&init,
92
std::index_sequence<Is...>)
93
{
94
if
(!((map(init[Is].first) == Is) && ...))
95
QEnumIndexedArrayBase
::
unsorted
(
)
;
96
return
Base{
std
::move(init[Is].second)...};
97
}
98
99
protected
:
// like std::stack
100
Base
c
;
101
102
public
:
103
using
value_type
=
typename
Base::
value_type
;
104
using
size_type
=
typename
Base::
size_type
;
105
using
index_type
= Enum;
// new, not on std::array
106
using
difference_type
=
typename
Base::
difference_type
;
107
using
reference
=
typename
Base::
reference
;
108
using
const_reference
=
typename
Base::
const_reference
;
109
using
pointer
=
typename
Base::
pointer
;
110
using
const_pointer
=
typename
Base::
const_pointer
;
111
using
iterator
=
typename
Base::
iterator
;
112
using
const_iterator
=
typename
Base::
const_iterator
;
113
using
reverse_iterator
=
typename
Base::
reverse_iterator
;
114
using
const_reverse_iterator
=
typename
Base::
const_reverse_iterator
;
115
116
QEnumIndexedArray
() =
default
;
117
Q_IMPLICIT
constexpr
QEnumIndexedArray
(
std
::
array
<
T
,
Size
>
init
)
118
noexcept
(
std
::
is_nothrow_move_constructible_v
<
T
>)
119
:
c
{
std
::
move
(
init
)}
120
{}
121
Q_IMPLICIT
constexpr
QEnumIndexedArray
(
std
::
array
<
std
::
pair
<
Enum
,
T
>,
Size
>
init
)
122
noexcept
(
std
::
is_nothrow_move_constructible_v
<
T
>)
123
:
c
(
extract2nd
(
std
::
move
(
init
),
std
::
make_index_sequence
<
Size
>{}))
124
{}
125
126
constexpr
decltype
(
auto
)
value
(
size_type
i) {
return
c
[i]; }
// new, not on std::array
127
constexpr
decltype
(
auto
)
value
(
size_type
i)
const
{
return
c
[i]; }
// new, not on std::array
128
constexpr
decltype
(
auto
)
at
(Enum i) {
return
c
.at(map(i)); }
129
constexpr
decltype
(
auto
)
at
(Enum i)
const
{
return
c
.at(map(i)); }
130
constexpr
decltype
(
auto
)
operator
[](Enum i) {
return
c
[map(i)]; }
131
constexpr
decltype
(
auto
)
operator
[](Enum i)
const
{
return
c
[map(i)]; }
132
133
#
define
QT_FWD_IMPL_0
(
name
,
cv
)
134
constexpr
decltype
(
auto
)
name
(
)
cv
&
135
noexcept
(
noexcept
(
c
.
name
(
)
)
)
136
{
return
c
.
name
(
)
;
}
137
constexpr
decltype
(
auto
)
name
(
)
cv
&&
138
noexcept
(
noexcept
(
std
::
move
(
c
)
.
name
(
)
)
)
139
{
return
std
::
move
(
c
)
.
name
(
)
;
}
140
/*end*/
141
#
define
QT_FWD_M_0
(
name
)
QT_FWD_IMPL_0
(
name
,
)
142
#
define
QT_FWD_C_0
(
name
)
QT_FWD_IMPL_0
(
name
,
const
)
143
#
define
QT_FWD_0
(
name
)
144
QT_FWD_M_0
(
name
)
145
QT_FWD_C_0
(
name
)
146
/*end*/
147
148
QT_FWD_0
(back)
149
QT_FWD_0
(data)
150
QT_FWD_0
(front)
151
152
QT_FWD_0
(begin)
153
QT_FWD_C_0
(cbegin)
154
QT_FWD_C_0
(cend)
155
QT_FWD_C_0
(crbegin)
156
QT_FWD_C_0
(crend)
157
QT_FWD_0
(end)
158
QT_FWD_0
(rbegin)
159
QT_FWD_0
(rend)
160
161
QT_FWD_C_0
(empty)
162
QT_FWD_C_0
(max_size)
163
QT_FWD_C_0
(size)
164
165
#
undef
QT_FWD_0
166
#
undef
QT_FWD_C_0
167
#
undef
QT_FWD_M_0
168
#
undef
QT_FWD_IMPL_0
169
170
constexpr
void
fill
(
const
value_type
& val)
171
noexcept
(
noexcept
(
c
.
fill
(
val
)))
172
{
c
.fill(val); }
173
174
constexpr
void
swap
(
QEnumIndexedArray
&other)
175
noexcept
(
noexcept
(
c
.
swap
(
other
.
c
)))
176
{
c
.swap(other
.
c
); }
177
178
private
:
179
friend
constexpr
void
swap
(
QEnumIndexedArray
&lhs,
QEnumIndexedArray
&rhs)
180
noexcept
(
noexcept
(
lhs
.
swap
(
rhs
)))
181
{ lhs
.
swap
(
rhs
)
; }
182
183
#
define
Q_REL_OP
(
op
)
184
friend
constexpr
auto
operator
op
(
const
QEnumIndexedArray
&
lhs
,
const
QEnumIndexedArray
&
rhs
)
185
noexcept
(
noexcept
(
lhs
.
c
op
rhs
.
c
)
)
/* copy from implementation */
186
{
return
lhs
.
c
op
rhs
.
c
;
}
187
188
Q_REL_OP
(==)
189
#
ifndef
__cpp_impl_three_way_comparison
190
Q_REL_OP
(!=)
191
#
endif
192
193
#
ifdef
__cpp_lib_three_way_comparison
194
Q_REL_OP
(<=>)
195
#
else
196
Q_REL_OP
(<)
197
Q_REL_OP
(>)
198
Q_REL_OP
(<=)
199
Q_REL_OP
(>=)
200
#
endif
201
#
undef
Q_REL_OP
202
203
template
<
std
::
size_t
I
,
typename
EIA
,
204
std
::
enable_if_t
<
I
<
q20
::
remove_cvref_t
<
EIA
>::
Size
,
bool
> =
true
,
205
if_cvref_self
<
EIA
> =
true
>
206
friend
constexpr
decltype
(
auto
)
get
(
EIA
&&
e
)
noexcept
207
{
208
using
std
::
get
;
209
return
get
<
I
>(
std
::
forward
<
EIA
>(
e
).
c
);
210
}
211
};
212
213
template
<
typename
T,
auto
MAX>
214
using
QEnumIndexedArrayFor
=
QEnumIndexedArray
<T,
decltype
(MAX), qToUnderlying(MAX)>;
215
216
QT_END_NAMESPACE
217
218
// Tuple Protocol
219
namespace
std
{
220
template
<
typename
T
,
typename
Enum
,
std
::
size_t
N
>
221
class
tuple_size
<
QT_PREPEND_NAMESPACE
(
QEnumIndexedArray
)<
T
,
Enum
,
N
>>
222
:
public
std
::
integral_constant
<
size_t
,
N
> {};
223
template
<
std
::
size_t
I
,
typename
T
,
typename
Enum
,
std
::
size_t
N
>
224
class
tuple_element
<
I
,
QT_PREPEND_NAMESPACE
(
QEnumIndexedArray
)<
T
,
Enum
,
N
>>
225
{
public
:
using
type
=
T
; };
226
}
227
228
#
endif
// QENUMINDEXEDARRAY_P_H
QEnumIndexedArrayBase
Definition
qenumindexedarray_p.h:65
QEnumIndexedArrayBase::QEnumIndexedArrayBase
QEnumIndexedArrayBase()=default
QEnumIndexedArrayBase::unsorted
static void unsorted()
Definition
qenumindexedarray_p.h:70
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
QT_FWD_0
#define QT_FWD_0(name)
Definition
qenumindexedarray_p.h:143
Q_REL_OP
#define Q_REL_OP(op)
Definition
qenumindexedarray_p.h:183
QT_FWD_C_0
#define QT_FWD_C_0(name)
Definition
qenumindexedarray_p.h:142
QEnumIndexedArrayFor
QEnumIndexedArray< T, decltype(MAX), qToUnderlying(MAX)> QEnumIndexedArrayFor
Definition
qenumindexedarray_p.h:214
QEnumIndexedArray
Definition
qenumindexedarray_p.h:76
QEnumIndexedArray::value
constexpr decltype(auto) value(size_type i) const
Definition
qenumindexedarray_p.h:127
QEnumIndexedArray::c
Base c
Definition
qenumindexedarray_p.h:100
QEnumIndexedArray::QEnumIndexedArray
QEnumIndexedArray()=default
QEnumIndexedArray::const_pointer
typename Base::const_pointer const_pointer
Definition
qenumindexedarray_p.h:110
QEnumIndexedArray::size_type
typename Base::size_type size_type
Definition
qenumindexedarray_p.h:104
QEnumIndexedArray::operator[]
constexpr decltype(auto) operator[](Enum i)
Definition
qenumindexedarray_p.h:130
QEnumIndexedArray::reference
typename Base::reference reference
Definition
qenumindexedarray_p.h:107
QEnumIndexedArray::const_reverse_iterator
typename Base::const_reverse_iterator const_reverse_iterator
Definition
qenumindexedarray_p.h:114
QEnumIndexedArray::reverse_iterator
typename Base::reverse_iterator reverse_iterator
Definition
qenumindexedarray_p.h:113
QEnumIndexedArray::at
constexpr decltype(auto) at(Enum i) const
Definition
qenumindexedarray_p.h:129
QEnumIndexedArray::value_type
typename Base::value_type value_type
Definition
qenumindexedarray_p.h:103
QEnumIndexedArray::swap
constexpr void swap(QEnumIndexedArray &other) noexcept(noexcept(c.swap(other.c)))
Definition
qenumindexedarray_p.h:174
QEnumIndexedArray::iterator
typename Base::iterator iterator
Definition
qenumindexedarray_p.h:111
QEnumIndexedArray::const_iterator
typename Base::const_iterator const_iterator
Definition
qenumindexedarray_p.h:112
QEnumIndexedArray::at
constexpr decltype(auto) at(Enum i)
Definition
qenumindexedarray_p.h:128
QEnumIndexedArray::value
constexpr decltype(auto) value(size_type i)
Definition
qenumindexedarray_p.h:126
QEnumIndexedArray::index_type
Enum index_type
Definition
qenumindexedarray_p.h:105
QEnumIndexedArray::swap
friend constexpr void swap(QEnumIndexedArray &lhs, QEnumIndexedArray &rhs) noexcept(noexcept(lhs.swap(rhs)))
Definition
qenumindexedarray_p.h:179
QEnumIndexedArray::const_reference
typename Base::const_reference const_reference
Definition
qenumindexedarray_p.h:108
QEnumIndexedArray::fill
constexpr void fill(const value_type &val) noexcept(noexcept(c.fill(val)))
Definition
qenumindexedarray_p.h:170
QEnumIndexedArray::operator[]
constexpr decltype(auto) operator[](Enum i) const
Definition
qenumindexedarray_p.h:131
QEnumIndexedArray::pointer
typename Base::pointer pointer
Definition
qenumindexedarray_p.h:109
QEnumIndexedArray::difference_type
typename Base::difference_type difference_type
Definition
qenumindexedarray_p.h:106
qtbase
src
corelib
tools
qenumindexedarray_p.h
Generated on
for Qt by
1.18.0