summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qoffsetstringarray_p.h
blob: 7c9e457b39b3be62aae1de8ede036acad4624980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
******************************************************************************/

#ifndef QOFFSETSTRINGARRAY_P_H
#define QOFFSETSTRINGARRAY_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "private/qglobal_p.h"

#include <tuple>
#include <array>
#include <limits>

QT_BEGIN_NAMESPACE

namespace QtPrivate {
template<int N, int O, int I, int ... Idx>
struct OffsetSequenceHelper : OffsetSequenceHelper<N - 1, O + I, Idx..., O> { };

template<int Last, int I, int S, int ... Idx>
struct OffsetSequenceHelper<1, Last, I, S, Idx...> : IndexesList<Last + I, Idx..., Last>
{
    // the unary + before std::numeric_limits below is required. Otherwise we get on g++-10.2:
    // error: comparison is always false due to limited range of data type [-Werror=type-limits]
    static const constexpr auto Length = Last + I;
    using Type = typename std::conditional<
        Last <= +std::numeric_limits<quint8>::max(),
        quint8,
        typename std::conditional<
            Last <= std::numeric_limits<quint16>::max(),
            quint16,
            int>::type
        >::type;
};

template<int ... Idx>
struct OffsetSequence : OffsetSequenceHelper<sizeof ... (Idx), 0, Idx..., 0> { };

template<int N>
struct StaticString
{
    const char data[N];
};


template<>
struct StaticString<0>
{
    static constexpr int size() noexcept
    {
        return 0;
    }
};

template<typename, typename>
struct StaticStringBuilder;

template<int ... I1, int ... I2>
struct StaticStringBuilder<IndexesList<I1...>, IndexesList<I2...>>
{

QT_WARNING_PUSH
QT_WARNING_DISABLE_MSVC(4100) // The formal parameter is not referenced in the body of the function.
                              // The unreferenced parameter is ignored.
                              // It happens when 'rs' is StaticString<0>
    template<int N1, int N2>
    static constexpr StaticString<N1 + N2> concatenate(
        const char (&ls)[N1], const StaticString<N2> &rs) noexcept
    {
        return StaticString<N1 + N2>{{ls[I1]..., rs.data[I2]...}};
    }
QT_WARNING_POP
};

template<int Sum>
constexpr StaticString<0> staticString() noexcept
{
    return StaticString<0>{};
}

QT_WARNING_PUSH
QT_WARNING_DISABLE_MSVC(4503)
template<int Sum, int I, int ... Ix>
constexpr StaticString<Sum> staticString(const char (&s)[I], const char (&...sx)[Ix]) noexcept
{
    return StaticStringBuilder<
        makeIndexSequence<I>,
        makeIndexSequence<Sum - I>>::concatenate(s, staticString<Sum - I>(sx...));
}
QT_WARNING_POP
} // namespace QtPrivate

QT_WARNING_PUSH
#if defined(Q_CC_GNU_ONLY) && __GNUC__ == 9
QT_WARNING_DISABLE_GCC("-Wstringop-overflow")
#endif
template<typename T, int SizeString, int SizeOffsets>
class QOffsetStringArray
{
public:
    using Type = T;

    template<int ... Ox>
    constexpr QOffsetStringArray(const QtPrivate::StaticString<SizeString> &str,
                                 QtPrivate::IndexesList<SizeString, Ox...>) noexcept
        : m_string(str),
          m_offsets{Ox...}
    { }

    constexpr inline const char *operator[](const int index) const noexcept
    {
        return m_string.data + m_offsets[qBound(int(0), index, SizeOffsets - 1)];
    }

    constexpr inline const char *at(const int index) const noexcept
    {
        return m_string.data + m_offsets[index];
    }

    constexpr inline const char *str() const { return m_string.data; }
    constexpr inline const T *offsets() const { return m_offsets; }
    constexpr inline int count() const { return SizeOffsets; }

    static constexpr const auto sizeString = SizeString;
    static constexpr const auto sizeOffsets = SizeOffsets;

private:
    QtPrivate::StaticString<SizeString> m_string;
    const T m_offsets[SizeOffsets];
};
QT_WARNING_POP

template<typename T, int N, int ... Ox>
constexpr QOffsetStringArray<T, N, sizeof ... (Ox)> qOffsetStringArray(
    const QtPrivate::StaticString<N> &string,
    QtPrivate::IndexesList<N, Ox...> offsets) noexcept
{
    return QOffsetStringArray<T, N, sizeof ... (Ox)>(
               string,
               offsets);
}

template<int ... Nx>
struct QOffsetStringArrayRet
{
    using Offsets = QtPrivate::OffsetSequence<Nx...>;
    using Type = QOffsetStringArray<typename Offsets::Type, Offsets::Length, sizeof ... (Nx)>;
};

template<int ... Nx>
constexpr auto qOffsetStringArray(const char (&...strings)[Nx]) noexcept -> typename QOffsetStringArrayRet<Nx...>::Type
{
    using Offsets = QtPrivate::OffsetSequence<Nx...>;
    return qOffsetStringArray<typename Offsets::Type>(
            QtPrivate::staticString<Offsets::Length>(strings...), Offsets{});
}

QT_END_NAMESPACE

#endif // QOFFSETSTRINGARRAY_P_H