From b1e8287b8db5463f9a933302a27671b2b0ca1fa5 Mon Sep 17 00:00:00 2001 From: Isak Fyksen Date: Fri, 10 Nov 2023 16:46:33 +0100 Subject: [PATCH] Extract method `bool QDomNodeListPrivate::maybeCreateList()` Extract duplicated logic to own method, and call where previously used. Also, make `QDomNodeListPrivate::[list|timestamp]` mutable, to allow `maybeCreateList()` and `createList()` to be `const` methods, and avoid `const_cast` in `QDomNodeListPrivate::length()`. Task-number: QTBUG-115076 Change-Id: I4f3a27315da28082a12cc4f5653567039b4cb376 Reviewed-by: Edward Welbourne Reviewed-by: Marc Mutz --- src/xml/dom/qdom.cpp | 21 ++++++++++----------- src/xml/dom/qdom_p.h | 7 ++++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/xml/dom/qdom.cpp b/src/xml/dom/qdom.cpp index 2ac044004e0..b25cdf487fc 100644 --- a/src/xml/dom/qdom.cpp +++ b/src/xml/dom/qdom.cpp @@ -649,7 +649,7 @@ bool QDomNodeListPrivate::operator!=(const QDomNodeListPrivate &other) const return !operator==(other); } -void QDomNodeListPrivate::createList() +void QDomNodeListPrivate::createList() const { if (!node_impl) return; @@ -703,16 +703,21 @@ void QDomNodeListPrivate::createList() } } -QDomNodePrivate* QDomNodeListPrivate::item(int index) +bool QDomNodeListPrivate::maybeCreateList() const { if (!node_impl) - return nullptr; + return false; const QDomDocumentPrivate *const doc = node_impl->ownerDocument(); if (!doc || timestamp != doc->nodeListTime) createList(); - if (index >= list.size()) + return true; +} + +QDomNodePrivate *QDomNodeListPrivate::item(int index) +{ + if (!maybeCreateList() || index >= list.size() || index < 0) return nullptr; return list.at(index); @@ -720,15 +725,9 @@ QDomNodePrivate* QDomNodeListPrivate::item(int index) int QDomNodeListPrivate::length() const { - if (!node_impl) + if (!maybeCreateList()) return 0; - const QDomDocumentPrivate *const doc = node_impl->ownerDocument(); - if (!doc || timestamp != doc->nodeListTime) { - QDomNodeListPrivate *that = const_cast(this); - that->createList(); - } - return list.size(); } diff --git a/src/xml/dom/qdom_p.h b/src/xml/dom/qdom_p.h index fb71f8ce236..b2ecd534c88 100644 --- a/src/xml/dom/qdom_p.h +++ b/src/xml/dom/qdom_p.h @@ -142,7 +142,8 @@ public: bool operator==(const QDomNodeListPrivate &) const; bool operator!=(const QDomNodeListPrivate &) const; - void createList(); + void createList() const; + bool maybeCreateList() const; QDomNodePrivate *item(int index); int length() const; @@ -153,8 +154,8 @@ public: QDomNodePrivate *node_impl; QString tagname; QString nsURI; - QList list; - long timestamp; + mutable QList list; + mutable long timestamp; }; class QDomNamedNodeMapPrivate