diff options
| author | Rym Bouabid <rym.bouabid@qt.io> | 2024-04-03 16:56:34 +0200 |
|---|---|---|
| committer | Rym Bouabid <rym.bouabid@qt.io> | 2024-07-04 22:08:12 +0200 |
| commit | 7131240754d4caa3891f667b4645d8915cb82535 (patch) | |
| tree | 5b9e1518d055fb362ae56e6777e880240185727e /src/corelib/io/qurlquery.cpp | |
| parent | aca8235c753d673abe7442cdf8b628fe4e05c471 (diff) | |
QSharedDataPointer: Use new comparison helper macros
Provide the new comparesEqual() helper function as an implementation of
the (in)equality operators and compareThreeWay() helper function for
the rest of the relational operators.
Use Q_DECLARE_STRONGLY_ORDERED to provide all relational operators.
Use the new Qt::totally_ordered_wrapper to wrap the "d" pointer to
avoid UB when performing comparisons of QSharedDataPointer.
The old implementation of the operators between QSharedDataPointer
and the normal pointer T* used to cause a bug but this was not noticed
because QSharedDataPointer didn't have any tests.
I added tests and tried to compare using operator<(). I got
build issues:
error: no match for call to
‘(std::less<MyClass*>) (const MyClass*&, MyClass* const&)’
This happens because QSharedDataPointer has an implicit conversion
to MyClass*.
Making the normal pointer T* non-const as follow resolves the issue:
DECLARE_COMPARE_SET(const QSharedDataPointer &p1, p1.d, T *ptr, ptr)
DECLARE_COMPARE_SET(T *ptr, ptr, const QSharedDataPointer &p2, p2.d)
A similar bug happens if we make the normal pointer const in the
comparison macro Q_DECLARE_STRONGLY_ORDERED. The relational operators
will be ambiguous. The compiler picks between
operator<(QSharedDataPointer<MyClass>, MyClass*) and
opeartor<(MyClass*, MyClass*) (In case we're calling <).
Therefore, use:
Q_DECLARE_STRONGLY_ORDERED(QSharedDataPointer, T*)
Add some comparisons related tests for QSharedDataPointer.
Use QT_TEST_ALL_COMPARISON_OPS macros in unit-tests.
Task-number: QTBUG-120306
Change-Id: I8d1159a01651bab442b79a89c53e064914103133
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'src/corelib/io/qurlquery.cpp')
| -rw-r--r-- | src/corelib/io/qurlquery.cpp | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/corelib/io/qurlquery.cpp b/src/corelib/io/qurlquery.cpp index 31f3ee1d902..c155451edc3 100644 --- a/src/corelib/io/qurlquery.cpp +++ b/src/corelib/io/qurlquery.cpp @@ -174,8 +174,8 @@ template<> void QSharedDataPointer<QUrlQueryPrivate>::detach() : new QUrlQueryPrivate); x->ref.ref(); if (d && !d->ref.deref()) - delete d; - d = x; + delete d.get(); + d.reset(x); } // Here's how we do the encoding in QUrlQuery |
