1 | // Copyright (C) 2021 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 QMLTCOUTPUTPRIMITIVES_H |
5 | #define QMLTCOUTPUTPRIMITIVES_H |
6 | |
7 | #include <QtCore/qstack.h> |
8 | #include <QtCore/qstring.h> |
9 | #include <QtCore/qstringbuilder.h> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | struct QmltcOutput |
14 | { |
15 | QString ; |
16 | QString cpp; |
17 | }; |
18 | |
19 | class QmltcOutputWrapper |
20 | { |
21 | QmltcOutput &m_code; |
22 | |
23 | template<typename String> |
24 | static void rawAppend(QString &out, const String &what, int = 0) |
25 | { |
26 | constexpr char16_t newLine[] = u"\n" ; |
27 | out += QString(extraIndent * 4, u' ') + what + newLine; |
28 | } |
29 | |
30 | public: |
31 | QmltcOutputWrapper(QmltcOutput &code) : m_code(code) { } |
32 | const QmltcOutput &code() const { return m_code; } |
33 | |
34 | QStack<QString> memberScopes; // member name scopes e.g. MyClass::MySubclass:: |
35 | int = 0; // header indentation level |
36 | int cppIndent = 0; // cpp indentation level |
37 | |
38 | // manages current scope of the generated code, which is necessary for |
39 | // cpp file generation. Example: |
40 | // class MyClass { MyClass(); }; - in header |
41 | // MyClass::MyClass() {} - in cpp |
42 | // MemberNameScope makes sure "MyClass::" is recorded |
43 | struct MemberNameScope |
44 | { |
45 | QmltcOutputWrapper *m_code; |
46 | MemberNameScope(QmltcOutputWrapper *code, const QString &str) : m_code(code) |
47 | { |
48 | m_code->memberScopes.push(t: str); |
49 | } |
50 | ~MemberNameScope() { m_code->memberScopes.pop(); } |
51 | Q_DISABLE_COPY_MOVE(MemberNameScope) |
52 | }; |
53 | |
54 | struct |
55 | { |
56 | QmltcOutputWrapper *; |
57 | (QmltcOutputWrapper *code) : m_code(code) { ++m_code->headerIndent; } |
58 | () { --m_code->headerIndent; } |
59 | Q_DISABLE_COPY_MOVE() |
60 | }; |
61 | |
62 | struct CppIndentationScope |
63 | { |
64 | QmltcOutputWrapper *m_code; |
65 | CppIndentationScope(QmltcOutputWrapper *code) : m_code(code) { ++m_code->cppIndent; } |
66 | ~CppIndentationScope() { --m_code->cppIndent; } |
67 | Q_DISABLE_COPY_MOVE(CppIndentationScope) |
68 | }; |
69 | |
70 | // appends string \a what with extra indentation \a extraIndent to current |
71 | // header string |
72 | template<typename String> |
73 | void (const String &what, int = 0) |
74 | { |
75 | rawAppend(m_code.header, what, headerIndent + extraIndent); |
76 | } |
77 | |
78 | // appends string \a what with extra indentation \a extraIndent to current |
79 | // cpp string |
80 | template<typename String> |
81 | void rawAppendToCpp(const String &what, int = 0) |
82 | { |
83 | rawAppend(m_code.cpp, what, cppIndent + extraIndent); |
84 | } |
85 | |
86 | // special case of rawAppendToCpp that makes sure that string "foo()" |
87 | // becomes "MyClass::foo()" |
88 | template<typename String> |
89 | void rawAppendSignatureToCpp(const String &what, int = 0) |
90 | { |
91 | QString signatureScope; |
92 | for (const auto &scope : memberScopes) |
93 | signatureScope += scope + u"::" ; |
94 | rawAppendToCpp(signatureScope + what, extraIndent); |
95 | } |
96 | }; |
97 | |
98 | QT_END_NAMESPACE |
99 | |
100 | #endif // QMLTCOUTPUTPRIMITIVES_H |
101 | |