Skip to content

Commit

Permalink
QDebug: add streaming operators for std::set
Browse files Browse the repository at this point in the history
The stream insertion operator for QDebug is not overloaded
to handle std::set

Overload the stream insertion operator for QDebug
to handle std::set

[ChangeLog][QtCore][QDebug] Added support for std::set.

Fixes: QTBUG-130574
Change-Id: I8e1589589c8f23318bf5073609995f7da4ea1108
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
  • Loading branch information
QtDheeru committed Nov 22, 2024
1 parent 278d225 commit 0c96528
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/corelib/io/qdebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,15 @@ QDebug &QDebug::putTupleLikeImplImpl(const char *ns, const char *what,
\c T need to support streaming into QDebug.
*/

/*!
\fn template <typename Key, typename Compare, typename Alloc> QDebug operator<<(QDebug debug, const std::set<Key, Compare, Alloc> &set)
\relates QDebug
\since 6.9
Writes the contents of \a set to \a debug. The \c Key type
needs to support streaming into QDebug.
*/

/*!
\fn template <typename Key, typename T, typename Hash, typename KeyEqual, typename Alloc> QDebug operator<<(QDebug debug, const std::unordered_map<Key, T, Compare, Alloc> &unordered_map)
\relates QDebug
Expand Down
7 changes: 7 additions & 0 deletions src/corelib/io/qdebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <set>
#include <tuple>
#include <QtCore/q20type_traits.h>
#include <utility>
Expand Down Expand Up @@ -438,6 +439,12 @@ inline QDebugIfHasDebugStream<Key, T> operator<<(QDebug debug, const std::multim
return QtPrivate::printSequentialContainer(std::move(debug), "std::multimap", map); // yes, sequential: *it is std::pair
}

template <typename Key, typename Compare, typename Alloc>
inline QDebug operator<<(QDebug debug, const std::set<Key, Compare, Alloc>& set)
{
return QtPrivate::printSequentialContainer(std::move(debug), "std::set", set);
}

template <typename Key, typename T, typename Hash, typename KeyEqual, typename Alloc>
inline QDebug operator<<(QDebug debug, const std::unordered_map<Key, T, Hash, KeyEqual, Alloc> &unordered_map)
{
Expand Down
36 changes: 36 additions & 0 deletions tests/auto/corelib/io/qdebug/tst_qdebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace pmr = std::pmr;
#else
namespace pmr = std;
#endif
#include <set>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
Expand All @@ -37,6 +38,7 @@ static_assert(QTypeTraits::has_ostream_operator_v<QDebug, int>);
static_assert(QTypeTraits::has_ostream_operator_v<QDebug, QMetaType>);
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::set<int>>);
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>>);
Expand Down Expand Up @@ -87,6 +89,7 @@ private slots:
void qDebugQUtf8StringView() const;
void qDebugQLatin1String() const;
void qDebugStdPair() const;
void qDebugStdSet() const;
void qDebugStdTuple() const;
void qDebugStdUnorderedMap() const;
void qDebugStdUnorderedSet() const;
Expand Down Expand Up @@ -733,6 +736,39 @@ void tst_QDebug::qDebugStdPair() const
}
}

void tst_QDebug::qDebugStdSet() const
{
QByteArray file, function;
int line = 0;
MessageHandlerSetter mhs(myMessageHandler);

{
QDebug d = qDebug();
std::set<int> Set{1, 2, 3, 2, 1};
d.nospace().noquote() << Set;
}
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, "std::set(1, 2, 3)"_L1);
QCOMPARE(s_file, file);
QCOMPARE(s_line, line);
QCOMPARE(s_function, function);

{
qDebug() << std::set<std::string>{"apple", "banana", "cherry", "banana", "apple"};
}

QCOMPARE(s_msg, "std::set(\"apple\", \"banana\", \"cherry\")"_L1);

{
qDebug() << std::set<int>{};
}

QCOMPARE(s_msg, "std::set()"_L1);
}

void tst_QDebug::qDebugStdTuple() const
{
QByteArray file, function;
Expand Down

0 comments on commit 0c96528

Please sign in to comment.