summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qtmochelpers.h
blob: 7bdaaf276b42ac97ab1793ba7a6477bb27d6b5c8 (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
// Copyright (C) 2022 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QTMOCHELPERS_H
#define QTMOCHELPERS_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API. It exists to be used by the code that
// moc generates. This file will not change quickly, but it over the long term,
// it will likely change or even be removed.
//
// We mean it.
//

#include <QtCore/qglobal.h>

#if 0
#pragma qt_no_master_include
#endif

QT_BEGIN_NAMESPACE
namespace QtMocHelpers {
template <uint... Nx> struct StringData
{
    static constexpr size_t calculateStringSize()
    {
        // same as:
        //   return (0 + ... + Nx);
        // but not using the fold expression to avoid exceeding compiler limits
        size_t total = 0;
        uint sizes[] = { Nx... };
        for (uint n : sizes)
            total += n;
        return total;
    }

    // The maximum Size of a string literal is 2 GB on 32-bit and 4 GB on 64-bit
    // (but the compiler is likely to give up before you get anywhere near that much)
    static constexpr size_t MaxStringSize =
            (std::min)(size_t((std::numeric_limits<uint>::max)()),
                       size_t((std::numeric_limits<qsizetype>::max)()));

    static constexpr size_t StringSize = calculateStringSize();
    static_assert(StringSize <= MaxStringSize, "Meta Object data is too big");

    uint offsetsAndSizes[2 * sizeof...(Nx)] = {};
    char stringdata0[StringSize] = {};
    constexpr StringData() = default;
};

template <uint... Nx> constexpr auto stringData(const char (&...strings)[Nx])
{
    StringData<Nx...> result;
    const char *inputs[] = { strings... };
    uint sizes[] = { Nx... };

    uint offset = 0;
    char *output = result.stringdata0;
    for (size_t i = 0; i < sizeof...(Nx); ++i) {
        // copy the input string, including the terminating null
        uint len = sizes[i];
        for (uint j = 0; j < len; ++j)
            output[offset + j] = inputs[i][j];
        result.offsetsAndSizes[2 * i] = offset + sizeof(result.offsetsAndSizes);
        result.offsetsAndSizes[2 * i + 1] = len - 1;
        offset += len;
    }

    return result;
}

#if !defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1000
// It looks like there's a bug in GCC 9
#  define QT_MOC_HAS_STRINGDATA       1
#endif

} // namespace QtMocHelpers
QT_END_NAMESPACE

QT_USE_NAMESPACE

#endif // QTMOCHELPERS_H