Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmltcoutputprimitives_p.h
Go to the documentation of this file.
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// Qt-Security score:significant
4
5#ifndef QQMLTCOUTPUTPRIMITIVES_P_H
6#define QQMLTCOUTPUTPRIMITIVES_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qstack.h>
20#include <QtCore/qstring.h>
21#include <QtCore/qstringbuilder.h>
22
23QT_BEGIN_NAMESPACE
24
25namespace QQmltc {
26
32
34{
35 Output &m_code;
36
37 template<typename String>
38 static void rawAppend(QString &out, const String &what, int extraIndent = 0)
39 {
40 constexpr char16_t newLine[] = u"\n";
41 out += QString(extraIndent * 4, u' ') + what + newLine;
42 }
43
44public:
45 OutputWrapper(Output &code) : m_code(code) { }
46 const Output &code() const { return m_code; }
47
48 QStack<QString> memberScopes; // member name scopes e.g. MyClass::MySubclass::
49 int headerIndent = 0; // header indentation level
50 int cppIndent = 0; // cpp indentation level
51
52 // manages current scope of the generated code, which is necessary for
53 // cpp file generation. Example:
54 // class MyClass { MyClass(); }; - in header
55 // MyClass::MyClass() {} - in cpp
56 // MemberNameScope makes sure "MyClass::" is recorded
58 {
60 MemberNameScope(OutputWrapper *code, const QString &str) : m_code(code)
61 {
62 m_code->memberScopes.push(str);
63 }
64 ~MemberNameScope() { m_code->memberScopes.pop(); }
66 };
67
75
83
84 // appends string \a what with extra indentation \a extraIndent to current
85 // header string
86 template<typename String>
87 void rawAppendToHeader(const String &what, int extraIndent = 0)
88 {
89 rawAppend(m_code.header, what, headerIndent + extraIndent);
90 }
91
92 // appends string \a what with extra indentation \a extraIndent to current
93 // cpp string
94 template<typename String>
95 void rawAppendToCpp(const String &what, int extraIndent = 0)
96 {
97 rawAppend(m_code.cpp, what, cppIndent + extraIndent);
98 }
99
100 // special case of rawAppendToCpp that makes sure that string "foo()"
101 // becomes "MyClass::foo()"
102 template<typename String>
103 void rawAppendSignatureToCpp(const String &what, int extraIndent = 0)
104 {
105 QString signatureScope;
106 for (const auto &scope : std::as_const(memberScopes))
107 signatureScope += scope + u"::";
108 rawAppendToCpp(signatureScope + what, extraIndent);
109 }
110};
111
112} // namespace QQmltc
113
114QT_END_NAMESPACE
115
116#endif // QQMLTCOUTPUTPRIMITIVES_P_H
void rawAppendToHeader(const String &what, int extraIndent=0)
void rawAppendSignatureToCpp(const String &what, int extraIndent=0)
void rawAppendToCpp(const String &what, int extraIndent=0)
const Output & code() const
MemberNameScope(OutputWrapper *code, const QString &str)