aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmltyperegistrar/qqmljsstreamwriter_p.h
blob: ef961de69cf2dd22df1762c333c70942702eff37 (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
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#ifndef QQMLJSSTREAMWRITER_P_H
#define QQMLJSSTREAMWRITER_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 <QtCore/QIODevice>
#include <QtCore/QList>
#include <QtCore/QString>
#include <QtCore/QScopedPointer>
#include <QtCore/QPair>

QT_BEGIN_NAMESPACE

class QQmlJSStreamWriter
{
public:
    QQmlJSStreamWriter(QByteArray *array);

    void writeStartDocument();
    void writeEndDocument();
    void writeLibraryImport(
        QByteArrayView uri, int majorVersion, int minorVersion, QByteArrayView as = {});
    void writeStartObject(QByteArrayView component);
    void writeEndObject();
    void writeScriptBinding(QByteArrayView name, QByteArrayView rhs);
    void writeStringBinding(QByteArrayView name, QAnyStringView value);
    void writeNumberBinding(QByteArrayView name, qint64 value);

    // TODO: Drop this once we can drop qmlplugindump. It is substantially weird.
    void writeEnumObjectLiteralBinding(
        QByteArrayView name, const QList<QPair<QAnyStringView, int>> &keyValue);

    // TODO: these would look better with generator functions.
    void writeArrayBinding(QByteArrayView name, const QByteArrayList &elements);
    void writeStringListBinding(QByteArrayView name, const QList<QAnyStringView> &elements);

    void write(QByteArrayView data);
    void writeBooleanBinding(QByteArrayView name, bool value);

private:
    void writeIndent();
    void writePotentialLine(const QByteArray &line);
    void flushPotentialLinesWithNewlines();

    template<typename String, typename ElementHandler>
    void doWriteArrayBinding(
            QByteArrayView name, const QList<String> &elements, ElementHandler &&handler);

    int m_indentDepth;
    QList<QByteArray> m_pendingLines;
    int m_pendingLineLength;
    bool m_maybeOneline;
    QScopedPointer<QIODevice> m_stream;
};

QT_END_NAMESPACE

#endif // QQMLJSSTREAMWRITER_P_H