| 1 | // Copyright (C) 2018 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 LANGUAGE_H |
| 5 | #define LANGUAGE_H |
| 6 | |
| 7 | #include <QtCore/qstring.h> |
| 8 | #include <QtCore/qstringview.h> |
| 9 | |
| 10 | QT_FORWARD_DECLARE_CLASS(QTextStream) |
| 11 | |
| 12 | enum class Language { Cpp, Python }; |
| 13 | |
| 14 | enum class ConnectionSyntax { StringBased, MemberFunctionPtr }; |
| 15 | |
| 16 | namespace language { |
| 17 | |
| 18 | Language language(); |
| 19 | void setLanguage(Language); |
| 20 | |
| 21 | ConnectionSyntax connectionSyntax(); |
| 22 | void setConnectionSyntax(ConnectionSyntax cs); |
| 23 | |
| 24 | extern QString derefPointer; |
| 25 | extern char listStart; |
| 26 | extern char listEnd; |
| 27 | extern QString nullPtr; |
| 28 | extern QString operatorNew; |
| 29 | extern QString qtQualifier; |
| 30 | extern QString qualifier; |
| 31 | extern QString self; |
| 32 | extern QString eol; |
| 33 | extern QString emptyString; |
| 34 | |
| 35 | extern QString cppQualifier; |
| 36 | extern QString cppTrue; |
| 37 | extern QString cppFalse; |
| 38 | |
| 39 | // Base class for streamable objects with one QStringView parameter |
| 40 | class StringViewStreamable |
| 41 | { |
| 42 | public: |
| 43 | StringViewStreamable(QStringView parameter) : m_parameter(parameter) {} |
| 44 | |
| 45 | QStringView parameter() const { return m_parameter; } |
| 46 | |
| 47 | private: |
| 48 | QStringView m_parameter; |
| 49 | }; |
| 50 | |
| 51 | class qtConfig : public StringViewStreamable |
| 52 | { |
| 53 | public: |
| 54 | qtConfig(QStringView name) : StringViewStreamable(name) {} |
| 55 | }; |
| 56 | |
| 57 | QTextStream &operator<<(QTextStream &str, const qtConfig &c); |
| 58 | |
| 59 | class openQtConfig : public StringViewStreamable |
| 60 | { |
| 61 | public: |
| 62 | openQtConfig(QStringView name) : StringViewStreamable(name) {} |
| 63 | }; |
| 64 | |
| 65 | QTextStream &operator<<(QTextStream &str, const openQtConfig &c); |
| 66 | |
| 67 | class closeQtConfig : public StringViewStreamable |
| 68 | { |
| 69 | public: |
| 70 | closeQtConfig(QStringView name) : StringViewStreamable(name) {} |
| 71 | }; |
| 72 | |
| 73 | QTextStream &operator<<(QTextStream &, const closeQtConfig &c); |
| 74 | |
| 75 | QString fixClassName(QString className); |
| 76 | |
| 77 | QLatin1StringView toolbarArea(int v); |
| 78 | QLatin1StringView sizePolicy(int v); |
| 79 | QLatin1StringView dockWidgetArea(int v); |
| 80 | QLatin1StringView paletteColorRole(int v); |
| 81 | |
| 82 | enum class Encoding { Utf8, Unicode }; |
| 83 | |
| 84 | void _formatString(QTextStream &str, const QString &value, const QString &indent, |
| 85 | bool qString); |
| 86 | |
| 87 | template <bool AsQString> |
| 88 | class _string |
| 89 | { |
| 90 | public: |
| 91 | explicit _string(const QString &value, const QString &indent = QString()) |
| 92 | : m_value(value), m_indent(indent) {} |
| 93 | |
| 94 | void format(QTextStream &str) const |
| 95 | { _formatString(str, value: m_value, indent: m_indent, qString: AsQString); } |
| 96 | |
| 97 | private: |
| 98 | const QString &m_value; |
| 99 | const QString &m_indent; |
| 100 | }; |
| 101 | |
| 102 | template <bool AsQString> |
| 103 | inline QTextStream &operator<<(QTextStream &str, const language::_string<AsQString> &s) |
| 104 | { |
| 105 | s.format(str); |
| 106 | return str; |
| 107 | } |
| 108 | |
| 109 | using charliteral = _string<false>; |
| 110 | using qstring = _string<true>; |
| 111 | |
| 112 | class repeat { |
| 113 | public: |
| 114 | explicit repeat(int count, char c) : m_count(count), m_char(c) {} |
| 115 | |
| 116 | friend QTextStream &operator<<(QTextStream &str, const repeat &r); |
| 117 | |
| 118 | private: |
| 119 | const int m_count; |
| 120 | const char m_char; |
| 121 | }; |
| 122 | |
| 123 | class startFunctionDefinition1 { |
| 124 | public: |
| 125 | explicit startFunctionDefinition1(const char *name, const QString ¶meterType, |
| 126 | const QString ¶meterName, |
| 127 | const QString &indent, |
| 128 | const char *returnType = nullptr); |
| 129 | |
| 130 | friend QTextStream &operator<<(QTextStream &str, const startFunctionDefinition1 &f); |
| 131 | private: |
| 132 | const char *m_name; |
| 133 | const QString &m_parameterType; |
| 134 | const QString &m_parameterName; |
| 135 | const QString &m_indent; |
| 136 | const char *m_return; |
| 137 | }; |
| 138 | |
| 139 | class endFunctionDefinition { |
| 140 | public: |
| 141 | explicit endFunctionDefinition(const char *name); |
| 142 | |
| 143 | friend QTextStream &operator<<(QTextStream &str, const endFunctionDefinition &f); |
| 144 | private: |
| 145 | const char *m_name; |
| 146 | }; |
| 147 | |
| 148 | void _formatStackVariable(QTextStream &str, const char *className, QStringView varName, bool withInitParameters); |
| 149 | |
| 150 | template <bool withInitParameters> |
| 151 | class _stackVariable { |
| 152 | public: |
| 153 | explicit _stackVariable(const char *className, QStringView varName) : |
| 154 | m_className(className), m_varName(varName) {} |
| 155 | |
| 156 | void format(QTextStream &str) const |
| 157 | { _formatStackVariable(str, className: m_className, varName: m_varName, withInitParameters); } |
| 158 | |
| 159 | private: |
| 160 | const char *m_className; |
| 161 | QStringView m_varName; |
| 162 | QStringView m_parameters; |
| 163 | }; |
| 164 | |
| 165 | template <bool withInitParameters> |
| 166 | inline QTextStream &operator<<(QTextStream &str, const _stackVariable<withInitParameters> &s) |
| 167 | { |
| 168 | s.format(str); |
| 169 | return str; |
| 170 | } |
| 171 | |
| 172 | using stackVariable = _stackVariable<false>; |
| 173 | using stackVariableWithInitParameters = _stackVariable<true>; |
| 174 | |
| 175 | enum class SignalSlotOption |
| 176 | { |
| 177 | Ambiguous = 0x1 |
| 178 | }; |
| 179 | |
| 180 | Q_DECLARE_FLAGS(SignalSlotOptions, SignalSlotOption) |
| 181 | |
| 182 | struct SignalSlot |
| 183 | { |
| 184 | QString name; |
| 185 | QString signature; |
| 186 | QString className; |
| 187 | SignalSlotOptions options; |
| 188 | }; |
| 189 | |
| 190 | void formatConnection(QTextStream &str, const SignalSlot &sender, const SignalSlot &receiver, |
| 191 | ConnectionSyntax connectionSyntax); |
| 192 | |
| 193 | QString boolValue(bool v); |
| 194 | |
| 195 | QString enumValue(const QString &value); |
| 196 | |
| 197 | } // namespace language |
| 198 | |
| 199 | #endif // LANGUAGE_H |
| 200 | |