diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2023-03-28 22:42:49 +0200 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2023-04-13 14:01:56 +0200 |
commit | 015a1ed8e06c92a8dc788ab71f42f1c247e99405 (patch) | |
tree | a6a2fc3e30ed7e05fc8ced05aac5e5784f470c78 /src/qml/qml/qqmlscriptstring.cpp | |
parent | 922c89cf955f5703fca015f25e681cbcc783d580 (diff) |
QQmlScriptString: Avoid allocation in default constructed state
Classes like QQuickAnchorSetPrivate have multiple script string
properties, most of them being unused. Avoid allocating memory
in that case by simply letting the d pointer be null, and
adding null-checks in the various accessor methods.
Change-Id: I4b7a1298c4a5892028174b82c3a92ccd93bdf65e
Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Diffstat (limited to 'src/qml/qml/qqmlscriptstring.cpp')
-rw-r--r-- | src/qml/qml/qqmlscriptstring.cpp | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/qml/qml/qqmlscriptstring.cpp b/src/qml/qml/qqmlscriptstring.cpp index 370573b199..d478136085 100644 --- a/src/qml/qml/qqmlscriptstring.cpp +++ b/src/qml/qml/qqmlscriptstring.cpp @@ -44,7 +44,7 @@ const QQmlScriptStringPrivate* QQmlScriptStringPrivate::get(const QQmlScriptStri Constructs an empty instance. */ QQmlScriptString::QQmlScriptString() -: d(new QQmlScriptStringPrivate) +: d() { } @@ -92,6 +92,8 @@ bool QQmlScriptString::operator==(const QQmlScriptString &other) const { if (d == other.d) return true; + if (!d) + return false; if (d->isNumberLiteral || other.d->isNumberLiteral) return d->isNumberLiteral && other.d->isNumberLiteral && d->numberValue == other.d->numberValue; @@ -126,7 +128,7 @@ Returns whether the QQmlScriptString is empty. */ bool QQmlScriptString::isEmpty() const { - if (!d->script.isEmpty()) + if (!d || !d->script.isEmpty()) return false; return d->bindingId == -1; } @@ -136,7 +138,7 @@ Returns whether the content of the QQmlScriptString is the \c undefined literal. */ bool QQmlScriptString::isUndefinedLiteral() const { - return d->script == QLatin1String("undefined"); + return d && d->script == QLatin1String("undefined"); } /*! @@ -144,7 +146,7 @@ Returns whether the content of the QQmlScriptString is the \c null literal. */ bool QQmlScriptString::isNullLiteral() const { - return d->script == QLatin1String("null"); + return d && d->script == QLatin1String("null"); } /*! @@ -153,7 +155,7 @@ Otherwise returns a null QString. */ QString QQmlScriptString::stringLiteral() const { - if (d->isStringLiteral) + if (d && d->isStringLiteral) return d->script.mid(1, d->script.size()-2); return QString(); } @@ -165,8 +167,8 @@ sets \a ok to true. Otherwise returns 0.0 and sets \a ok to false. qreal QQmlScriptString::numberLiteral(bool *ok) const { if (ok) - *ok = d->isNumberLiteral; - return d->isNumberLiteral ? d->numberValue : 0.; + *ok = d && d->isNumberLiteral; + return (d && d->isNumberLiteral) ? d->numberValue : 0.; } /*! @@ -175,8 +177,8 @@ sets \a ok to true. Otherwise returns false and sets \a ok to false. */ bool QQmlScriptString::booleanLiteral(bool *ok) const { - bool isTrue = d->script == QLatin1String("true"); - bool isFalse = !isTrue && d->script == QLatin1String("false"); + bool isTrue = d && d->script == QLatin1String("true"); + bool isFalse = !isTrue && d && d->script == QLatin1String("false"); if (ok) *ok = isTrue || isFalse; return isTrue ? true : false; |