Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-07 10:16:13

0001 // Copyright (C) 2016 The Qt Company Ltd.
0002 // Copyright (C) 2016 Intel Corporation.
0003 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0004 // Qt-Security score:critical reason:data-parsing
0005 
0006 #ifndef QDEBUG_H
0007 #define QDEBUG_H
0008 
0009 #if 0
0010 #pragma qt_class(QtDebug)
0011 #endif
0012 
0013 #include <QtCore/qcompare.h>
0014 #include <QtCore/qcontainerfwd.h>
0015 #include <QtCore/qfloat16.h>
0016 #include <QtCore/qtextstream.h>
0017 #include <QtCore/qttypetraits.h>
0018 #include <QtCore/qtypes.h>
0019 #include <QtCore/qstring.h>
0020 #include <QtCore/qcontiguouscache.h>
0021 #include <QtCore/qsharedpointer.h>
0022 
0023 // all these have already been included by various headers above, but don't rely on indirect includes:
0024 #include <array>
0025 #include <chrono>
0026 #include <list>
0027 #include <map>
0028 #include <memory>
0029 #include <optional>
0030 #include <string>
0031 #include <string_view>
0032 #include <set>
0033 #include <tuple>
0034 #include <QtCore/q20type_traits.h>
0035 #include <utility>
0036 #include <unordered_map>
0037 #include <unordered_set>
0038 #include <vector>
0039 
0040 #if !defined(QT_LEAN_HEADERS) || QT_LEAN_HEADERS < 1
0041 #  include <QtCore/qlist.h>
0042 #  include <QtCore/qmap.h>
0043 #  include <QtCore/qset.h>
0044 #  include <QtCore/qvarlengtharray.h>
0045 #endif
0046 
0047 QT_BEGIN_NAMESPACE
0048 
0049 class QT6_ONLY(Q_CORE_EXPORT) QDebug : public QIODeviceBase
0050 {
0051     friend class QMessageLogger;
0052     friend class QDebugStateSaver;
0053     friend class QDebugStateSaverPrivate;
0054     struct Stream {
0055         enum { VerbosityShift = 29, VerbosityMask = 0x7 };
0056 
0057         explicit Stream(QIODevice *device)
0058             : ts(device)
0059         {}
0060         explicit Stream(QString *string)
0061             : ts(string, WriteOnly)
0062         {}
0063         explicit Stream(QByteArray *ba)
0064             : ts(ba, WriteOnly)
0065         {}
0066         explicit Stream(QtMsgType t)
0067             : ts(&buffer, WriteOnly),
0068               type(t),
0069               message_output(true)
0070         {}
0071         QTextStream ts;
0072         QString buffer;
0073         int ref = 1;
0074         QtMsgType type = QtDebugMsg;
0075         bool space = true;
0076         bool noQuotes = false;
0077         bool message_output = false;
0078         int verbosity = DefaultVerbosity;
0079         QMessageLogContext context;
0080     } *stream;
0081 
0082     enum Latin1Content { ContainsBinary = 0, ContainsLatin1 };
0083 
0084     QT7_ONLY(Q_CORE_EXPORT) void putUcs4(uint ucs4);
0085     QT7_ONLY(Q_CORE_EXPORT) void putString(const QChar *begin, size_t length);
0086     QT7_ONLY(Q_CORE_EXPORT) void putByteArray(const char *begin, size_t length, Latin1Content content);
0087     QT7_ONLY(Q_CORE_EXPORT) void putTimeUnit(qint64 num, qint64 den);
0088     QT7_ONLY(Q_CORE_EXPORT) void putInt128(const void *i);
0089     QT7_ONLY(Q_CORE_EXPORT) void putUInt128(const void *i);
0090     QT7_ONLY(Q_CORE_EXPORT) void putQtOrdering(QtOrderingPrivate::QtOrderingTypeFlag flags,
0091                                                Qt::partial_ordering order);
0092 
0093     template <typename...Ts>
0094     using if_streamable = std::enable_if_t<
0095             std::conjunction_v<QTypeTraits::has_ostream_operator<QDebug, Ts>...>
0096         , bool>;
0097 public:
0098     explicit QDebug(QIODevice *device) : stream(new Stream(device)) {}
0099     explicit QDebug(QString *string) : stream(new Stream(string)) {}
0100     explicit QDebug(QByteArray *bytes) : stream(new Stream(bytes)) {}
0101     explicit QDebug(QtMsgType t) : stream(new Stream(t)) {}
0102     QDebug(const QDebug &o) : stream(o.stream) { ++stream->ref; }
0103     QDebug(QDebug &&other) noexcept : stream{std::exchange(other.stream, nullptr)} {}
0104     inline QDebug &operator=(const QDebug &other);
0105     QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QDebug)
0106     ~QDebug();
0107     void swap(QDebug &other) noexcept { qt_ptr_swap(stream, other.stream); }
0108 
0109     QT7_ONLY(Q_CORE_EXPORT) QDebug &resetFormat();
0110 
0111     inline QDebug &space() { stream->space = true; stream->ts << ' '; return *this; }
0112     inline QDebug &nospace() { stream->space = false; return *this; }
0113     inline QDebug &maybeSpace() { if (stream->space) stream->ts << ' '; return *this; }
0114     inline QDebug &verbosity(int verbosityLevel) { stream->verbosity = verbosityLevel; return *this; }
0115     int verbosity() const { return stream->verbosity; }
0116     void setVerbosity(int verbosityLevel) { stream->verbosity = verbosityLevel; }
0117     enum VerbosityLevel { MinimumVerbosity = 0, DefaultVerbosity = 2, MaximumVerbosity = 7 };
0118 
0119     bool autoInsertSpaces() const { return stream->space; }
0120     void setAutoInsertSpaces(bool b) { stream->space = b; }
0121 
0122     [[nodiscard]] bool quoteStrings() const noexcept { return !stream->noQuotes; }
0123     void setQuoteStrings(bool b) { stream->noQuotes = !b; }
0124 
0125     inline QDebug &quote() { stream->noQuotes = false; return *this; }
0126     inline QDebug &noquote() { stream->noQuotes = true; return *this; }
0127     inline QDebug &maybeQuote(char c = '"') { if (!stream->noQuotes) stream->ts << c; return *this; }
0128 
0129     inline QDebug &operator<<(QChar t) { putUcs4(t.unicode()); return maybeSpace(); }
0130     inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); }
0131     inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); }
0132     inline QDebug &operator<<(signed short t) { stream->ts << t; return maybeSpace(); }
0133     inline QDebug &operator<<(unsigned short t) { stream->ts << t; return maybeSpace(); }
0134     inline QDebug &operator<<(char16_t t) { return *this << QChar(t); }
0135     inline QDebug &operator<<(char32_t t) { putUcs4(t); return maybeSpace(); }
0136     inline QDebug &operator<<(signed int t) { stream->ts << t; return maybeSpace(); }
0137     inline QDebug &operator<<(unsigned int t) { stream->ts << t; return maybeSpace(); }
0138     inline QDebug &operator<<(signed long t) { stream->ts << t; return maybeSpace(); }
0139     inline QDebug &operator<<(unsigned long t) { stream->ts << t; return maybeSpace(); }
0140     inline QDebug &operator<<(qint64 t) { stream->ts << t; return maybeSpace(); }
0141     inline QDebug &operator<<(quint64 t) { stream->ts << t; return maybeSpace(); }
0142     inline QDebug &operator<<(qfloat16 t) { stream->ts << t; return maybeSpace(); }
0143     inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); }
0144     inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); }
0145     inline QDebug &operator<<(const char* t) { stream->ts << QString::fromUtf8(t); return maybeSpace(); }
0146     inline QDebug &operator<<(const char16_t *t)  { stream->ts << QStringView(t); return maybeSpace(); }
0147     inline QDebug &operator<<(const QString & t) { putString(t.constData(), size_t(t.size())); return maybeSpace(); }
0148     inline QDebug &operator<<(QStringView s) { putString(s.data(), size_t(s.size())); return maybeSpace(); }
0149     inline QDebug &operator<<(QUtf8StringView s) { putByteArray(reinterpret_cast<const char*>(s.data()), s.size(), ContainsBinary); return maybeSpace(); }
0150     inline QDebug &operator<<(QLatin1StringView t) { putByteArray(t.latin1(), t.size(), ContainsLatin1); return maybeSpace(); }
0151     inline QDebug &operator<<(const QByteArray & t) { putByteArray(t.constData(), t.size(), ContainsBinary); return maybeSpace(); }
0152     inline QDebug &operator<<(QByteArrayView t) { putByteArray(t.constData(), t.size(), ContainsBinary); return maybeSpace(); }
0153     inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); }
0154     inline QDebug &operator<<(std::nullptr_t) { stream->ts << "(nullptr)"; return maybeSpace(); }
0155     inline QDebug &operator<<(std::nullopt_t) { stream->ts << "nullopt"; return maybeSpace(); }
0156     inline QDebug &operator<<(QTextStreamFunction f) {
0157         stream->ts << f;
0158         return *this;
0159     }
0160 
0161     inline QDebug &operator<<(QTextStreamManipulator m)
0162     { stream->ts << m; return *this; }
0163 
0164 #ifdef Q_QDOC
0165     template <typename Char, typename...Args>
0166     QDebug &operator<<(const std::basic_string<Char, Args...> &s);
0167 
0168     template <typename Char, typename...Args>
0169     QDebug &operator<<(std::basic_string_view<Char, Args...> s);
0170 #else
0171     template <typename...Args>
0172     QDebug &operator<<(const std::basic_string<char, Args...> &s)
0173     { return *this << QUtf8StringView(s); }
0174 
0175     template <typename...Args>
0176     QDebug &operator<<(std::basic_string_view<char, Args...> s)
0177     { return *this << QUtf8StringView(s); }
0178 
0179 #ifdef __cpp_char8_t
0180     template <typename...Args>
0181     QDebug &operator<<(const std::basic_string<char8_t, Args...> &s)
0182     { return *this << QUtf8StringView(s); }
0183 
0184     template <typename...Args>
0185     QDebug &operator<<(std::basic_string_view<char8_t, Args...> s)
0186     { return *this << QUtf8StringView(s); }
0187 #endif // __cpp_char8_t
0188 
0189     template <typename...Args>
0190     QDebug &operator<<(const std::basic_string<char16_t, Args...> &s)
0191     { return *this << QStringView(s); }
0192 
0193     template <typename...Args>
0194     QDebug &operator<<(std::basic_string_view<char16_t, Args...> s)
0195     { return *this << QStringView(s); }
0196 
0197     template <typename...Args>
0198     QDebug &operator<<(const std::basic_string<wchar_t, Args...> &s)
0199     {
0200         if constexpr (sizeof(wchar_t) == 2)
0201             return *this << QStringView(s);
0202         else
0203             return *this << QString::fromWCharArray(s.data(), s.size()); // ### optimize
0204     }
0205 
0206     template <typename...Args>
0207     QDebug &operator<<(std::basic_string_view<wchar_t, Args...> s)
0208     {
0209         if constexpr (sizeof(wchar_t) == 2)
0210             return *this << QStringView(s);
0211         else
0212             return *this << QString::fromWCharArray(s.data(), s.size()); // ### optimize
0213     }
0214 
0215     template <typename...Args>
0216     QDebug &operator<<(const std::basic_string<char32_t, Args...> &s)
0217     { return *this << QString::fromUcs4(s.data(), s.size()); }
0218 
0219     template <typename...Args>
0220     QDebug &operator<<(std::basic_string_view<char32_t, Args...> s)
0221     { return *this << QString::fromUcs4(s.data(), s.size()); }
0222 #endif // !Q_QDOC
0223 
0224     template <typename Rep, typename Period>
0225     QDebug &operator<<(std::chrono::duration<Rep, Period> duration)
0226     {
0227         stream->ts << duration.count();
0228         putTimeUnit(Period::num, Period::den);
0229         return maybeSpace();
0230     }
0231 
0232 #ifdef QT_SUPPORTS_INT128
0233 private:
0234     // Constrained templates so they only match q(u)int128 without conversions.
0235     // Also keeps these operators out of the ABI.
0236     template <typename T>
0237     using if_qint128 = std::enable_if_t<std::is_same_v<T, qint128>, bool>;
0238     template <typename T>
0239     using if_quint128 = std::enable_if_t<std::is_same_v<T, quint128>, bool>;
0240 public:
0241     template <typename T, if_qint128<T> = true>
0242     QDebug &operator<<(T i128) { putInt128(&i128); return maybeSpace(); }
0243     template <typename T, if_quint128<T> = true>
0244     QDebug &operator<<(T u128) { putUInt128(&u128); return maybeSpace(); }
0245 #endif // QT_SUPPORTS_INT128
0246 
0247 private:
0248     template <typename T>
0249     static void streamTypeErased(QDebug &d, const void *obj)
0250     {
0251         d << *static_cast<const T*>(obj);
0252     }
0253     using StreamTypeErased = void(*)(QDebug&, const void*);
0254     QT7_ONLY(Q_CORE_EXPORT) static QString toStringImpl(StreamTypeErased s, const void *obj);
0255     QT7_ONLY(Q_CORE_EXPORT) static QByteArray toBytesImpl(StreamTypeErased s, const void *obj);
0256     QT7_ONLY(Q_CORE_EXPORT) QDebug &putTupleLikeImplImpl(const char *ns, const char *what, size_t n,
0257                                                          StreamTypeErased *ops, const void **data);
0258 
0259     template <typename TupleLike, size_t...Is>
0260     QDebug &putTupleLikeImpl(const char *ns, const char *what, const TupleLike &t,
0261                              std::index_sequence<Is...>)
0262     {
0263         if constexpr (sizeof...(Is)) {
0264             StreamTypeErased ops[] = {
0265                 &streamTypeErased<q20::remove_cvref_t<std::tuple_element_t<Is, TupleLike>>>...
0266             };
0267             const void *data[] = {
0268                 std::addressof(std::get<Is>(t))...
0269             };
0270             return putTupleLikeImplImpl(ns, what, sizeof...(Is), ops, data);
0271         } else {
0272             return putTupleLikeImplImpl(ns, what, 0, nullptr, nullptr);
0273         }
0274     }
0275 
0276     template <typename TupleLike>
0277     QDebug &putTupleLike(const char *ns, const char *what, const TupleLike &t)
0278     {
0279         using Indexes = std::make_index_sequence<std::tuple_size_v<TupleLike>>;
0280         return putTupleLikeImpl(ns, what, t, Indexes{});
0281     }
0282 public:
0283     template <typename T>
0284     static QString toString(const T &object)
0285     {
0286         return toStringImpl(&streamTypeErased<T>, std::addressof(object));
0287     }
0288 
0289     template <typename T>
0290     static QByteArray toBytes(const T &object)
0291     {
0292         return toBytesImpl(&streamTypeErased<T>, std::addressof(object));
0293     }
0294 
0295     template <typename...Ts, if_streamable<Ts...> = true>
0296     QDebug &operator<<(const std::tuple<Ts...> &t)
0297     {
0298         return putTupleLike("std", "tuple", t);
0299     }
0300 
0301     template <typename T, if_streamable<T> = true>
0302     QDebug &operator<<(const std::optional<T> &o)
0303     {
0304         if (!o)
0305             return *this << std::nullopt;
0306         StreamTypeErased s = &streamTypeErased<std::remove_cv_t<T>>;
0307         const void *d = std::addressof(*o);
0308         return putTupleLikeImplImpl("std", "optional", 1, &s, &d);
0309     }
0310 
0311 private:
0312     template <typename T>
0313     using if_ordering_type = std::enable_if_t<QtOrderingPrivate::is_ordering_type_v<T>, bool>;
0314 
0315     template <typename T, if_ordering_type<T> = true>
0316     friend QDebug operator<<(QDebug debug, T t)
0317     {
0318         debug.putQtOrdering(QtOrderingPrivate::orderingFlagsFor(t), Qt::partial_ordering(t));
0319         return debug;
0320     }
0321 };
0322 
0323 Q_DECLARE_SHARED(QDebug)
0324 
0325 class QDebugStateSaverPrivate;
0326 class QDebugStateSaver
0327 {
0328 public:
0329     Q_NODISCARD_CTOR Q_CORE_EXPORT
0330     QDebugStateSaver(QDebug &dbg);
0331     Q_CORE_EXPORT
0332     ~QDebugStateSaver();
0333 private:
0334     Q_DISABLE_COPY(QDebugStateSaver)
0335     std::unique_ptr<QDebugStateSaverPrivate> d;
0336 };
0337 
0338 class QNoDebug
0339 {
0340 public:
0341     inline QNoDebug &operator<<(QTextStreamFunction) { return *this; }
0342     inline QNoDebug &operator<<(QTextStreamManipulator) { return *this; }
0343     inline QNoDebug &space() { return *this; }
0344     inline QNoDebug &nospace() { return *this; }
0345     inline QNoDebug &maybeSpace() { return *this; }
0346     inline QNoDebug &quote() { return *this; }
0347     inline QNoDebug &noquote() { return *this; }
0348     inline QNoDebug &maybeQuote(const char = '"') { return *this; }
0349     inline QNoDebug &verbosity(int) { return *this; }
0350 
0351     template<typename T>
0352     inline QNoDebug &operator<<(const T &) { return *this; }
0353 };
0354 
0355 QNoDebug QMessageLogger::noDebug(...) const noexcept
0356 { return {}; }
0357 
0358 inline QDebug &QDebug::operator=(const QDebug &other)
0359 {
0360     QDebug{other}.swap(*this);
0361     return *this;
0362 }
0363 
0364 namespace QtPrivate {
0365 
0366 template <typename SequentialContainer>
0367 inline QDebug printSequentialContainer(QDebug debug, const char *which, const SequentialContainer &c)
0368 {
0369     const QDebugStateSaver saver(debug);
0370     debug.nospace() << which << '(';
0371     typename SequentialContainer::const_iterator it = c.begin(), end = c.end();
0372     if (it != end) {
0373         debug << *it;
0374         ++it;
0375     }
0376     while (it != end) {
0377         debug << ", " << *it;
0378         ++it;
0379     }
0380     debug << ')';
0381     return debug;
0382 }
0383 
0384 template <typename AssociativeContainer>
0385 inline QDebug printAssociativeContainer(QDebug debug, const char *which, const AssociativeContainer &c)
0386 {
0387     const QDebugStateSaver saver(debug);
0388     debug.nospace() << which << "(";
0389     for (typename AssociativeContainer::const_iterator it = c.constBegin();
0390          it != c.constEnd(); ++it) {
0391         debug << '(' << it.key() << ", " << it.value() << ')';
0392     }
0393     debug << ')';
0394     return debug;
0395 }
0396 
0397 } // namespace QtPrivate
0398 
0399 template<typename ...T>
0400 using QDebugIfHasDebugStream =
0401     std::enable_if_t<std::conjunction_v<QTypeTraits::has_ostream_operator<QDebug, T>...>, QDebug>;
0402 
0403 template<typename Container, typename ...T>
0404 using QDebugIfHasDebugStreamContainer =
0405     std::enable_if_t<std::conjunction_v<QTypeTraits::has_ostream_operator_container<QDebug, Container, T>...>, QDebug>;
0406 
0407 #ifndef Q_QDOC
0408 
0409 template<typename T>
0410 inline QDebugIfHasDebugStreamContainer<QList<T>, T> operator<<(QDebug debug, const QList<T> &vec)
0411 {
0412     return QtPrivate::printSequentialContainer(std::move(debug), "QList", vec);
0413 }
0414 
0415 template<typename T, qsizetype P>
0416 inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const QVarLengthArray<T, P> &vec)
0417 {
0418     return QtPrivate::printSequentialContainer(std::move(debug), "QVarLengthArray", vec);
0419 }
0420 
0421 template <typename T, typename Alloc>
0422 inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const std::vector<T, Alloc> &vec)
0423 {
0424     return QtPrivate::printSequentialContainer(std::move(debug), "std::vector", vec);
0425 }
0426 
0427 template <typename T, std::size_t N>
0428 inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const std::array<T, N> &array)
0429 {
0430     return QtPrivate::printSequentialContainer(std::move(debug), "std::array", array);
0431 }
0432 
0433 template <typename T, typename Alloc>
0434 inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const std::list<T, Alloc> &vec)
0435 {
0436     return QtPrivate::printSequentialContainer(std::move(debug), "std::list", vec);
0437 }
0438 
0439 template <typename T>
0440 inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, std::initializer_list<T> list)
0441 {
0442     return QtPrivate::printSequentialContainer(std::move(debug), "std::initializer_list", list);
0443 }
0444 
0445 template <typename Key, typename T, typename Compare, typename Alloc>
0446 inline QDebugIfHasDebugStream<Key, T> operator<<(QDebug debug, const std::map<Key, T, Compare, Alloc> &map)
0447 {
0448     return QtPrivate::printSequentialContainer(std::move(debug), "std::map", map); // yes, sequential: *it is std::pair
0449 }
0450 
0451 template <typename Key, typename T, typename Compare, typename Alloc>
0452 inline QDebugIfHasDebugStream<Key, T> operator<<(QDebug debug, const std::multimap<Key, T, Compare, Alloc> &map)
0453 {
0454     return QtPrivate::printSequentialContainer(std::move(debug), "std::multimap", map); // yes, sequential: *it is std::pair
0455 }
0456 
0457 template <typename Key, typename Compare, typename Alloc>
0458 inline QDebugIfHasDebugStream<Key> operator<<(QDebug debug, const std::multiset<Key, Compare, Alloc> &multiset)
0459 {
0460     return QtPrivate::printSequentialContainer(std::move(debug), "std::multiset", multiset);
0461 }
0462 
0463 template <typename Key, typename Compare, typename Alloc>
0464 inline QDebugIfHasDebugStream<Key> operator<<(QDebug debug, const std::set<Key, Compare, Alloc> &set)
0465 {
0466     return QtPrivate::printSequentialContainer(std::move(debug), "std::set", set);
0467 }
0468 
0469 template <typename Key, typename T, typename Hash, typename KeyEqual, typename Alloc>
0470 inline QDebugIfHasDebugStream<Key, T> operator<<(QDebug debug, const std::unordered_map<Key, T, Hash, KeyEqual, Alloc> &unordered_map)
0471 {
0472     return QtPrivate::printSequentialContainer(std::move(debug), "std::unordered_map", unordered_map); // yes, sequential: *it is std::pair
0473 }
0474 
0475 template <typename Key, typename Hash, typename KeyEqual, typename Alloc>
0476 inline QDebugIfHasDebugStream<Key> operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc> &unordered_set)
0477 {
0478     return QtPrivate::printSequentialContainer(std::move(debug), "std::unordered_set", unordered_set);
0479 }
0480 
0481 template <class Key, class T>
0482 inline QDebugIfHasDebugStreamContainer<QMap<Key, T>, Key, T> operator<<(QDebug debug, const QMap<Key, T> &map)
0483 {
0484     return QtPrivate::printAssociativeContainer(std::move(debug), "QMap", map);
0485 }
0486 
0487 template <class Key, class T>
0488 inline QDebugIfHasDebugStreamContainer<QMultiMap<Key, T>, Key, T> operator<<(QDebug debug, const QMultiMap<Key, T> &map)
0489 {
0490     return QtPrivate::printAssociativeContainer(std::move(debug), "QMultiMap", map);
0491 }
0492 
0493 template <class Key, class T>
0494 inline QDebugIfHasDebugStreamContainer<QHash<Key, T>, Key, T> operator<<(QDebug debug, const QHash<Key, T> &hash)
0495 {
0496     return QtPrivate::printAssociativeContainer(std::move(debug), "QHash", hash);
0497 }
0498 
0499 template <class Key, class T>
0500 inline QDebugIfHasDebugStreamContainer<QMultiHash<Key, T>, Key, T> operator<<(QDebug debug, const QMultiHash<Key, T> &hash)
0501 {
0502     return QtPrivate::printAssociativeContainer(std::move(debug), "QMultiHash", hash);
0503 }
0504 
0505 template <class T1, class T2>
0506 inline QDebugIfHasDebugStream<T1, T2> operator<<(QDebug debug, const std::pair<T1, T2> &pair)
0507 {
0508     const QDebugStateSaver saver(debug);
0509     debug.nospace() << "std::pair(" << pair.first << ", " << pair.second << ')';
0510     return debug;
0511 }
0512 
0513 template <typename T>
0514 inline QDebugIfHasDebugStreamContainer<QSet<T>, T> operator<<(QDebug debug, const QSet<T> &set)
0515 {
0516     return QtPrivate::printSequentialContainer(std::move(debug), "QSet", set);
0517 }
0518 
0519 template <class T>
0520 inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const QContiguousCache<T> &cache)
0521 {
0522     const QDebugStateSaver saver(debug);
0523     debug.nospace() << "QContiguousCache(";
0524     for (qsizetype i = cache.firstIndex(); i <= cache.lastIndex(); ++i) {
0525         debug << cache[i];
0526         if (i != cache.lastIndex())
0527             debug << ", ";
0528     }
0529     debug << ')';
0530     return debug;
0531 }
0532 
0533 #else
0534 template <class T>
0535 QDebug operator<<(QDebug debug, const QList<T> &list);
0536 
0537 template <class T, qsizetype P>
0538 QDebug operator<<(QDebug debug, const QVarLengthArray<T, P> &array);
0539 
0540 template <typename T, typename Alloc>
0541 QDebug operator<<(QDebug debug, const std::vector<T, Alloc> &vec);
0542 
0543 template <typename T, std::size_t N>
0544 QDebug operator<<(QDebug debug, const std::array<T, N> &array);
0545 
0546 template <typename T, typename Alloc>
0547 QDebug operator<<(QDebug debug, const std::list<T, Alloc> &vec);
0548 
0549 template <typename Key, typename T, typename Compare, typename Alloc>
0550 QDebug operator<<(QDebug debug, const std::map<Key, T, Compare, Alloc> &map);
0551 
0552 template <typename Key, typename T, typename Compare, typename Alloc>
0553 QDebug operator<<(QDebug debug, const std::multimap<Key, T, Compare, Alloc> &map);
0554 
0555 template <class Key, class T>
0556 QDebug operator<<(QDebug debug, const QMap<Key, T> &map);
0557 
0558 template <class Key, class T>
0559 QDebug operator<<(QDebug debug, const QMultiMap<Key, T> &map);
0560 
0561 template <class Key, class T>
0562 QDebug operator<<(QDebug debug, const QHash<Key, T> &hash);
0563 
0564 template <class Key, class T>
0565 QDebug operator<<(QDebug debug, const QMultiHash<Key, T> &hash);
0566 
0567 template <typename T>
0568 QDebug operator<<(QDebug debug, const QSet<T> &set);
0569 
0570 template <class T1, class T2>
0571 QDebug operator<<(QDebug debug, const std::pair<T1, T2> &pair);
0572 
0573 template <typename T>
0574 QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache);
0575 
0576 template <typename Key, typename Compare, typename Alloc>
0577 QDebug operator<<(QDebug debug, const std::multiset<Key, Compare, Alloc> &multiset);
0578 
0579 template <typename Key, typename Compare, typename Alloc>
0580 QDebug operator<<(QDebug debug, const std::set<Key, Compare, Alloc> &set);
0581 
0582 template <typename Key, typename T, typename Hash, typename KeyEqual, typename Alloc>
0583 QDebug operator<<(QDebug debug, const std::unordered_map<Key, T, Hash, KeyEqual, Alloc> &unordered_map);
0584 
0585 template <typename Key, typename Hash, typename KeyEqual, typename Alloc>
0586 QDebug operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc> &unordered_set);
0587 
0588 #endif // Q_QDOC
0589 
0590 template <class T>
0591 inline QDebug operator<<(QDebug debug, const QSharedPointer<T> &ptr)
0592 {
0593     QDebugStateSaver saver(debug);
0594     debug.nospace() << "QSharedPointer(" << ptr.data() << ")";
0595     return debug;
0596 }
0597 
0598 template <typename T, typename Tag> class QTaggedPointer;
0599 
0600 template <typename T, typename Tag>
0601 inline QDebug operator<<(QDebug debug, const QTaggedPointer<T, Tag> &ptr)
0602 {
0603     QDebugStateSaver saver(debug);
0604     debug.nospace() << "QTaggedPointer(" << ptr.pointer() << ", " << ptr.tag() << ")";
0605     return debug;
0606 }
0607 
0608 Q_CORE_EXPORT QDebug qt_QMetaEnum_debugOperator(QDebug&, qint64 value, const QMetaObject *meta, const char *name);
0609 Q_CORE_EXPORT QDebug qt_QMetaEnum_flagDebugOperator(QDebug &dbg, quint64 value, const QMetaObject *meta, const char *name);
0610 Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, uint value);
0611 Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, quint64 value);
0612 
0613 template <typename Int>
0614 void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, Int value)
0615 {
0616     static_assert(std::is_unsigned_v<Int>,
0617             "Cast value to an unsigned type before calling this function");
0618     const QDebugStateSaver saver(debug);
0619     debug.resetFormat();
0620     debug.nospace() << "QFlags(" << Qt::hex << Qt::showbase;
0621     bool needSeparator = false;
0622     for (size_t i = 0; i < sizeofT * 8; ++i) {
0623         if (value & (Int(1) << i)) {
0624             if (needSeparator)
0625                 debug << '|';
0626             else
0627                 needSeparator = true;
0628             debug << (Int(1) << i);
0629         }
0630     }
0631     debug << ')';
0632 }
0633 
0634 template <class Flags,
0635           std::enable_if_t<QtPrivate::IsQFlags<Flags>::value, bool> = true>
0636 inline QDebug operator<<(QDebug debug, Flags flags)
0637 {
0638     using T = typename Flags::enum_type;
0639     using UInt = typename QIntegerForSizeof<T>::Unsigned;
0640 #if !defined(QT_NO_QOBJECT)
0641     if constexpr (QtPrivate::IsQEnumHelper<T>::Value || QtPrivate::IsQEnumHelper<Flags>::Value) {
0642         // if QFlags<T> is a Q_FLAG, we always zero-extend; if not, we need to
0643         // allow it to sign-extend if it is signed (see QMetaEnum::valueToKeys)
0644         using Int = std::conditional_t<QtPrivate::IsQEnumHelper<Flags>::Value, UInt,
0645                                        std::underlying_type_t<T>>;
0646         const QMetaObject *obj = qt_getEnumMetaObject(T());
0647         const char *name = qt_getEnumName(T());
0648         return qt_QMetaEnum_flagDebugOperator(debug, Int(flags.toInt()), obj, name);
0649     } else
0650 #endif
0651     {
0652         qt_QMetaEnum_flagDebugOperator(debug, sizeof(T), UInt(flags.toInt()));
0653         return debug;
0654     }
0655 }
0656 
0657 #ifdef Q_QDOC
0658 template <typename T>
0659 QDebug operator<<(QDebug debug, const QFlags<T> &flags);
0660 #endif // Q_QDOC
0661 
0662 #if !defined(QT_NO_QOBJECT) && !defined(Q_QDOC)
0663 // Debugging of plain enums. There are three cases:
0664 //  1) the enum is part of a Q_DECLARE_FLAGS and there's a Q_FLAG for that
0665 //     -> debugs as that QFlags (even if a Q_ENUM is present)
0666 //  2) the enum is declared a Q_ENUM but is not part of a Q_DECLARE_FLAGS
0667 //     -> debugs via qt_QMetaEnum_debugOperator()
0668 //  3) the enum is not associated with a QMetaObject
0669 //     -> no streaming
0670 // To avoid ambiguity in overload resolution, the template conditions are
0671 // mutually exclusive.
0672 
0673 namespace QtPrivate {
0674 template <typename T, bool IsEnum = std::is_enum_v<T>, bool = sizeof(T) <= sizeof(quint64)>
0675 struct EnumHasQFlag { static constexpr bool Value = false; };
0676 template <typename T> struct EnumHasQFlag<T, true, true> : QtPrivate::IsQEnumHelper<QFlags<T>> {};
0677 
0678 template <typename T, bool IsEnum = std::is_enum_v<T>, bool HasQFlag = EnumHasQFlag<T>::Value>
0679 struct EnumHasQEnum { static constexpr bool Value = false; };
0680 template <typename T> struct EnumHasQEnum<T, true, false> : QtPrivate::IsQEnumHelper<T> {};
0681 }
0682 
0683 template <typename T>
0684 std::enable_if_t<QtPrivate::EnumHasQFlag<T>::Value, QDebug> // case 1
0685 operator<<(QDebug debug, T flag)
0686 {
0687     return debug << QFlags(flag);
0688 }
0689 
0690 template<typename T>
0691 std::enable_if_t<QtPrivate::EnumHasQEnum<T>::Value, QDebug> // case 2
0692 operator<<(QDebug dbg, T value)
0693 {
0694     const QMetaObject *obj = qt_getEnumMetaObject(value);
0695     const char *name = qt_getEnumName(value);
0696     return qt_QMetaEnum_debugOperator(dbg, static_cast<typename std::underlying_type<T>::type>(value), obj, name);
0697 }
0698 #endif // !QT_NO_QOBJECT && !Q_QDOC
0699 
0700 inline QDebug operator<<(QDebug debug, QKeyCombination combination)
0701 {
0702     QDebugStateSaver saver(debug);
0703     debug.nospace() << "QKeyCombination("
0704                     << combination.keyboardModifiers()
0705                     << ", "
0706                     << combination.key()
0707                     << ")";
0708     return debug;
0709 }
0710 
0711 #ifdef Q_OS_DARWIN
0712 
0713 // We provide QDebug stream operators for commonly used Core Foundation
0714 // and Core Graphics types, as well as NSObject. Additional CF/CG types
0715 // may be added by the user, using Q_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE.
0716 
0717 #define QT_FOR_EACH_CORE_FOUNDATION_TYPE(F) \
0718     F(CFArray) \
0719     F(CFURL) \
0720     F(CFData) \
0721     F(CFNumber) \
0722     F(CFDictionary) \
0723     F(CFLocale) \
0724     F(CFDate) \
0725     F(CFBoolean) \
0726     F(CFTimeZone) \
0727 
0728 #define QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(F) \
0729     F(CFError) \
0730     F(CFBundle) \
0731 
0732 #define QT_FOR_EACH_CORE_GRAPHICS_TYPE(F) \
0733     F(CGPath) \
0734 
0735 #define QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(F) \
0736     F(CGColorSpace) \
0737     F(CGImage) \
0738     F(CGFont) \
0739     F(CGColor) \
0740 
0741 #define QT_FORWARD_DECLARE_CF_TYPE(type) Q_FORWARD_DECLARE_CF_TYPE(type);
0742 #define QT_FORWARD_DECLARE_MUTABLE_CF_TYPE(type) Q_FORWARD_DECLARE_MUTABLE_CF_TYPE(type);
0743 #define QT_FORWARD_DECLARE_CG_TYPE(type) Q_FORWARD_DECLARE_CG_TYPE(type);
0744 #define QT_FORWARD_DECLARE_MUTABLE_CG_TYPE(type) Q_FORWARD_DECLARE_MUTABLE_CG_TYPE(type);
0745 
0746 QT_END_NAMESPACE
0747 Q_FORWARD_DECLARE_CF_TYPE(CFString);
0748 struct objc_object;
0749 Q_FORWARD_DECLARE_OBJC_CLASS(NSObject);
0750 QT_FOR_EACH_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_CF_TYPE)
0751 QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_MUTABLE_CF_TYPE)
0752 QT_FOR_EACH_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_CG_TYPE)
0753 QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_MUTABLE_CG_TYPE)
0754 QT_BEGIN_NAMESPACE
0755 
0756 #define QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE(CFType) \
0757     Q_CORE_EXPORT QDebug operator<<(QDebug, CFType##Ref);
0758 
0759 #define Q_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE(CFType) \
0760     QDebug operator<<(QDebug debug, CFType##Ref ref) \
0761     { \
0762         if (!ref) \
0763             return debug << QT_STRINGIFY(CFType) "Ref(0x0)"; \
0764         if (CFStringRef description = CFCopyDescription(ref)) { \
0765             QDebugStateSaver saver(debug); \
0766             debug.noquote() << description; \
0767             CFRelease(description); \
0768         } \
0769         return debug; \
0770     }
0771 
0772 // Defined in qcore_mac_objc.mm
0773 #if defined(__OBJC__)
0774 Q_CORE_EXPORT QDebug operator<<(QDebug, id);
0775 #endif
0776 Q_CORE_EXPORT QDebug operator<<(QDebug, objc_object *);
0777 Q_CORE_EXPORT QDebug operator<<(QDebug, const NSObject *);
0778 Q_CORE_EXPORT QDebug operator<<(QDebug, CFStringRef);
0779 
0780 QT_FOR_EACH_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
0781 QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
0782 QT_FOR_EACH_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
0783 QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
0784 
0785 #undef QT_FORWARD_DECLARE_CF_TYPE
0786 #undef QT_FORWARD_DECLARE_MUTABLE_CF_TYPE
0787 #undef QT_FORWARD_DECLARE_CG_TYPE
0788 #undef QT_FORWARD_DECLARE_MUTABLE_CG_TYPE
0789 
0790 #endif // Q_OS_DARWIN
0791 
0792 QT_END_NAMESPACE
0793 
0794 #endif // QDEBUG_H