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 | static const constexpr auto Length = Last + I; |
69 | using Type = typename std::conditional< |
70 | Last <= std::numeric_limits<quint8>::max(), |
71 | quint8, |
72 | typename std::conditional< |
73 | Last <= std::numeric_limits<quint16>::max(), |
74 | quint16, |
75 | int>::type |
76 | >::type; |
77 | }; |
78 | |
79 | template<int ... Idx> |
80 | struct OffsetSequence : OffsetSequenceHelper<sizeof ... (Idx), 0, Idx..., 0> { }; |
81 | |
82 | template<int N> |
83 | struct StaticString |
84 | { |
85 | const char data[N]; |
86 | }; |
87 | |
88 | |
89 | template<> |
90 | struct StaticString<0> |
91 | { |
92 | static constexpr int size() noexcept |
93 | { |
94 | return 0; |
95 | } |
96 | }; |
97 | |
98 | template<typename, typename> |
99 | struct StaticStringBuilder; |
100 | |
101 | template<int ... I1, int ... I2> |
102 | struct StaticStringBuilder<IndexesList<I1...>, IndexesList<I2...>> |
103 | { |
104 | |
105 | QT_WARNING_PUSH |
106 | QT_WARNING_DISABLE_MSVC(4100) // The formal parameter is not referenced in the body of the function. |
107 | // The unreferenced parameter is ignored. |
108 | // It happens when 'rs' is StaticString<0> |
109 | template<int N1, int N2> |
110 | static constexpr StaticString<N1 + N2> concatenate( |
111 | const char (&ls)[N1], const StaticString<N2> &rs) noexcept |
112 | { |
113 | return StaticString<N1 + N2>{{ls[I1]..., rs.data[I2]...}}; |
114 | } |
115 | QT_WARNING_POP |
116 | }; |
117 | |
118 | template<int Sum> |
119 | constexpr StaticString<0> staticString() noexcept |
120 | { |
121 | return StaticString<0>{}; |
122 | } |
123 | |
124 | QT_WARNING_PUSH |
125 | QT_WARNING_DISABLE_MSVC(4503) |
126 | template<int Sum, int I, int ... Ix> |
127 | constexpr StaticString<Sum> staticString(const char (&s)[I], const char (&...sx)[Ix]) noexcept |
128 | { |
129 | return StaticStringBuilder< |
130 | makeIndexSequence<I>, |
131 | makeIndexSequence<Sum - I>>::concatenate(s, staticString<Sum - I>(sx...)); |
132 | } |
133 | QT_WARNING_POP |
134 | } // namespace QtPrivate |
135 | |
136 | template<typename T, int SizeString, int SizeOffsets> |
137 | class QOffsetStringArray |
138 | { |
139 | public: |
140 | using Type = T; |
141 | |
142 | template<int ... Ox> |
143 | constexpr QOffsetStringArray(const QtPrivate::StaticString<SizeString> &str, |
144 | QtPrivate::IndexesList<SizeString, Ox...>) noexcept |
145 | : m_string(str), |
146 | m_offsets{Ox...} |
147 | { } |
148 | |
149 | constexpr inline const char *operator[](const int index) const noexcept |
150 | { |
151 | return m_string.data + m_offsets[qBound(min: int(0), val: index, max: SizeOffsets - 1)]; |
152 | } |
153 | |
154 | constexpr inline const char *at(const int index) const noexcept |
155 | { |
156 | return m_string.data + m_offsets[index]; |
157 | } |
158 | |
159 | constexpr inline const char *str() const { return m_string.data; } |
160 | constexpr inline const T *offsets() const { return m_offsets; } |
161 | constexpr inline int count() const { return SizeOffsets; }; |
162 | |
163 | static constexpr const auto sizeString = SizeString; |
164 | static constexpr const auto sizeOffsets = SizeOffsets; |
165 | |
166 | private: |
167 | QtPrivate::StaticString<SizeString> m_string; |
168 | const T m_offsets[SizeOffsets]; |
169 | }; |
170 | |
171 | template<typename T, int N, int ... Ox> |
172 | constexpr QOffsetStringArray<T, N, sizeof ... (Ox)> qOffsetStringArray( |
173 | const QtPrivate::StaticString<N> &string, |
174 | QtPrivate::IndexesList<N, Ox...> offsets) noexcept |
175 | { |
176 | return QOffsetStringArray<T, N, sizeof ... (Ox)>( |
177 | string, |
178 | offsets); |
179 | } |
180 | |
181 | template<int ... Nx> |
182 | struct QOffsetStringArrayRet |
183 | { |
184 | using Offsets = QtPrivate::OffsetSequence<Nx...>; |
185 | using Type = QOffsetStringArray<typename Offsets::Type, Offsets::Length, sizeof ... (Nx)>; |
186 | }; |
187 | |
188 | template<int ... Nx> |
189 | constexpr auto qOffsetStringArray(const char (&...strings)[Nx]) noexcept -> typename QOffsetStringArrayRet<Nx...>::Type |
190 | { |
191 | using Offsets = QtPrivate::OffsetSequence<Nx...>; |
192 | return qOffsetStringArray<typename Offsets::Type>( |
193 | QtPrivate::staticString<Offsets::Length>(strings...), Offsets{}); |
194 | } |
195 | |
196 | QT_END_NAMESPACE |
197 | |
198 | #endif // QOFFSETSTRINGARRAY_P_H |
199 | |