1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #ifndef LANGUAGE_H
30 #define LANGUAGE_H
31 
32 #include <QtCore/qstring.h>
33 #include <QtCore/qstringview.h>
34 #include <QtCore/qstring.h>
35 
36 QT_FORWARD_DECLARE_CLASS(QTextStream)
37 
38 enum class Language { Cpp, Python };
39 
40 namespace language {
41 
42 Language language();
43 void setLanguage(Language);
44 
45 extern QString derefPointer;
46 extern QString nullPtr;
47 extern QString operatorNew;
48 extern QString qtQualifier;
49 extern QString qualifier;
50 extern QString self;
51 extern QString eol;
52 extern QString emptyString;
53 
54 extern QString cppQualifier;
55 extern QString cppTrue;
56 extern QString cppFalse;
57 
58 // Base class for streamable objects with one QStringView parameter
59 class StringViewStreamable
60 {
61 public:
StringViewStreamable(QStringView parameter)62     StringViewStreamable(QStringView parameter) : m_parameter(parameter) {}
63 
parameter()64     QStringView parameter() const { return m_parameter; }
65 
66 private:
67     QStringView m_parameter;
68 };
69 
70 class qtConfig : public StringViewStreamable
71 {
72 public:
qtConfig(QStringView name)73     qtConfig(QStringView name) : StringViewStreamable(name) {}
74 };
75 
76 QTextStream &operator<<(QTextStream &str, const qtConfig &c);
77 
78 class openQtConfig : public StringViewStreamable
79 {
80 public:
openQtConfig(QStringView name)81     openQtConfig(QStringView name) : StringViewStreamable(name) {}
82 };
83 
84 QTextStream &operator<<(QTextStream &str, const openQtConfig &c);
85 
86 class closeQtConfig : public StringViewStreamable
87 {
88 public:
closeQtConfig(QStringView name)89     closeQtConfig(QStringView name) : StringViewStreamable(name) {}
90 };
91 
92 QTextStream &operator<<(QTextStream &, const closeQtConfig &c);
93 
94 QString fixClassName(QString className);
95 
96 const char *toolbarArea(int v);
97 const char *sizePolicy(int v);
98 const char *dockWidgetArea(int v);
99 const char *paletteColorRole(int v);
100 
101 enum class Encoding { Utf8, Unicode };
102 
103 void _formatString(QTextStream &str, const QString &value, const QString &indent,
104                    bool qString);
105 
106 template <bool AsQString>
107 class _string
108 {
109 public:
110     explicit _string(const QString &value, const QString &indent = QString())
m_value(value)111         : m_value(value), m_indent(indent) {}
112 
format(QTextStream & str)113     void format(QTextStream &str) const
114     { _formatString(str, m_value, m_indent, AsQString); }
115 
116 private:
117     const QString &m_value;
118     const QString &m_indent;
119 };
120 
121 template <bool AsQString>
122 inline QTextStream &operator<<(QTextStream &str, const language::_string<AsQString> &s)
123 {
124     s.format(str);
125     return str;
126 }
127 
128 using charliteral = _string<false>;
129 using qstring = _string<true>;
130 
131 class repeat {
132 public:
repeat(int count,char c)133     explicit repeat(int count, char c) : m_count(count), m_char(c) {}
134 
135     friend QTextStream &operator<<(QTextStream &str, const repeat &r);
136 
137 private:
138     const int m_count;
139     const char m_char;
140 };
141 
142 class startFunctionDefinition1 {
143 public:
144     explicit startFunctionDefinition1(const char *name, const QString &parameterType,
145                                       const QString &parameterName,
146                                       const QString &indent,
147                                       const char *returnType = nullptr);
148 
149     friend QTextStream &operator<<(QTextStream &str, const startFunctionDefinition1 &f);
150 private:
151     const char *m_name;
152     const QString &m_parameterType;
153     const QString &m_parameterName;
154     const QString &m_indent;
155     const char *m_return;
156 };
157 
158 class endFunctionDefinition {
159 public:
160     explicit endFunctionDefinition(const char *name);
161 
162     friend QTextStream &operator<<(QTextStream &str, const endFunctionDefinition &f);
163 private:
164     const char *m_name;
165 };
166 
167 void _formatStackVariable(QTextStream &str, const char *className, QStringView varName, bool withInitParameters);
168 
169 template <bool withInitParameters>
170 class _stackVariable {
171 public:
_stackVariable(const char * className,QStringView varName)172     explicit _stackVariable(const char *className, QStringView varName) :
173         m_className(className), m_varName(varName) {}
174 
format(QTextStream & str)175     void format(QTextStream &str) const
176     { _formatStackVariable(str, m_className, m_varName, withInitParameters); }
177 
178 private:
179     const char *m_className;
180     QStringView m_varName;
181     QStringView m_parameters;
182 };
183 
184 template <bool withInitParameters>
185 inline QTextStream &operator<<(QTextStream &str, const _stackVariable<withInitParameters> &s)
186 {
187     s.format(str);
188     return str;
189 }
190 
191 using stackVariable = _stackVariable<false>;
192 using stackVariableWithInitParameters = _stackVariable<true>;
193 
194 struct SignalSlot
195 {
196     QString name;
197     QString signature;
198     QString className;
199 };
200 
201 void formatConnection(QTextStream &str, const SignalSlot &sender, const SignalSlot &receiver);
202 
203 QString boolValue(bool v);
204 
205 QString enumValue(const QString &value);
206 
207 } // namespace language
208 
209 #endif // LANGUAGE_H
210