1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QOFFSETSTRINGARRAY_P_H |
41 | #define QOFFSETSTRINGARRAY_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include "private/qglobal_p.h" |
55 | |
56 | #include <tuple> |
57 | #include <array> |
58 | |
59 | QT_BEGIN_NAMESPACE |
60 | |
61 | namespace QtPrivate { |
62 | template<int N, int O, int I, int ... Idx> |
63 | struct OffsetSequenceHelper : OffsetSequenceHelper<N - 1, O + I, Idx..., O> { }; |
64 | |
65 | template<int Last, int I, int S, int ... Idx> |
66 | struct OffsetSequenceHelper<1, Last, I, S, Idx...> : IndexesList<Last + I, Idx..., Last> |
67 | { |
68 | // the unary + before std::numeric_limits below is required. Otherwise we get on g++-10.2: |
69 | // error: comparison is always false due to limited range of data type [-Werror=type-limits] |
70 | static const constexpr auto Length = Last + I; |
71 | using Type = typename std::conditional< |
72 | Last <= +std::numeric_limits<quint8>::max(), |
73 | quint8, |
74 | typename std::conditional< |
75 | Last <= std::numeric_limits<quint16>::max(), |
76 | quint16, |
77 | int>::type |
78 | >::type; |
79 | }; |
80 | |
81 | template<int ... Idx> |
82 | struct OffsetSequence : OffsetSequenceHelper<sizeof ... (Idx), 0, Idx..., 0> { }; |
83 | |
84 | template<int N> |
85 | struct StaticString |
86 | { |
87 | const char data[N]; |
88 | }; |
89 | |
90 | |
91 | template<> |
92 | struct StaticString<0> |
93 | { |
94 | static constexpr int size() noexcept |
95 | { |
96 | return 0; |
97 | } |
98 | }; |
99 | |
100 | template<typename, typename> |
101 | struct StaticStringBuilder; |
102 | |
103 | template<int ... I1, int ... I2> |
104 | struct StaticStringBuilder<IndexesList<I1...>, IndexesList<I2...>> |
105 | { |
106 | |
107 | QT_WARNING_PUSH |
108 | QT_WARNING_DISABLE_MSVC(4100) // The formal parameter is not referenced in the body of the function. |
109 | // The unreferenced parameter is ignored. |
110 | // It happens when 'rs' is StaticString<0> |
111 | template<int N1, int N2> |
112 | static constexpr StaticString<N1 + N2> concatenate( |
113 | const char (&ls)[N1], const StaticString<N2> &rs) noexcept |
114 | { |
115 | return StaticString<N1 + N2>{{ls[I1]..., rs.data[I2]...}}; |
116 | } |
117 | QT_WARNING_POP |
118 | }; |
119 | |
120 | template<int Sum> |
121 | constexpr StaticString<0> staticString() noexcept |
122 | { |
123 | return StaticString<0>{}; |
124 | } |
125 | |
126 | QT_WARNING_PUSH |
127 | QT_WARNING_DISABLE_MSVC(4503) |
128 | template<int Sum, int I, int ... Ix> |
129 | constexpr StaticString<Sum> staticString(const char (&s)[I], const char (&...sx)[Ix]) noexcept |
130 | { |
131 | return StaticStringBuilder< |
132 | makeIndexSequence<I>, |
133 | makeIndexSequence<Sum - I>>::concatenate(s, staticString<Sum - I>(sx...)); |
134 | } |
135 | QT_WARNING_POP |
136 | } // namespace QtPrivate |
137 | |
138 | QT_WARNING_PUSH |
139 | #if defined(Q_CC_GNU) && __GNUC__ == 9 |
140 | QT_WARNING_DISABLE_GCC("-Wstringop-overflow" ) |
141 | #endif |
142 | template<typename T, int SizeString, int SizeOffsets> |
143 | class QOffsetStringArray |
144 | { |
145 | public: |
146 | using Type = T; |
147 | |
148 | template<int ... Ox> |
149 | constexpr QOffsetStringArray(const QtPrivate::StaticString<SizeString> &str, |
150 | QtPrivate::IndexesList<SizeString, Ox...>) noexcept |
151 | : m_string(str), |
152 | m_offsets{Ox...} |
153 | { } |
154 | |
155 | constexpr inline const char *operator[](const int index) const noexcept |
156 | { |
157 | return m_string.data + m_offsets[qBound(int(0), index, SizeOffsets - 1)]; |
158 | } |
159 | |
160 | constexpr inline const char *at(const int index) const noexcept |
161 | { |
162 | return m_string.data + m_offsets[index]; |
163 | } |
164 | |
165 | constexpr inline const char *str() const { return m_string.data; } |
166 | constexpr inline const T *offsets() const { return m_offsets; } |
167 | constexpr inline int count() const { return SizeOffsets; } |
168 | |
169 | static constexpr const auto sizeString = SizeString; |
170 | static constexpr const auto sizeOffsets = SizeOffsets; |
171 | |
172 | private: |
173 | QtPrivate::StaticString<SizeString> m_string; |
174 | const T m_offsets[SizeOffsets]; |
175 | }; |
176 | QT_WARNING_POP |
177 | |
178 | template<typename T, int N, int ... Ox> |
179 | constexpr QOffsetStringArray<T, N, sizeof ... (Ox)> qOffsetStringArray( |
180 | const QtPrivate::StaticString<N> &string, |
181 | QtPrivate::IndexesList<N, Ox...> offsets) noexcept |
182 | { |
183 | return QOffsetStringArray<T, N, sizeof ... (Ox)>( |
184 | string, |
185 | offsets); |
186 | } |
187 | |
188 | template<int ... Nx> |
189 | struct QOffsetStringArrayRet |
190 | { |
191 | using Offsets = QtPrivate::OffsetSequence<Nx...>; |
192 | using Type = QOffsetStringArray<typename Offsets::Type, Offsets::Length, sizeof ... (Nx)>; |
193 | }; |
194 | |
195 | template<int ... Nx> |
196 | constexpr auto qOffsetStringArray(const char (&...strings)[Nx]) noexcept -> typename QOffsetStringArrayRet<Nx...>::Type |
197 | { |
198 | using Offsets = QtPrivate::OffsetSequence<Nx...>; |
199 | return qOffsetStringArray<typename Offsets::Type>( |
200 | QtPrivate::staticString<Offsets::Length>(strings...), Offsets{}); |
201 | } |
202 | |
203 | QT_END_NAMESPACE |
204 | |
205 | #endif // QOFFSETSTRINGARRAY_P_H |
206 | |