diff options
author | Tatiana Borisova <tatiana.borisova@qt.io> | 2024-03-12 20:33:15 +0100 |
---|---|---|
committer | Tatiana Borisova <tatiana.borisova@qt.io> | 2024-03-20 15:21:00 +0100 |
commit | e5ebb9022ab9e00ab01d0bce527755da77083217 (patch) | |
tree | 0bed612324c53537eacfe296a34461c11d75d4c2 /src | |
parent | b7eb73b1f0b550c1697b70c3ab9ddb7480557faf (diff) |
QCborMap: use new comparison helper macros
Replace public operators operator==(), operator!=(), operator<() of
QCborMap to friend methods comparesEqual() / compareThreeWay().
Use QT_CORE_REMOVED_SINCE to get rid of current comparison methods
and replace them with a friend.
Delete #if 0 && __has_include(<compare>) blocks,
since they are not required anymore.
Add friend methods comparesEqual(QCborMap, QCborValue) and
compareThreeWay(QCborMap, QCborValue) to the QCborMap
class, to support comparison between QCborMap
and QCborValue elements, see test-case mapSelfAssign() ->
QT_TEST_EQUALITY_OPS(it.key(), QCborMap({{0, v}}), true);
Task-number: QTBUG-120300
Change-Id: I9e33df255d16484efd3124cf0632db859408fb5d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/compat/removed_api.cpp | 2 | ||||
-rw-r--r-- | src/corelib/serialization/qcbormap.cpp | 81 | ||||
-rw-r--r-- | src/corelib/serialization/qcbormap.h | 38 |
3 files changed, 102 insertions, 19 deletions
diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index 372b647bf0..f10eba5f7a 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -934,6 +934,8 @@ QUrl QUrl::fromEncoded(const QByteArray &input, ParsingMode mode) #include "qcborarray.h" // inlined API +#include "qcbormap.h" // inlined API + #include "qcborvalue.h" // inlined API #include "qdatastream.h" // inlined API diff --git a/src/corelib/serialization/qcbormap.cpp b/src/corelib/serialization/qcbormap.cpp index 57186a3d1f..9367a2ab9b 100644 --- a/src/corelib/serialization/qcbormap.cpp +++ b/src/corelib/serialization/qcbormap.cpp @@ -18,6 +18,10 @@ using namespace QtCbor; \brief The QCborMap class is used to hold an associative container representable in CBOR. + \compares strong + \compareswith strong QCborValue + \endcompareswith + This class can be used to hold an associative container in CBOR, a map between a key and a value type. CBOR is the Concise Binary Object Representation, a very compact form of binary data encoding that is a @@ -1105,10 +1109,10 @@ QCborValue QCborMap::extract(iterator it) */ /*! - \fn bool QCborMap::operator==(const QCborMap &other) const + \fn bool QCborMap::operator==(const QCborMap &lhs, const QCborMap &rhs) - Compares this map and \a other, comparing each element in sequence, and - returns true if the two maps contains the same elements in the same order, + Compares \a lhs and \a rhs maps, comparing each element in sequence, and + returns true if the two maps contain the same elements in the same order, false otherwise. Note that CBOR maps are unordered, which means that two maps containing the @@ -1124,10 +1128,10 @@ QCborValue QCborMap::extract(iterator it) */ /*! - \fn bool QCborMap::operator!=(const QCborMap &other) const + \fn bool QCborMap::operator!=(const QCborMap &lhs, const QCborMap &rhs) - Compares this map and \a other, comparing each element in sequence, and - returns true if the two maps contains any different elements or elements in + Compares \a lhs and \a rhs maps, comparing each element in sequence, and + returns true if the two maps contain any different elements or elements in different orders, false otherwise. Note that CBOR maps are unordered, which means that two maps containing the @@ -1143,10 +1147,10 @@ QCborValue QCborMap::extract(iterator it) */ /*! - \fn bool QCborMap::operator<(const QCborMap &other) const + \fn bool QCborMap::operator<(const QCborMap &lhs, const QCborMap &rhs) - Compares this map and \a other, comparing each element in sequence, and - returns true if this map should be sorted before \a other, false + Compares \a lhs and \a rhs maps, comparing each element in sequence, and + returns true if \a lhs map should be sorted before \a rhs, false otherwise. Note that CBOR maps are unordered, which means that two maps containing the @@ -1161,6 +1165,65 @@ QCborValue QCborMap::extract(iterator it) operator==(), operator!=() */ +/*! + \fn bool QCborMap::operator<=(const QCborMap &lhs, const QCborMap &rhs) + + Compares \a lhs and \a rhs maps, comparing each element in sequence, and + returns true if \a lhs map should be sorted before \a rhs or + if the two maps contain the same elements in the same order, false + otherwise. + + Note that CBOR maps are unordered, which means that two maps containing the + very same pairs but in different order will still compare differently. To + avoid this, it is recommended to insert elements into the map in a + predictable order, such as by ascending key value. In fact, maps with keys + in sorted order are required for Canonical CBOR representation. + + For more information on CBOR sorting order, see QCborValue::compare(). + + \sa compare(), QCborValue::operator==(), QCborMap::operator==(), + operator==(), operator!=() +*/ + +/*! + \fn bool QCborMap::operator>=(const QCborMap &lhs, const QCborMap &rhs) + + Compares \a lhs and \a rhs maps, comparing each element in sequence, and + returns true if \a lhs map should be sorted after \a rhs or + if the two maps contain the same elements in the same order, false + otherwise. + + Note that CBOR maps are unordered, which means that two maps containing the + very same pairs but in different order will still compare differently. To + avoid this, it is recommended to insert elements into the map in a + predictable order, such as by ascending key value. In fact, maps with keys + in sorted order are required for Canonical CBOR representation. + + For more information on CBOR sorting order, see QCborValue::compare(). + + \sa compare(), QCborValue::operator==(), QCborMap::operator==(), + operator==(), operator!=() +*/ + +/*! + \fn bool QCborMap::operator>(const QCborMap &lhs, const QCborMap &rhs) + + Compares \a lhs and \a rhs maps, comparing each element in sequence, and + returns true if \a lhs map should be sorted after \a rhs, false + otherwise. + + Note that CBOR maps are unordered, which means that two maps containing the + very same pairs but in different order will still compare differently. To + avoid this, it is recommended to insert elements into the map in a + predictable order, such as by ascending key value. In fact, maps with keys + in sorted order are required for Canonical CBOR representation. + + For more information on CBOR sorting order, see QCborValue::compare(). + + \sa compare(), QCborValue::operator==(), QCborMap::operator==(), + operator==(), operator!=() +*/ + void QCborMap::detach(qsizetype reserved) { d = QCborContainerPrivate::detach(d.data(), reserved ? reserved : size() * 2); diff --git a/src/corelib/serialization/qcbormap.h b/src/corelib/serialization/qcbormap.h index f5a118d673..55935e113e 100644 --- a/src/corelib/serialization/qcbormap.h +++ b/src/corelib/serialization/qcbormap.h @@ -221,19 +221,11 @@ public: { const_iterator it = find(key); return it != end(); } int compare(const QCborMap &other) const noexcept Q_DECL_PURE_FUNCTION; -#if 0 && __has_include(<compare>) - std::strong_ordering operator<=>(const QCborMap &other) const - { - int c = compare(other); - if (c > 0) return std::strong_ordering::greater; - if (c == 0) return std::strong_ordering::equivalent; - return std::strong_ordering::less; - } -#else +#if QT_CORE_REMOVED_SINCE(6, 8) bool operator==(const QCborMap &other) const noexcept { return compare(other) == 0; } bool operator!=(const QCborMap &other) const noexcept - { return !(*this == other); } + { return !operator==(other); } bool operator<(const QCborMap &other) const { return compare(other) < 0; } #endif @@ -308,6 +300,32 @@ private: friend class QJsonPrivate::Variant; void detach(qsizetype reserve = 0); + friend bool comparesEqual(const QCborMap &lhs, const QCborMap &rhs) noexcept + { + return lhs.compare(rhs) == 0; + } + friend Qt::strong_ordering compareThreeWay(const QCborMap &lhs, + const QCborMap &rhs) noexcept + { + int c = lhs.compare(rhs); + return Qt::compareThreeWay(c, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QCborMap) + + friend bool comparesEqual(const QCborMap &lhs, + const QCborValue &rhs) noexcept + { + return lhs.compare(rhs.toMap()) == 0; + } + + friend Qt::strong_ordering compareThreeWay(const QCborMap &lhs, + const QCborValue &rhs) noexcept + { + int c = lhs.compare(rhs.toMap()); + return Qt::compareThreeWay(c, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QCborMap, QCborValue) + explicit QCborMap(QCborContainerPrivate &dd) noexcept; QExplicitlySharedDataPointer<QCborContainerPrivate> d; }; |