| 1 | // Copyright (C) 2020 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #ifndef QQMLJSSTREAMWRITER_P_H |
| 5 | #define QQMLJSSTREAMWRITER_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | |
| 17 | #include <QtCore/QIODevice> |
| 18 | #include <QtCore/QList> |
| 19 | #include <QtCore/QString> |
| 20 | #include <QtCore/QScopedPointer> |
| 21 | #include <QtCore/QPair> |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | class QQmlJSStreamWriter |
| 26 | { |
| 27 | public: |
| 28 | QQmlJSStreamWriter(QByteArray *array); |
| 29 | |
| 30 | void writeStartDocument(); |
| 31 | void writeEndDocument(); |
| 32 | void writeLibraryImport( |
| 33 | QByteArrayView uri, int majorVersion, int minorVersion, QByteArrayView as = {}); |
| 34 | void writeStartObject(QByteArrayView component); |
| 35 | void writeEndObject(); |
| 36 | void writeScriptBinding(QByteArrayView name, QByteArrayView rhs); |
| 37 | void writeStringBinding(QByteArrayView name, QAnyStringView value); |
| 38 | void writeNumberBinding(QByteArrayView name, qint64 value); |
| 39 | |
| 40 | // TODO: Drop this once we can drop qmlplugindump. It is substantially weird. |
| 41 | void writeEnumObjectLiteralBinding( |
| 42 | QByteArrayView name, const QList<QPair<QAnyStringView, int>> &keyValue); |
| 43 | |
| 44 | // TODO: these would look better with generator functions. |
| 45 | void writeArrayBinding(QByteArrayView name, const QByteArrayList &elements); |
| 46 | void writeStringListBinding(QByteArrayView name, const QList<QAnyStringView> &elements); |
| 47 | |
| 48 | void write(QByteArrayView data); |
| 49 | void writeBooleanBinding(QByteArrayView name, bool value); |
| 50 | |
| 51 | private: |
| 52 | void writeIndent(); |
| 53 | void writePotentialLine(const QByteArray &line); |
| 54 | void flushPotentialLinesWithNewlines(); |
| 55 | |
| 56 | template<typename String, typename ElementHandler> |
| 57 | void doWriteArrayBinding( |
| 58 | QByteArrayView name, const QList<String> &elements, ElementHandler &&handler); |
| 59 | |
| 60 | int m_indentDepth; |
| 61 | QList<QByteArray> m_pendingLines; |
| 62 | int m_pendingLineLength; |
| 63 | bool m_maybeOneline; |
| 64 | QScopedPointer<QIODevice> m_stream; |
| 65 | }; |
| 66 | |
| 67 | QT_END_NAMESPACE |
| 68 | |
| 69 | #endif // QQMLJSSTREAMWRITER_P_H |
| 70 | |