diff options
| author | Dheerendra Purohit <dheerendra@pthinks.com> | 2024-10-25 21:38:54 +0530 |
|---|---|---|
| committer | Dheerendra Purohit <dheerendra@pthinks.com> | 2024-11-20 22:42:38 +0000 |
| commit | a8c9a5617c72ad2e55bf497df6ff2c2e141cbdef (patch) | |
| tree | c1ab31f7e107dbfceb3aa3cfb540e3ea8a734131 | |
| parent | 7be9b5997d84910eeacead251ccdf002f1754147 (diff) | |
QDebug: add streaming operators for std::unordered_set
The stream insertion operator for QDebug is not overloaded
to handle std::unordered_set
Overload the stream insertion operator for QDebug
to handle std::unordered_set
[ChangeLog][QtCOre][QDebug] Added support for std::unordered_set.
Fixes: QTBUG-130481
Change-Id: I75a1f62f9ecb6b06ba45cc00d789cd3f3886b4c7
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
| -rw-r--r-- | src/corelib/io/qdebug.cpp | 10 | ||||
| -rw-r--r-- | src/corelib/io/qdebug.h | 7 | ||||
| -rw-r--r-- | tests/auto/corelib/io/qdebug/tst_qdebug.cpp | 43 |
3 files changed, 60 insertions, 0 deletions
diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp index 970fe15a776..c08bb07ca85 100644 --- a/src/corelib/io/qdebug.cpp +++ b/src/corelib/io/qdebug.cpp @@ -1198,6 +1198,16 @@ QDebug &QDebug::putTupleLikeImplImpl(const char *ns, const char *what, */ /*! + \fn template <typename Key, typename Hash, typename KeyEqual, typename Alloc> QDebug operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc> &unordered_set) + \relates QDebug + \since 6.9 + +Writes the contents of \a unordered_set to \a debug. The \c Key type +needs to support streaming into QDebug. +*/ + + +/*! \fn template <class Key, class T> QDebug operator<<(QDebug debug, const QHash<Key, T> &hash) \relates QDebug diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index 3fc63552953..93f326a7307 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -31,6 +31,7 @@ #include <QtCore/q20type_traits.h> #include <utility> #include <unordered_map> +#include <unordered_set> #include <vector> #if !defined(QT_LEAN_HEADERS) || QT_LEAN_HEADERS < 1 @@ -443,6 +444,12 @@ inline QDebug operator<<(QDebug debug, const std::unordered_map<Key, T, Hash, Ke return QtPrivate::printSequentialContainer(std::move(debug), "std::unordered_map", unordered_map); // yes, sequential: *it is std::pair } +template <typename Key, typename Hash, typename KeyEqual, typename Alloc> +inline QDebug operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc>& unordered_set) +{ + return QtPrivate::printSequentialContainer(std::move(debug), "std::unordered_set", unordered_set); +} + template <class Key, class T> inline QDebugIfHasDebugStreamContainer<QMap<Key, T>, Key, T> operator<<(QDebug debug, const QMap<Key, T> &map) { diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 3d1617fff21..a2deb675ece 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -27,6 +27,7 @@ namespace pmr = std; #endif #include <tuple> #include <unordered_map> +#include <unordered_set> using namespace std::chrono; using namespace q20::chrono; @@ -38,6 +39,7 @@ static_assert(QTypeTraits::has_ostream_operator_v<QDebug, QList<int>>); static_assert(QTypeTraits::has_ostream_operator_v<QDebug, QMap<int, QString>>); static_assert(QTypeTraits::has_ostream_operator_v<QDebug, std::tuple<int, QString, QMap<int, QString>>>); static_assert(QTypeTraits::has_ostream_operator_v<QDebug, std::unordered_map<int, QString>>); +static_assert(QTypeTraits::has_ostream_operator_v<QDebug, std::unordered_set<int>>); struct NonStreamable {}; static_assert(!QTypeTraits::has_ostream_operator_v<QDebug, NonStreamable>); static_assert(!QTypeTraits::has_ostream_operator_v<QDebug, QList<NonStreamable>>); @@ -87,6 +89,7 @@ private slots: void qDebugStdPair() const; void qDebugStdTuple() const; void qDebugStdUnorderedMap() const; + void qDebugStdUnorderedSet() const; void qDebugStdString() const; void qDebugStdStringView() const; void qDebugStdWString() const; @@ -813,6 +816,46 @@ void tst_QDebug::qDebugStdUnorderedMap() const QCOMPARE(s_msg, "std::unordered_map()"_L1); } +void tst_QDebug::qDebugStdUnorderedSet() const +{ + QByteArray file, function; + int line = 0; + MessageHandlerSetter mhs(myMessageHandler); + + { + QDebug d = qDebug(); + std::unordered_set<int> unorderedSet{1, 2, 3, 2, 1}; + d.nospace().noquote() << unorderedSet; + } +#ifndef QT_NO_MESSAGELOGCONTEXT + file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO; +#endif + QCOMPARE(s_msgType, QtDebugMsg); + + QStringList expectedValues = {"std::unordered_set", "1", "2", "3"}; + for (const QString &expectedValue : expectedValues) { + QVERIFY(s_msg.contains(expectedValue)); + } + QCOMPARE(s_file, file); + QCOMPARE(s_line, line); + QCOMPARE(s_function, function); + + { + qDebug() << std::unordered_set<std::string>{"apple", "banana", "cherry", "banana", "apple"}; + } + + expectedValues = {"std::unordered_set", "\"apple\"", "\"banana\"", "\"cherry\""}; + for (const QString &expectedValue : expectedValues) { + QVERIFY(s_msg.contains(expectedValue)); + } + + { + qDebug() << std::unordered_set<int>{}; // Empty set + } + + QCOMPARE(s_msg, "std::unordered_set()"_L1); +} + void tst_QDebug::qDebugStdString() const { QString file, function; |
