| 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 PROPERTYNODE_H |
| 5 | #define PROPERTYNODE_H |
| 6 | |
| 7 | #include "functionnode.h" |
| 8 | #include "node.h" |
| 9 | |
| 10 | #include <QtCore/qglobal.h> |
| 11 | #include <QtCore/qstring.h> |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | class Aggregate; |
| 16 | |
| 17 | class PropertyNode : public Node |
| 18 | { |
| 19 | public: |
| 20 | enum class PropertyType { StandardProperty, BindableProperty }; |
| 21 | enum class FunctionRole { Getter, Setter, Resetter, Notifier, Bindable, NumFunctionRoles }; |
| 22 | static QString roleName(FunctionRole role); |
| 23 | |
| 24 | PropertyNode(Aggregate *parent, const QString &name); |
| 25 | |
| 26 | void setDataType(const QString &dataType) override { m_type = dataType; } |
| 27 | void addFunction(FunctionNode *function, FunctionRole role); |
| 28 | void addSignal(FunctionNode *function, FunctionRole role); |
| 29 | void setStored(bool stored) { m_stored = toFlagValue(b: stored); } |
| 30 | void setWritable(bool writable) { m_writable = toFlagValue(b: writable); } |
| 31 | void setOverriddenFrom(const PropertyNode *baseProperty); |
| 32 | void setConstant() { m_const = true; } |
| 33 | void setRequired() { m_required = true; } |
| 34 | void setPropertyType(PropertyType type) { m_propertyType = type; } |
| 35 | |
| 36 | [[nodiscard]] const QString &dataType() const { return m_type; } |
| 37 | [[nodiscard]] QString qualifiedDataType() const; |
| 38 | [[nodiscard]] NodeList functions() const; |
| 39 | [[nodiscard]] const NodeList &functions(FunctionRole role) const |
| 40 | { |
| 41 | return m_functions[(int)role]; |
| 42 | } |
| 43 | [[nodiscard]] const NodeList &getters() const { return functions(role: FunctionRole::Getter); } |
| 44 | [[nodiscard]] const NodeList &setters() const { return functions(role: FunctionRole::Setter); } |
| 45 | [[nodiscard]] const NodeList &resetters() const { return functions(role: FunctionRole::Resetter); } |
| 46 | [[nodiscard]] const NodeList ¬ifiers() const { return functions(role: FunctionRole::Notifier); } |
| 47 | [[nodiscard]] bool hasAccessFunction(const QString &name) const; |
| 48 | FunctionRole role(const FunctionNode *functionNode) const; |
| 49 | [[nodiscard]] bool isStored() const { return fromFlagValue(fv: m_stored, defaultValue: storedDefault()); } |
| 50 | [[nodiscard]] bool isWritable() const { return fromFlagValue(fv: m_writable, defaultValue: writableDefault()); } |
| 51 | [[nodiscard]] bool isConstant() const { return m_const; } |
| 52 | [[nodiscard]] bool isRequired() const { return m_required; } |
| 53 | [[nodiscard]] PropertyType propertyType() const { return m_propertyType; } |
| 54 | [[nodiscard]] const PropertyNode *overriddenFrom() const { return m_overrides; } |
| 55 | |
| 56 | [[nodiscard]] bool storedDefault() const { return true; } |
| 57 | [[nodiscard]] bool writableDefault() const { return !setters().isEmpty(); } |
| 58 | |
| 59 | private: |
| 60 | QString m_type {}; |
| 61 | PropertyType m_propertyType { PropertyType::StandardProperty }; |
| 62 | NodeList m_functions[(qsizetype)FunctionRole::NumFunctionRoles] {}; |
| 63 | FlagValue m_stored { FlagValueDefault }; |
| 64 | FlagValue m_writable { FlagValueDefault }; |
| 65 | FlagValue m_user { FlagValueDefault }; |
| 66 | bool m_const { false }; |
| 67 | bool m_required { false }; |
| 68 | const PropertyNode *m_overrides { nullptr }; |
| 69 | }; |
| 70 | |
| 71 | inline void PropertyNode::addFunction(FunctionNode *function, FunctionRole role) |
| 72 | { |
| 73 | m_functions[(int)role].append(t: function); |
| 74 | function->addAssociatedProperty(property: this); |
| 75 | } |
| 76 | |
| 77 | inline void PropertyNode::addSignal(FunctionNode *function, FunctionRole role) |
| 78 | { |
| 79 | m_functions[(int)role].append(t: function); |
| 80 | function->addAssociatedProperty(property: this); |
| 81 | } |
| 82 | |
| 83 | inline NodeList PropertyNode::functions() const |
| 84 | { |
| 85 | NodeList list; |
| 86 | for (qsizetype i{0}; i < (qsizetype)FunctionRole::NumFunctionRoles; ++i) |
| 87 | list += m_functions[i]; |
| 88 | return list; |
| 89 | } |
| 90 | |
| 91 | QT_END_NAMESPACE |
| 92 | |
| 93 | #endif // PROPERTYNODE_H |
| 94 | |