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
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
27QT_BEGIN_NAMESPACE
28
29QT_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
65{
66protected:
69
70 /* not constexpr! */ static void unsorted()
71 { Q_ASSERT_X(false, "QEnumIndexedArray", "initializer is not sorted"); }
72};
73
74template <typename T, typename E, std::size_t N>
76{
77private:
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>
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) && ...))
96 return Base{std::move(init[Is].second)...};
97 }
98
99protected: // like std::stack
100 Base c;
101
102public:
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;
115
116 QEnumIndexedArray() = default;
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
178private:
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
213template <typename T, auto MAX>
214using QEnumIndexedArrayFor = QEnumIndexedArray<T, decltype(MAX), qToUnderlying(MAX)>;
215
216QT_END_NAMESPACE
217
218// Tuple Protocol
219namespace std {
220 template <typename T, typename Enum, std::size_t N>
223 template <std::size_t I, typename T, typename Enum, std::size_t N>
226}
227
228#endif // QENUMINDEXEDARRAY_P_H
QEnumIndexedArrayBase()=default
#define QT_FWD_0(name)
#define Q_REL_OP(op)
#define QT_FWD_C_0(name)
QEnumIndexedArray< T, decltype(MAX), qToUnderlying(MAX)> QEnumIndexedArrayFor
constexpr decltype(auto) value(size_type i) const
QEnumIndexedArray()=default
typename Base::const_pointer const_pointer
typename Base::size_type size_type
constexpr decltype(auto) operator[](Enum i)
typename Base::reference reference
typename Base::const_reverse_iterator const_reverse_iterator
typename Base::reverse_iterator reverse_iterator
constexpr decltype(auto) at(Enum i) const
typename Base::value_type value_type
constexpr void swap(QEnumIndexedArray &other) noexcept(noexcept(c.swap(other.c)))
typename Base::iterator iterator
typename Base::const_iterator const_iterator
constexpr decltype(auto) at(Enum i)
constexpr decltype(auto) value(size_type i)
friend constexpr void swap(QEnumIndexedArray &lhs, QEnumIndexedArray &rhs) noexcept(noexcept(lhs.swap(rhs)))
typename Base::const_reference const_reference
constexpr void fill(const value_type &val) noexcept(noexcept(c.fill(val)))
constexpr decltype(auto) operator[](Enum i) const
typename Base::pointer pointer
typename Base::difference_type difference_type