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 PARAMETERS_H |
5 | #define PARAMETERS_H |
6 | |
7 | #include <QtCore/qlist.h> |
8 | #include <QtCore/qregularexpression.h> |
9 | #include <QtCore/qset.h> |
10 | |
11 | #include <utility> |
12 | |
13 | QT_BEGIN_NAMESPACE |
14 | |
15 | class Location; |
16 | class Tokenizer; |
17 | class CodeChunk; |
18 | |
19 | class Parameter |
20 | { |
21 | public: |
22 | Parameter() = default; |
23 | explicit Parameter(QString type, QString name = QString(), QString defaultValue = QString()) |
24 | : m_type(std::move(type)), m_name(std::move(name)), m_defaultValue(std::move(defaultValue)) |
25 | { |
26 | } |
27 | |
28 | void setName(const QString &name) { m_name = name; } |
29 | [[nodiscard]] bool hasType() const { return !m_type.isEmpty(); } |
30 | [[nodiscard]] const QString &type() const { return m_type; } |
31 | [[nodiscard]] const QString &name() const { return m_name; } |
32 | [[nodiscard]] const QString &defaultValue() const { return m_defaultValue; } |
33 | void setDefaultValue(const QString &t) { m_defaultValue = t; } |
34 | |
35 | void set(const QString &type, const QString &name, const QString &defaultValue = QString()) |
36 | { |
37 | m_type = type; |
38 | m_name = name; |
39 | m_defaultValue = defaultValue; |
40 | } |
41 | |
42 | [[nodiscard]] QString signature(bool includeValue = false) const; |
43 | |
44 | [[nodiscard]] const QString &canonicalType() const { return m_canonicalType; } |
45 | void setCanonicalType(const QString &t) { m_canonicalType = t; } |
46 | |
47 | public: |
48 | QString m_canonicalType {}; |
49 | QString m_type {}; |
50 | QString m_name {}; |
51 | QString m_defaultValue {}; |
52 | }; |
53 | |
54 | typedef QList<Parameter> ParameterVector; |
55 | |
56 | class Parameters |
57 | { |
58 | public: |
59 | Parameters(); |
60 | Parameters(const QString &signature); // TODO: Making this explicit breaks QDoc |
61 | |
62 | void clear() |
63 | { |
64 | m_parameters.clear(); |
65 | m_privateSignal = false; |
66 | m_valid = true; |
67 | } |
68 | [[nodiscard]] const ParameterVector ¶meters() const { return m_parameters; } |
69 | [[nodiscard]] bool isPrivateSignal() const { return m_privateSignal; } |
70 | [[nodiscard]] bool isEmpty() const { return m_parameters.isEmpty(); } |
71 | [[nodiscard]] bool isValid() const { return m_valid; } |
72 | [[nodiscard]] int count() const { return m_parameters.size(); } |
73 | void reserve(int count) { m_parameters.reserve(asize: count); } |
74 | [[nodiscard]] const Parameter &at(int i) const { return m_parameters.at(i); } |
75 | Parameter &last() { return m_parameters.last(); } |
76 | [[nodiscard]] const Parameter &last() const { return m_parameters.last(); } |
77 | inline Parameter &operator[](int index) { return m_parameters[index]; } |
78 | void append(const QString &type, const QString &name, const QString &value); |
79 | void append(const QString &type, const QString &name) { append(type, name, value: QString()); } |
80 | void append(const QString &type) { append(type, name: QString(), value: QString()); } |
81 | void pop_back() { m_parameters.pop_back(); } |
82 | void setPrivateSignal() { m_privateSignal = true; } |
83 | [[nodiscard]] QString signature(bool includeValues = false) const; |
84 | [[nodiscard]] QString rawSignature(bool names = false, bool values = false) const; |
85 | void set(const QString &signature); |
86 | [[nodiscard]] QSet<QString> getNames() const; |
87 | [[nodiscard]] QString generateTypeList() const; |
88 | [[nodiscard]] QString generateTypeAndNameList() const; |
89 | [[nodiscard]] bool match(const Parameters ¶meters) const; |
90 | |
91 | private: |
92 | void readToken(); |
93 | QString lexeme(); |
94 | QString previousLexeme(); |
95 | bool match(int target); |
96 | void matchTemplateAngles(CodeChunk &type); |
97 | bool matchTypeAndName(CodeChunk &type, QString &name); |
98 | bool matchParameter(); |
99 | bool parse(const QString &signature); |
100 | |
101 | private: |
102 | static QRegularExpression ; |
103 | |
104 | bool m_valid {}; |
105 | bool m_privateSignal {}; |
106 | int m_tok {}; |
107 | Tokenizer *m_tokenizer { nullptr }; |
108 | ParameterVector m_parameters; |
109 | }; |
110 | |
111 | QT_END_NAMESPACE |
112 | |
113 | #endif |
114 | |