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 <edward.welbourne@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Isak Fyksen 2023-11-10 16:46:33 +01:00 committed by Marc Mutz
parent e5ebb9022a
commit b1e8287b8d
2 changed files with 14 additions and 14 deletions

View File

@ -649,7 +649,7 @@ bool QDomNodeListPrivate::operator!=(const QDomNodeListPrivate &other) const
return !operator==(other); return !operator==(other);
} }
void QDomNodeListPrivate::createList() void QDomNodeListPrivate::createList() const
{ {
if (!node_impl) if (!node_impl)
return; return;
@ -703,16 +703,21 @@ void QDomNodeListPrivate::createList()
} }
} }
QDomNodePrivate* QDomNodeListPrivate::item(int index) bool QDomNodeListPrivate::maybeCreateList() const
{ {
if (!node_impl) if (!node_impl)
return nullptr; return false;
const QDomDocumentPrivate *const doc = node_impl->ownerDocument(); const QDomDocumentPrivate *const doc = node_impl->ownerDocument();
if (!doc || timestamp != doc->nodeListTime) if (!doc || timestamp != doc->nodeListTime)
createList(); createList();
if (index >= list.size()) return true;
}
QDomNodePrivate *QDomNodeListPrivate::item(int index)
{
if (!maybeCreateList() || index >= list.size() || index < 0)
return nullptr; return nullptr;
return list.at(index); return list.at(index);
@ -720,15 +725,9 @@ QDomNodePrivate* QDomNodeListPrivate::item(int index)
int QDomNodeListPrivate::length() const int QDomNodeListPrivate::length() const
{ {
if (!node_impl) if (!maybeCreateList())
return 0; return 0;
const QDomDocumentPrivate *const doc = node_impl->ownerDocument();
if (!doc || timestamp != doc->nodeListTime) {
QDomNodeListPrivate *that = const_cast<QDomNodeListPrivate *>(this);
that->createList();
}
return list.size(); return list.size();
} }

View File

@ -142,7 +142,8 @@ public:
bool operator==(const QDomNodeListPrivate &) const; bool operator==(const QDomNodeListPrivate &) const;
bool operator!=(const QDomNodeListPrivate &) const; bool operator!=(const QDomNodeListPrivate &) const;
void createList(); void createList() const;
bool maybeCreateList() const;
QDomNodePrivate *item(int index); QDomNodePrivate *item(int index);
int length() const; int length() const;
@ -153,8 +154,8 @@ public:
QDomNodePrivate *node_impl; QDomNodePrivate *node_impl;
QString tagname; QString tagname;
QString nsURI; QString nsURI;
QList<QDomNodePrivate *> list; mutable QList<QDomNodePrivate *> list;
long timestamp; mutable long timestamp;
}; };
class QDomNamedNodeMapPrivate class QDomNamedNodeMapPrivate