5#include <QtCore/QDebug>
6#include <QtCore/QTextStream>
20
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
113void Base::
dump(
const Sink &sink,
const QString &name,
bool hasSquareBrackets)
const {
114 if (hasSquareBrackets)
117 if (hasSquareBrackets)
123 int k1 =
static_cast<
int>(p1
.kind());
124 int k2 =
static_cast<
int>(p2
.kind());
133 const auto a =
std::get<Field>(p1.m_data).fieldName;
134 const auto b =
std::get<Field>(p2.m_data).fieldName;
137 return size_t(a) < size_t(b) ? -1 : 1;
140 if (
std::get<Index>(p1.m_data).indexValue <
std::get<Index>(p2.m_data).indexValue)
142 if (
std::get<Index>(p1.m_data).indexValue >
std::get<Index>(p2.m_data).indexValue)
146 return std::get<Key>(p1.m_data).keyValue.compare(
std::get<Key>(p2.m_data).keyValue);
149 PathRoot k1 =
std::get<Root>(p1.m_data).contextKind;
150 PathRoot k2 =
std::get<Root>(p2.m_data).contextKind;
155 return int(k1) -
int(k2);
158 return int(
std::get<Current>(p1.m_data).contextKind)
159 -
int(
std::get<Current>(p2.m_data).contextKind);
161 Q_ASSERT(
false &&
"unexpected PathComponent in PathComponent::cmp");
167 QtPrivate::QHashCombineWithSeed combine(seed);
168 const size_t kindHash = combine(seed,
int(
kind()));
173 return combine(kindHash,
int(
std::get<Field>(m_data).fieldName));
175 return combine(kindHash,
std::get<Index>(m_data).indexValue);
177 return combine(kindHash,
std::get<Key>(m_data).keyValue);
183 return combine(kindHash,
int(r));
186 return combine(kindHash,
int(
std::get<Current>(m_data).contextKind));
188 Q_UNREACHABLE_RETURN(kindHash);
199 const PathNode *node = parent.take();
200 while (node && !node->ref.deref()) {
210
211
212
213Path::NodeArray
Path::nodes()
const
215 NodeArray result(m_length);
216 const Node *node = m_lastNode.data();
217 for (qsizetype i = m_length; i > 0; node = node->parent.data())
223
224
225
231 if (i >= m_length || i < 0) {
232 Q_ASSERT(
false &&
"index out of bounds");
233 return emptyComponent;
235 const Node *node = m_lastNode.data();
236 for (
int toSkip = m_length - 1 - i; toSkip > 0; --toSkip)
237 node = node->parent.data();
238 return node->component;
263 auto &comp = component(0);
271 auto &comp = component(0);
279 auto &comp = component(0);
294 return component(0).name();
299 auto &comp = component(0);
301 return i->indexValue;
312 return mid(m_length-1, 1);
316
317
318
319
320
321
322
323
324
325
328 const NodeArray nodes =
this->nodes();
339 return Source{
Path(), *
this};
352Path Path::fromKey(QStringView s) {
return fromKey(s.toString()); }
357 Q_ASSERT(m_length < std::numeric_limits<quint16>::max());
358 return Path(Node::Ptr(
new Node(std::move(c), m_lastNode)), m_length + 1);
369
370
371
372
373
374
380 for (
const Node *node : toAdd.nodes())
381 result = result.withComponent(node->component);
386
387
388
391 if (p1.m_lastNode == p2.m_lastNode && p1.m_length == p2.m_length)
393 const Node *n1 = p1.m_lastNode.data();
394 const Node *n2 = p2.m_lastNode.data();
396 for (
int l = p1.m_length; l > p2.m_length; --l)
397 n1 = n1->parent.data();
398 for (
int l = p2.m_length; l > p1.m_length; --l)
399 n2 = n2->parent.data();
403 for (
int l = qMin(p1.m_length, p2.m_length); l > 0 && n1 != n2; --l) {
406 n1 = n1->parent.data();
407 n2 = n2->parent.data();
411 return p1.m_length - p2.m_length;
416 const NodeArray nodes =
this->nodes();
418 for (
const Node *node : nodes) {
419 const Component &c = node->component;
420 if (!c.hasSquareBrackets()) {
421 if (!first || (c.kind() != Kind::Root && c.kind() != Kind::Current))
432 QTextStream stream(&res);
433 dump([&stream](QStringView str){ stream << str; });
440 if (m_length > n && n >= 0)
441 return Path(m_lastNode, m_length - n);
447 if (m_length > n && n >= 0) {
448 const Node *node = m_lastNode.data();
449 for (
int i = 0; i < n; ++i)
450 node = node->parent.data();
451 return Path(Node::Ptr(node), m_length - n);
458 length = qMin(m_length - offset, length);
459 if (offset < 0 || offset >= m_length || length <= 0 || length > m_length)
461 return dropTail(m_length - offset - length).dropFront(offset);
466 return mid(offset, m_length - offset);
471 QtPrivate::QHashCombineWithSeed combine(seed);
472 size_t hash = combine(seed, path
.length());
473 const Path::Node *node = path.m_lastNode.data();
474 for (
int i = path
.length(); i > 0; --i, node = node->parent.data())
475 hash = combine(hash, node->component.hash(seed));
Represents an error message connected to the dom.
void dump(const Sink &sink, const QString &name, bool hasSquareBrackets) const
const Index * asIndex() const
static int cmp(const PathComponent &p1, const PathComponent &p2)
size_t hash(size_t seed) const
const Field * asField() const
const Current * asCurrent() const
const Root * asRoot() const
PathIterator(const Path &path)
Represents an immutable JsonPath like path in the Qml code model (from a DomItem to another DomItem).
std::optional< index_type > headIndex() const
Path dropTail(int n=1) const
static Path fromComponent(Component c)
Path withIndex(index_type i) const
static int cmp(const Path &p1, const Path &p2)
Compares p1 and p2 lexicographically.
static Path fromField(FieldName name)
Path withKey(QStringView name) const
Source split() const
Splits the path at the last field or current component, or right after a root anchor.
PathIterator begin() const
static Path fromRoot(PathRoot r)
Path operator[](int i) const
Path withField(FieldName name) const
Path withKey(const QString &name) const
Path mid(int offset, int length) const
Path withCurrent(PathCurrent s) const
void dump(const Sink &sink) const
Path mid(int offset) const
Path withPath(const Path &toAdd) const
Returns a copy of this with toAdd appended to it.
std::optional< FieldName > headField() const
std::optional< PathRoot > headRoot() const
Path dropFront(int n=1) const
Path withComponent(PathEls::PathComponent c) const
PathEls::PathComponent Component
static Path fromCurrent(PathCurrent c)
std::optional< PathCurrent > headCurrent() const
QMLDOM_EXPORT size_t qHash(const Path &, size_t seed=0)