1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
5#include "qqmldommock_p.h"
6#include "qqmldomitem_p.h"
7#include "qqmldomcomments_p.h"
8
9#include <QtCore/QBasicMutex>
10#include <QtCore/QMutexLocker>
11
12QT_BEGIN_NAMESPACE
13
14namespace QQmlJS {
15namespace Dom {
16
17MockObject MockObject::copy() const
18{
19 QMap<QString, MockObject> newObjs;
20 auto objs = subObjects;
21 auto itO = objs.cbegin();
22 auto endO = objs.cend();
23 while (itO != endO) {
24 newObjs.insert(key: itO.key(), value: itO->copy());
25 ++itO;
26 }
27 return MockObject(pathFromOwner(), newObjs, subValues);
28}
29
30std::pair<QString, MockObject> MockObject::asStringPair() const
31{
32 return std::make_pair(x: pathFromOwner().last().headName(), y: *this);
33}
34
35bool MockObject::iterateDirectSubpaths(DomItem &self, DirectVisitor visitor)
36{
37 static QHash<QString, QString> knownFields;
38 static QBasicMutex m;
39 auto toField = [](QString f) -> QStringView {
40 QMutexLocker l(&m);
41 if (!knownFields.contains(key: f))
42 knownFields[f] = f;
43 return knownFields[f];
44 };
45 bool cont = CommentableDomElement::iterateDirectSubpaths(self, visitor);
46 auto itV = subValues.begin();
47 auto endV = subValues.end();
48 while (itV != endV) {
49 cont = cont && self.dvValue(visitor, c: PathEls::Field(toField(itV.key())), value: *itV);
50 ++itV;
51 }
52 auto itO = subObjects.begin();
53 auto endO = subObjects.end();
54 while (itO != endO) {
55 cont = cont && self.dvItem(visitor, c: PathEls::Field(toField(itO.key())), it: [&self, &itO]() {
56 return self.copy(base: &(*itO));
57 });
58 ++itO;
59 }
60 return cont;
61}
62
63std::shared_ptr<OwningItem> MockOwner::doCopy(DomItem &) const
64{
65 return std::make_shared<MockOwner>(args: *this);
66}
67
68MockOwner::MockOwner(const MockOwner &o)
69 : OwningItem(o), pathFromTop(o.pathFromTop), subValues(o.subValues)
70{
71 auto objs = o.subObjects;
72 auto itO = objs.cbegin();
73 auto endO = objs.cend();
74 while (itO != endO) {
75 subObjects.insert(key: itO.key(), value: itO->copy());
76 ++itO;
77 }
78}
79
80std::shared_ptr<MockOwner> MockOwner::makeCopy(DomItem &self) const
81{
82 return std::static_pointer_cast<MockOwner>(r: doCopy(self));
83}
84
85Path MockOwner::canonicalPath(DomItem &) const
86{
87 return pathFromTop;
88}
89
90bool MockOwner::iterateDirectSubpaths(DomItem &self, DirectVisitor visitor)
91{
92 static QHash<QString, QString> knownFields;
93 static QBasicMutex m;
94 auto toField = [](QString f) -> QStringView {
95 QMutexLocker l(&m);
96 if (!knownFields.contains(key: f))
97 knownFields[f] = f;
98 return knownFields[f];
99 };
100 {
101 auto itV = subValues.begin();
102 auto endV = subValues.end();
103 while (itV != endV) {
104 if (!self.dvValue(visitor, c: PathEls::Field(toField(itV.key())), value: *itV))
105 return false;
106 ++itV;
107 }
108 }
109 {
110 auto itO = subObjects.begin();
111 auto endO = subObjects.end();
112 while (itO != endO) {
113 if (!self.dvItem(visitor, c: PathEls::Field(toField(itO.key())),
114 it: [&self, &itO]() { return self.copy(base: &(*itO)); }))
115 return false;
116 ++itO;
117 }
118 }
119 {
120 auto it = subMaps.begin();
121 auto end = subMaps.end();
122 while (it != end) {
123 if (!self.dvWrapField(visitor, f: toField(it.key()), obj&: it.value()))
124 return false;
125 ++it;
126 }
127 }
128 {
129 auto it = subMultiMaps.begin();
130 auto end = subMultiMaps.end();
131 while (it != end) {
132 if (!self.dvWrapField(visitor, f: toField(it.key()), obj&: it.value()))
133 return false;
134 ++it;
135 }
136 }
137 {
138 auto it = subLists.begin();
139 auto end = subLists.end();
140 while (it != end) {
141 if (!self.dvWrapField(visitor, f: toField(it.key()), obj&: it.value()))
142 return false;
143 ++it;
144 }
145 }
146 return true;
147}
148
149} // end namespace Dom
150} // end namespace QQmlJS
151QT_END_NAMESPACE
152

source code of qtdeclarative/src/qmldom/qqmldommock.cpp