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
59QT_BEGIN_NAMESPACE
60
61namespace QtPrivate {
62template<int N, int O, int I, int ... Idx>
63struct OffsetSequenceHelper : OffsetSequenceHelper<N - 1, O + I, Idx..., O> { };
64
65template<int Last, int I, int S, int ... Idx>
66struct 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
81template<int ... Idx>
82struct OffsetSequence : OffsetSequenceHelper<sizeof ... (Idx), 0, Idx..., 0> { };
83
84template<int N>
85struct StaticString
86{
87 const char data[N];
88};
89
90
91template<>
92struct StaticString<0>
93{
94 static constexpr int size() noexcept
95 {
96 return 0;
97 }
98};
99
100template<typename, typename>
101struct StaticStringBuilder;
102
103template<int ... I1, int ... I2>
104struct StaticStringBuilder<IndexesList<I1...>, IndexesList<I2...>>
105{
106
107QT_WARNING_PUSH
108QT_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 }
117QT_WARNING_POP
118};
119
120template<int Sum>
121constexpr StaticString<0> staticString() noexcept
122{
123 return StaticString<0>{};
124}
125
126QT_WARNING_PUSH
127QT_WARNING_DISABLE_MSVC(4503)
128template<int Sum, int I, int ... Ix>
129constexpr 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}
135QT_WARNING_POP
136} // namespace QtPrivate
137
138QT_WARNING_PUSH
139#if defined(Q_CC_GNU) && __GNUC__ == 9
140QT_WARNING_DISABLE_GCC("-Wstringop-overflow")
141#endif
142template<typename T, int SizeString, int SizeOffsets>
143class QOffsetStringArray
144{
145public:
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
172private:
173 QtPrivate::StaticString<SizeString> m_string;
174 const T m_offsets[SizeOffsets];
175};
176QT_WARNING_POP
177
178template<typename T, int N, int ... Ox>
179constexpr 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
188template<int ... Nx>
189struct QOffsetStringArrayRet
190{
191 using Offsets = QtPrivate::OffsetSequence<Nx...>;
192 using Type = QOffsetStringArray<typename Offsets::Type, Offsets::Length, sizeof ... (Nx)>;
193};
194
195template<int ... Nx>
196constexpr 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
203QT_END_NAMESPACE
204
205#endif // QOFFSETSTRINGARRAY_P_H
206