summaryrefslogtreecommitdiffstats
path: root/src/xml/dom/qdom.cpp
diff options
context:
space:
mode:
authorIsak Fyksen <isak.fyksen@qt.io>2023-11-10 16:59:04 +0100
committerIsak Fyksen <isak.fyksen@qt.io>2024-12-05 17:54:39 +0000
commit056e78f456fb050b7fc28d0dbd8ebd25d9eb291f (patch)
treeb2bc8d1831ad4cca57c250c1a16f97e8cfe3817e /src/xml/dom/qdom.cpp
parent387633a6069a5e0e9b976971691b1b82725b6132 (diff)
Add iterator, begin() and end() for QDomNodeList
Implement iterator for QDomNodeList, based on iterator from QVersionNumber. Also, extract node-iteration from createList() to forEachNode(), and have both createList() and new noexceptLength() use this for appending to the QList and counting nodes without allocating them respectively. noexceptLength() is needed for noexcept [c?r?]begin/[c?r?]end methods. Add tests using begin(), end(), rbegin(), rend(), and range-based for-loop. std::reverse_iterator::operator-> does not work with ArrowProxy in C++17. This issue will be addressed in QTBUG-131933. [ChangeLog][QtXml][QDomNodeList] Added iterator support. Fixes: QTBUG-115076 Change-Id: I94570bda3bda00ab7938e9ff0472a42e38425838 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/xml/dom/qdom.cpp')
-rw-r--r--src/xml/dom/qdom.cpp81
1 files changed, 77 insertions, 4 deletions
diff --git a/src/xml/dom/qdom.cpp b/src/xml/dom/qdom.cpp
index e2faa54aab0..e1a4a36aa08 100644
--- a/src/xml/dom/qdom.cpp
+++ b/src/xml/dom/qdom.cpp
@@ -649,22 +649,29 @@ void QDomNodeListPrivate::createList() const
if (!node_impl)
return;
+ list.clear();
const QDomDocumentPrivate *const doc = node_impl->ownerDocument();
if (doc && timestamp != doc->nodeListTime)
timestamp = doc->nodeListTime;
+ forEachNode([&](QDomNodePrivate *p){ list.append(p); });
+}
+
+void QDomNodeListPrivate::forEachNode(qxp::function_ref<void(QDomNodePrivate*)> yield) const
+{
+ if (!node_impl)
+ return;
QDomNodePrivate* p = node_impl->first;
- list.clear();
if (tagname.isNull()) {
while (p) {
- list.append(p);
+ yield(p);
p = p->next;
}
} else if (nsURI.isNull()) {
while (p && p != node_impl) {
if (p->isElement() && p->nodeName() == tagname) {
- list.append(p);
+ yield(p);
}
if (p->first)
p = p->first;
@@ -681,7 +688,7 @@ void QDomNodeListPrivate::createList() const
} else {
while (p && p != node_impl) {
if (p->isElement() && p->name==tagname && p->namespaceURI==nsURI) {
- list.append(p);
+ yield(p);
}
if (p->first)
p = p->first;
@@ -726,6 +733,13 @@ int QDomNodeListPrivate::length() const
return list.size();
}
+int QDomNodeListPrivate::noexceptLength() const noexcept
+{
+ int count = 0;
+ forEachNode([&](QDomNodePrivate*){ ++count; });
+ return count;
+}
+
/**************************************************************
*
* QDomNodeList
@@ -852,6 +866,16 @@ int QDomNodeList::length() const
}
/*!
+ Returns the number of nodes without creating the underlying QList.
+*/
+int QDomNodeList::noexceptLength() const noexcept
+{
+ if (!impl)
+ return 0;
+ return impl->noexceptLength();
+}
+
+/*!
\fn bool QDomNodeList::isEmpty() const
Returns \c true if the list contains no items; otherwise returns \c false.
@@ -881,6 +905,55 @@ int QDomNodeList::length() const
true).
*/
+/*!
+ \typedef QDomNodeList::const_iterator
+ \typedef QDomNodeList::const_reverse_iterator
+ \since 6.9
+
+ Typedefs for an opaque class that implements a (reverse) random-access
+ iterator over a QDomNodeList.
+
+ \note QDomNodeList does not support modifying nodes in-place, so
+ there is no mutable iterator.
+*/
+
+/*!
+ \typedef QDomNodeList::value_type
+ \typedef QDomNodeList::difference_type
+ \typedef QDomNodeList::size_type
+ \typedef QDomNodeList::reference
+ \typedef QDomNodeList::const_reference
+ \typedef QDomNodeList::pointer
+ \typedef QDomNodeList::const_pointer
+ \since 6.9
+
+ Provided for STL-compatibility.
+
+ \note QDomNodeList does not support modifying nodes in-place, so
+ reference and const_reference are the same type, as are pointer and
+ const_pointer.
+*/
+
+/*!
+ \fn QDomNodeList::begin() const
+ \fn QDomNodeList::end() const;
+ \fn QDomNodeList::rbegin() const
+ \fn QDomNodeList::rend() const;
+ \fn QDomNodeList::cbegin() const
+ \fn QDomNodeList::cend() const;
+ \fn QDomNodeList::crbegin() const
+ \fn QDomNodeList::crend() const;
+ \fn QDomNodeList::constBegin() const;
+ \fn QDomNodeList::constEnd() const;
+ \since 6.9
+
+ Returns a const_iterator or const_reverse_iterator, respectively, pointing
+ to the first or one past the last item in the list.
+
+ \note QDomNodeList does not support modifying nodes in-place, so
+ there is no mutable iterator.
+*/
+
/**************************************************************
*
* QDomNodePrivate