Akonadi

nodetree.cpp
1/*
2 SPDX-FileCopyrightText: 2017 Daniel Vrátil <dvratil@kde.og>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "nodetree.h"
8#include "cpphelper.h"
9#include "typehelper.h"
10
11namespace
12{
13
14QString qualifiedName(const Node *node)
15{
16 if (node->type() == Node::Class) {
17 return static_cast<const ClassNode *>(node)->className();
18 }
19 if (node->type() == Node::Enum) {
20 auto enumNode = static_cast<const EnumNode *>(node);
21 if (enumNode->enumType() == EnumNode::TypeFlag) {
22 return QStringLiteral("%1::%2").arg(qualifiedName(node->parent()), enumNode->flagsName());
23 }
24 return QStringLiteral("%1::%2").arg(qualifiedName(node->parent()), enumNode->name());
25 }
26
27 Q_ASSERT_X(false, "qualifiedName", "Invalid node type");
28 return {};
29}
30
31} // namespace
32
33Node::Node(NodeType type, Node *parent)
34 : mParent(parent)
35 , mType(type)
36{
37 if (parent) {
38 parent->appendNode(this);
39 }
40}
41
42Node::~Node()
43{
44 qDeleteAll(mChildren);
45}
46
47Node::NodeType Node::type() const
48{
49 return mType;
50}
51
52Node *Node::parent() const
53{
54 return mParent;
55}
56
57void Node::appendNode(Node *child)
58{
59 child->mParent = this;
60 mChildren.push_back(child);
61}
62
63const QList<Node const *> &Node::children() const
64{
65 return mChildren;
66}
67
68DocumentNode::DocumentNode(int version)
69 : Node(Document, nullptr)
70 , mVersion(version)
71{
72}
73
74int DocumentNode::version() const
75{
76 return mVersion;
77}
78
79ClassNode::ClassNode(const QString &name, ClassType type, DocumentNode *parent)
80 : Node(Node::Class, parent)
81 , mName(name)
82 , mClassType(type)
83{
84}
85
86QString ClassNode::name() const
87{
88 return mName;
89}
90
91ClassNode::ClassType ClassNode::classType() const
92{
93 return mClassType;
94}
95
96QString ClassNode::className() const
97{
98 switch (mClassType) {
99 case Class:
100 return mName;
101 case Command:
102 return mName + QStringLiteral("Command");
103 case Response:
104 return mName + QStringLiteral("Response");
105 case Notification:
106 return mName + QStringLiteral("Notification");
107 default:
108 Q_ASSERT(false);
109 return QString();
110 }
111}
112
113QString ClassNode::parentClassName() const
114{
115 switch (mClassType) {
116 case Class:
117 return QString();
118 case Command:
119 return QStringLiteral("Command");
120 case Response:
121 return QStringLiteral("Response");
122 case Notification:
123 return QStringLiteral("ChangeNotification");
124 case Invalid:
125 Q_ASSERT(false);
126 return QString();
127 }
128 Q_UNREACHABLE();
129}
130
131ClassNode::ClassType ClassNode::elementNameToType(QStringView name)
132{
133 if (name == QLatin1StringView("class")) {
134 return Class;
135 } else if (name == QLatin1StringView("command")) {
136 return Command;
137 } else if (name == QLatin1StringView("response")) {
138 return Response;
139 } else if (name == QLatin1StringView("notification")) {
140 return Notification;
141 } else {
142 return Invalid;
143 }
144}
145
146QList<PropertyNode const *> ClassNode::properties() const
147{
149 for (const auto node : std::as_const(mChildren)) {
150 if (node->type() == Node::Property) {
151 rv << static_cast<PropertyNode const *>(node);
152 }
153 }
154 CppHelper::sortMembers(rv);
155 return rv;
156}
157
158CtorNode::CtorNode(const QList<Argument> &args, ClassNode *parent)
159 : Node(Ctor, parent)
160 , mArgs(args)
161{
162}
163
164CtorNode::~CtorNode()
165{
166}
167
168QList<CtorNode::Argument> CtorNode::arguments() const
169{
170 return mArgs;
171}
172
173void CtorNode::setArgumentType(const QString &name, const QString &type)
174{
175 for (auto &arg : mArgs) {
176 if (arg.name == name) {
177 arg.type = type;
178 break;
179 }
180 }
181}
182
183EnumNode::EnumNode(const QString &name, EnumType type, ClassNode *parent)
184 : Node(Enum, parent)
185 , mName(name)
186 , mEnumType(type)
187{
188}
189
190QString EnumNode::name() const
191{
192 return mName;
193}
194
195EnumNode::EnumType EnumNode::enumType() const
196{
197 return mEnumType;
198}
199
200QString EnumNode::flagsName() const
201{
202 if (mEnumType == TypeFlag) {
203 return mName + QStringLiteral("s");
204 }
205
206 return {};
207}
208
209EnumNode::EnumType EnumNode::elementNameToType(QStringView name)
210{
211 if (name == QLatin1StringView("enum")) {
212 return TypeEnum;
213 } else if (name == QLatin1StringView("flag")) {
214 return TypeFlag;
215 } else {
216 return TypeInvalid;
217 }
218}
219
220EnumValueNode::EnumValueNode(const QString &name, EnumNode *parent)
221 : Node(EnumValue, parent)
222 , mName(name)
223 , mValue()
224{
225}
226
227QString EnumValueNode::name() const
228{
229 return mName;
230}
231
232void EnumValueNode::setValue(const QString &value)
233{
234 mValue = value;
235}
236
237QString EnumValueNode::value() const
238{
239 return mValue;
240}
241
242PropertyNode::PropertyNode(const QString &name, const QString &type, ClassNode *parent)
243 : Node(Property, parent)
244 , mName(name)
245 , mType(type)
246 , mSetter(nullptr)
247 , mReadOnly(false)
248 , mAsReference(false)
249{
250}
251
252PropertyNode::~PropertyNode()
253{
254 delete mSetter;
255}
256
257QString PropertyNode::type() const
258{
259 return mType;
260}
261
262QString PropertyNode::name() const
263{
264 return mName;
265}
266
267void PropertyNode::setDefaultValue(const QString &defaultValue)
268{
269 mDefaultValue = defaultValue;
270}
271
272QString PropertyNode::defaultValue() const
273{
274 return mDefaultValue;
275}
276
277bool PropertyNode::readOnly() const
278{
279 return mReadOnly;
280}
281
282void PropertyNode::setReadOnly(bool readOnly)
283{
284 mReadOnly = readOnly;
285}
286
287bool PropertyNode::asReference() const
288{
289 return mAsReference;
290}
291
292void PropertyNode::setAsReference(bool asReference)
293{
294 mAsReference = asReference;
295}
296
297bool PropertyNode::isPointer() const
298{
299 return TypeHelper::isPointerType(mType);
300}
301
302bool PropertyNode::isEnum() const
303{
304 auto parentClass = static_cast<ClassNode *>(parent());
305 for (const auto node : parentClass->children()) {
306 if (node->type() == Node::Enum) {
307 const auto enumNode = static_cast<const EnumNode *>(node);
308 if (qualifiedName(enumNode) == mType) {
309 return true;
310 }
311 }
312 }
313
314 return false;
315}
316
317QMultiMap<QString, QString> PropertyNode::dependencies() const
318{
319 return mDepends;
320}
321
322void PropertyNode::addDependency(const QString &enumVar, const QString &enumValue)
323{
324 mDepends.insert(enumVar, enumValue);
325}
326
327void PropertyNode::setSetter(Setter *setter)
328{
329 mSetter = setter;
330}
331
332PropertyNode::Setter *PropertyNode::setter() const
333{
334 return mSetter;
335}
336
337QString PropertyNode::mVariableName() const
338{
339 return QStringLiteral("m") + mName[0].toUpper() + QStringView(mName).mid(1);
340}
341
342QString PropertyNode::setterName() const
343{
344 return QStringLiteral("set") + mName[0].toUpper() + QStringView(mName).mid(1);
345}
virtual QString type() const
Definition nodetree.cpp:47
QString name() const
KDB_EXPORT KDbVersionInfo version()
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
QString name(StandardAction id)
void push_back(parameter_type value)
iterator insert(const Key &key, const T &value)
QString toUpper() const const
QStringView mid(qsizetype start, qsizetype length) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.