Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdebug.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:critical reason:data-parsing
5
6#ifndef QDEBUG_H
7#define QDEBUG_H
8
9#if 0
10#pragma qt_class(QtDebug)
11#endif
12
13#include <QtCore/qcompare.h>
14#include <QtCore/qcontainerfwd.h>
15#include <QtCore/qfloat16.h>
16#include <QtCore/qtextstream.h>
17#include <QtCore/qttypetraits.h>
18#include <QtCore/qtypes.h>
19#include <QtCore/qstring.h>
20#include <QtCore/qcontiguouscache.h>
21#include <QtCore/qsharedpointer.h>
22
23// all these have already been included by various headers above, but don't rely on indirect includes:
24#include <array>
25#include <chrono>
26#include <list>
27#include <map>
28#include <memory>
29#include <optional>
30#include <string>
31#include <string_view>
32#include <set>
33#include <system_error>
34#include <tuple>
35#include <QtCore/q20type_traits.h>
36#include <utility>
37#include <unordered_map>
38#include <unordered_set>
39#include <vector>
40
41#if !defined(QT_LEAN_HEADERS) || QT_LEAN_HEADERS < 1
42# include <QtCore/qlist.h>
43# include <QtCore/qmap.h>
44# include <QtCore/qset.h>
45# include <QtCore/qvarlengtharray.h>
46#endif
47
48#ifdef Q_OS_DARWIN
49Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
50#endif
51
52QT_BEGIN_NAMESPACE
53
54class QT6_ONLY(Q_CORE_EXPORT) QDebug : public QIODeviceBase
55{
56 friend class QMessageLogger;
57 friend class QDebugStateSaver;
58 friend class QDebugStateSaverPrivate;
59 struct Stream {
60 enum { VerbosityShift = 29, VerbosityMask = 0x7 };
61
62 explicit Stream(QIODevice *device)
63 : ts(device)
64 {}
65 explicit Stream(QString *string)
66 : ts(string, WriteOnly)
67 {}
68 explicit Stream(QByteArray *ba)
69 : ts(ba, WriteOnly)
70 {}
71 explicit Stream(QtMsgType t)
72 : ts(&buffer, WriteOnly),
73 type(t),
74 message_output(true)
75 {}
76 QTextStream ts;
77 QString buffer;
78 int ref = 1;
79 QtMsgType type = QtDebugMsg;
80 bool space = true;
81 bool noQuotes = false;
82 bool message_output = false;
83 int verbosity = DefaultVerbosity;
84 QMessageLogContext context;
85 } *stream;
86
87 enum Latin1Content { ContainsBinary = 0, ContainsLatin1 };
88
89 QT7_ONLY(Q_CORE_EXPORT) void putUcs4(uint ucs4);
90 QT7_ONLY(Q_CORE_EXPORT) void putString(const QChar *begin, size_t length);
91 QT7_ONLY(Q_CORE_EXPORT) void putByteArray(const char *begin, size_t length, Latin1Content content);
92 QT7_ONLY(Q_CORE_EXPORT) void putTimeUnit(qint64 num, qint64 den);
93 QT7_ONLY(Q_CORE_EXPORT) void putInt128(const void *i);
94 QT7_ONLY(Q_CORE_EXPORT) void putUInt128(const void *i);
95 QT7_ONLY(Q_CORE_EXPORT) void putQtOrdering(QtOrderingPrivate::QtOrderingTypeFlag flags,
96 Qt::partial_ordering order);
97
98 template <typename...Ts>
99 using if_streamable = std::enable_if_t<
100 std::conjunction_v<QTypeTraits::has_ostream_operator<QDebug, Ts>...>
101 , bool>;
102public:
103 explicit QDebug(QIODevice *device) : stream(new Stream(device)) {}
104 explicit QDebug(QString *string) : stream(new Stream(string)) {}
105 explicit QDebug(QByteArray *bytes) : stream(new Stream(bytes)) {}
106 explicit QDebug(QtMsgType t) : stream(new Stream(t)) {}
107 QDebug(const QDebug &o) : stream(o.stream) { ++stream->ref; }
108 QDebug(QDebug &&other) noexcept : stream{std::exchange(other.stream, nullptr)} {}
109 inline QDebug &operator=(const QDebug &other);
110 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QDebug)
111 ~QDebug();
112 void swap(QDebug &other) noexcept { qt_ptr_swap(stream, other.stream); }
113
114 QT7_ONLY(Q_CORE_EXPORT) QDebug &resetFormat();
115
116 inline QDebug &space() { stream->space = true; stream->ts << ' '; return *this; }
117 inline QDebug &nospace() { stream->space = false; return *this; }
118 inline QDebug &maybeSpace() { if (stream->space) stream->ts << ' '; return *this; }
119 inline QDebug &verbosity(int verbosityLevel) { stream->verbosity = verbosityLevel; return *this; }
120 int verbosity() const { return stream->verbosity; }
121 void setVerbosity(int verbosityLevel) { stream->verbosity = verbosityLevel; }
122 enum VerbosityLevel { MinimumVerbosity = 0, DefaultVerbosity = 2, MaximumVerbosity = 7 };
123
124 bool autoInsertSpaces() const { return stream->space; }
125 void setAutoInsertSpaces(bool b) { stream->space = b; }
126
127 [[nodiscard]] bool quoteStrings() const noexcept { return !stream->noQuotes; }
128 void setQuoteStrings(bool b) { stream->noQuotes = !b; }
129
130 inline QDebug &quote() { stream->noQuotes = false; return *this; }
131 inline QDebug &noquote() { stream->noQuotes = true; return *this; }
132 inline QDebug &maybeQuote(char c = '"') { if (!stream->noQuotes) stream->ts << c; return *this; }
133
134 inline QDebug &operator<<(QChar t) { putUcs4(t.unicode()); return maybeSpace(); }
135 inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); }
136 inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); }
137 inline QDebug &operator<<(signed short t) { stream->ts << t; return maybeSpace(); }
138 inline QDebug &operator<<(unsigned short t) { stream->ts << t; return maybeSpace(); }
139 inline QDebug &operator<<(char16_t t) { return *this << QChar(t); }
140 inline QDebug &operator<<(char32_t t) { putUcs4(t); return maybeSpace(); }
141 inline QDebug &operator<<(signed int t) { stream->ts << t; return maybeSpace(); }
142 inline QDebug &operator<<(unsigned int t) { stream->ts << t; return maybeSpace(); }
143 inline QDebug &operator<<(signed long t) { stream->ts << t; return maybeSpace(); }
144 inline QDebug &operator<<(unsigned long t) { stream->ts << t; return maybeSpace(); }
145 inline QDebug &operator<<(qint64 t) { stream->ts << t; return maybeSpace(); }
146 inline QDebug &operator<<(quint64 t) { stream->ts << t; return maybeSpace(); }
147 inline QDebug &operator<<(qfloat16 t) { stream->ts << t; return maybeSpace(); }
148 inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); }
149 inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); }
150 inline QDebug &operator<<(const char* t) { stream->ts << QString::fromUtf8(t); return maybeSpace(); }
151 inline QDebug &operator<<(const char16_t *t) { stream->ts << QStringView(t); return maybeSpace(); }
152 inline QDebug &operator<<(const QString & t) { putString(t.constData(), size_t(t.size())); return maybeSpace(); }
153 inline QDebug &operator<<(QStringView s) { putString(s.data(), size_t(s.size())); return maybeSpace(); }
154 inline QDebug &operator<<(QUtf8StringView s) { putByteArray(reinterpret_cast<const char*>(s.data()), s.size(), ContainsBinary); return maybeSpace(); }
155 inline QDebug &operator<<(QLatin1StringView t) { putByteArray(t.latin1(), t.size(), ContainsLatin1); return maybeSpace(); }
156 inline QDebug &operator<<(const QByteArray & t) { putByteArray(t.constData(), t.size(), ContainsBinary); return maybeSpace(); }
157 inline QDebug &operator<<(QByteArrayView t) { putByteArray(t.constData(), t.size(), ContainsBinary); return maybeSpace(); }
158 inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); }
159 inline QDebug &operator<<(std::nullptr_t) { stream->ts << "(nullptr)"; return maybeSpace(); }
160 inline QDebug &operator<<(std::nullopt_t) { stream->ts << "nullopt"; return maybeSpace(); }
161 inline QDebug &operator<<(QTextStreamFunction f) {
162 stream->ts << f;
163 return *this;
164 }
165
166 inline QDebug &operator<<(QTextStreamManipulator m)
167 { stream->ts << m; return *this; }
168
169#ifdef Q_QDOC
170 template <typename Char, typename...Args>
171 QDebug &operator<<(const std::basic_string<Char, Args...> &s);
172
173 template <typename Char, typename...Args>
174 QDebug &operator<<(std::basic_string_view<Char, Args...> s);
175#else
176 template <typename...Args>
177 QDebug &operator<<(const std::basic_string<char, Args...> &s)
178 { return *this << QUtf8StringView(s); }
179
180 template <typename...Args>
181 QDebug &operator<<(std::basic_string_view<char, Args...> s)
182 { return *this << QUtf8StringView(s); }
183
184#ifdef __cpp_char8_t
185 template <typename...Args>
186 QDebug &operator<<(const std::basic_string<char8_t, Args...> &s)
187 { return *this << QUtf8StringView(s); }
188
189 template <typename...Args>
190 QDebug &operator<<(std::basic_string_view<char8_t, Args...> s)
191 { return *this << QUtf8StringView(s); }
192#endif // __cpp_char8_t
193
194 template <typename...Args>
195 QDebug &operator<<(const std::basic_string<char16_t, Args...> &s)
196 { return *this << QStringView(s); }
197
198 template <typename...Args>
199 QDebug &operator<<(std::basic_string_view<char16_t, Args...> s)
200 { return *this << QStringView(s); }
201
202 template <typename...Args>
203 QDebug &operator<<(const std::basic_string<wchar_t, Args...> &s)
204 {
205 if constexpr (sizeof(wchar_t) == 2)
206 return *this << QStringView(s);
207 else
208 return *this << QString::fromWCharArray(s.data(), s.size()); // ### optimize
209 }
210
211 template <typename...Args>
212 QDebug &operator<<(std::basic_string_view<wchar_t, Args...> s)
213 {
214 if constexpr (sizeof(wchar_t) == 2)
215 return *this << QStringView(s);
216 else
217 return *this << QString::fromWCharArray(s.data(), s.size()); // ### optimize
218 }
219
220 template <typename...Args>
221 QDebug &operator<<(const std::basic_string<char32_t, Args...> &s)
222 { return *this << QString::fromUcs4(s.data(), s.size()); }
223
224 template <typename...Args>
225 QDebug &operator<<(std::basic_string_view<char32_t, Args...> s)
226 { return *this << QString::fromUcs4(s.data(), s.size()); }
227#endif // !Q_QDOC
228
229 template <typename Rep, typename Period>
230 QDebug &operator<<(std::chrono::duration<Rep, Period> duration)
231 {
232 stream->ts << duration.count();
233 putTimeUnit(Period::num, Period::den);
234 return maybeSpace();
235 }
236
237#ifdef QT_SUPPORTS_INT128
238private:
239 // Constrained templates so they only match q(u)int128 without conversions.
240 // Also keeps these operators out of the ABI.
241 template <typename T>
242 using if_qint128 = std::enable_if_t<std::is_same_v<T, qint128>, bool>;
243 template <typename T>
244 using if_quint128 = std::enable_if_t<std::is_same_v<T, quint128>, bool>;
245public:
246 template <typename T, if_qint128<T> = true>
247 QDebug &operator<<(T i128) { putInt128(&i128); return maybeSpace(); }
248 template <typename T, if_quint128<T> = true>
249 QDebug &operator<<(T u128) { putUInt128(&u128); return maybeSpace(); }
250#endif // QT_SUPPORTS_INT128
251
252private:
253 template <typename T>
254 static void streamTypeErased(QDebug &d, const void *obj)
255 {
256 d << *static_cast<const T*>(obj);
257 }
258 using StreamTypeErased = void(*)(QDebug&, const void*);
259 QT7_ONLY(Q_CORE_EXPORT) static QString toStringImpl(StreamTypeErased s, const void *obj);
260 QT7_ONLY(Q_CORE_EXPORT) static QByteArray toBytesImpl(StreamTypeErased s, const void *obj);
261 QT7_ONLY(Q_CORE_EXPORT) QDebug &putTupleLikeImplImpl(const char *ns, const char *what, size_t n,
262 StreamTypeErased *ops, const void **data);
263
264 template <typename TupleLike, size_t...Is>
265 QDebug &putTupleLikeImpl(const char *ns, const char *what, const TupleLike &t,
266 std::index_sequence<Is...>)
267 {
268 if constexpr (sizeof...(Is)) {
269 StreamTypeErased ops[] = {
270 &streamTypeErased<q20::remove_cvref_t<std::tuple_element_t<Is, TupleLike>>>...
271 };
272 const void *data[] = {
273 std::addressof(std::get<Is>(t))...
274 };
275 return putTupleLikeImplImpl(ns, what, sizeof...(Is), ops, data);
276 } else {
277 return putTupleLikeImplImpl(ns, what, 0, nullptr, nullptr);
278 }
279 }
280
281 template <typename TupleLike>
282 QDebug &putTupleLike(const char *ns, const char *what, const TupleLike &t)
283 {
284 using Indexes = std::make_index_sequence<std::tuple_size_v<TupleLike>>;
285 return putTupleLikeImpl(ns, what, t, Indexes{});
286 }
287public:
288 template <typename T>
289 static QString toString(const T &object)
290 {
291 return toStringImpl(&streamTypeErased<T>, std::addressof(object));
292 }
293
294 template <typename T>
295 static QByteArray toBytes(const T &object)
296 {
297 return toBytesImpl(&streamTypeErased<T>, std::addressof(object));
298 }
299
300 template <typename...Ts, if_streamable<Ts...> = true>
301 QDebug &operator<<(const std::tuple<Ts...> &t)
302 {
303 return putTupleLike("std", "tuple", t);
304 }
305
306 template <typename T, if_streamable<T> = true>
307 QDebug &operator<<(const std::optional<T> &o)
308 {
309 if (!o)
310 return *this << std::nullopt;
311 StreamTypeErased s = &streamTypeErased<std::remove_cv_t<T>>;
312 const void *d = std::addressof(*o);
313 return putTupleLikeImplImpl("std", "optional", 1, &s, &d);
314 }
315
316private:
317 template <typename T>
318 using if_ordering_type = std::enable_if_t<QtOrderingPrivate::is_ordering_type_v<T>, bool>;
319
320 template <typename T, if_ordering_type<T> = true>
321 friend QDebug operator<<(QDebug debug, T t)
322 {
323 debug.putQtOrdering(QtOrderingPrivate::orderingFlagsFor(t), Qt::partial_ordering(t));
324 return debug;
325 }
326
327 Q_CORE_EXPORT friend QDebug operator<<(QDebug debug, const std::error_code &code);
328 Q_CORE_EXPORT friend QDebug operator<<(QDebug debug, const std::error_condition &condition);
329
330#ifdef Q_OS_DARWIN
331 Q_CORE_EXPORT friend QDebug operator<<(QDebug, const NSString *);
332#endif
333};
334
336
338class QDebugStateSaver
339{
340public:
341 Q_NODISCARD_CTOR Q_CORE_EXPORT
342 QDebugStateSaver(QDebug &dbg);
343 Q_CORE_EXPORT
344 ~QDebugStateSaver();
345private:
346 Q_DISABLE_COPY(QDebugStateSaver)
347 std::unique_ptr<QDebugStateSaverPrivate> d;
348};
349
350class QNoDebug
351{
352public:
353 inline QNoDebug &operator<<(QTextStreamFunction) { return *this; }
354 inline QNoDebug &operator<<(QTextStreamManipulator) { return *this; }
355 inline QNoDebug &space() { return *this; }
356 inline QNoDebug &nospace() { return *this; }
357 inline QNoDebug &maybeSpace() { return *this; }
358 inline QNoDebug &quote() { return *this; }
359 inline QNoDebug &noquote() { return *this; }
360 inline QNoDebug &maybeQuote(const char = '"') { return *this; }
361 inline QNoDebug &verbosity(int) { return *this; }
362
363 template<typename T>
364 inline QNoDebug &operator<<(const T &) { return *this; }
365};
366
367QNoDebug QMessageLogger::noDebug(...) const noexcept
368{ return {}; }
369
370inline QDebug &QDebug::operator=(const QDebug &other)
371{
372 QDebug{other}.swap(*this);
373 return *this;
374}
375
376namespace QtPrivate {
377
378template <typename SequentialContainer>
379inline QDebug printSequentialContainer(QDebug debug, const char *which, const SequentialContainer &c)
380{
381 const QDebugStateSaver saver(debug);
382 debug.nospace() << which << '(';
383 typename SequentialContainer::const_iterator it = c.begin(), end = c.end();
384 if (it != end) {
385 debug << *it;
386 ++it;
387 }
388 while (it != end) {
389 debug << ", " << *it;
390 ++it;
391 }
392 debug << ')';
393 return debug;
394}
395
396template <typename AssociativeContainer>
397inline QDebug printAssociativeContainer(QDebug debug, const char *which, const AssociativeContainer &c)
398{
399 const QDebugStateSaver saver(debug);
400 debug.nospace() << which << "(";
401 for (typename AssociativeContainer::const_iterator it = c.constBegin();
402 it != c.constEnd(); ++it) {
403 debug << '(' << it.key() << ", " << it.value() << ')';
404 }
405 debug << ')';
406 return debug;
407}
408
409} // namespace QtPrivate
410
411template<typename ...T>
412using QDebugIfHasDebugStream =
413 std::enable_if_t<std::conjunction_v<QTypeTraits::has_ostream_operator<QDebug, T>...>, QDebug>;
414
415template<typename Container, typename ...T>
416using QDebugIfHasDebugStreamContainer =
417 std::enable_if_t<std::conjunction_v<QTypeTraits::has_ostream_operator_container<QDebug, Container, T>...>, QDebug>;
418
419#ifndef Q_QDOC
420
421template<typename T>
422inline QDebugIfHasDebugStreamContainer<QList<T>, T> operator<<(QDebug debug, const QList<T> &vec)
423{
424 return QtPrivate::printSequentialContainer(std::move(debug), "QList", vec);
425}
426
427template<typename T, qsizetype P>
428inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const QVarLengthArray<T, P> &vec)
429{
430 return QtPrivate::printSequentialContainer(std::move(debug), "QVarLengthArray", vec);
431}
432
433template <typename T, typename Alloc>
434inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const std::vector<T, Alloc> &vec)
435{
436 return QtPrivate::printSequentialContainer(std::move(debug), "std::vector", vec);
437}
438
439template <typename T, std::size_t N>
440inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const std::array<T, N> &array)
441{
442 return QtPrivate::printSequentialContainer(std::move(debug), "std::array", array);
443}
444
445template <typename T, typename Alloc>
446inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const std::list<T, Alloc> &vec)
447{
448 return QtPrivate::printSequentialContainer(std::move(debug), "std::list", vec);
449}
450
451template <typename T>
452inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, std::initializer_list<T> list)
453{
454 return QtPrivate::printSequentialContainer(std::move(debug), "std::initializer_list", list);
455}
456
457template <typename Key, typename T, typename Compare, typename Alloc>
458inline QDebugIfHasDebugStream<Key, T> operator<<(QDebug debug, const std::map<Key, T, Compare, Alloc> &map)
459{
460 return QtPrivate::printSequentialContainer(std::move(debug), "std::map", map); // yes, sequential: *it is std::pair
461}
462
463template <typename Key, typename T, typename Compare, typename Alloc>
464inline QDebugIfHasDebugStream<Key, T> operator<<(QDebug debug, const std::multimap<Key, T, Compare, Alloc> &map)
465{
466 return QtPrivate::printSequentialContainer(std::move(debug), "std::multimap", map); // yes, sequential: *it is std::pair
467}
468
469template <typename Key, typename Compare, typename Alloc>
470inline QDebugIfHasDebugStream<Key> operator<<(QDebug debug, const std::multiset<Key, Compare, Alloc> &multiset)
471{
472 return QtPrivate::printSequentialContainer(std::move(debug), "std::multiset", multiset);
473}
474
475template <typename Key, typename Compare, typename Alloc>
476inline QDebugIfHasDebugStream<Key> operator<<(QDebug debug, const std::set<Key, Compare, Alloc> &set)
477{
478 return QtPrivate::printSequentialContainer(std::move(debug), "std::set", set);
479}
480
481template <typename Key, typename T, typename Hash, typename KeyEqual, typename Alloc>
482inline QDebugIfHasDebugStream<Key, T> operator<<(QDebug debug, const std::unordered_map<Key, T, Hash, KeyEqual, Alloc> &unordered_map)
483{
484 return QtPrivate::printSequentialContainer(std::move(debug), "std::unordered_map", unordered_map); // yes, sequential: *it is std::pair
485}
486
487template <typename Key, typename Hash, typename KeyEqual, typename Alloc>
488inline QDebugIfHasDebugStream<Key> operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc> &unordered_set)
489{
490 return QtPrivate::printSequentialContainer(std::move(debug), "std::unordered_set", unordered_set);
491}
492
493template <class Key, class T>
494inline QDebugIfHasDebugStreamContainer<QMap<Key, T>, Key, T> operator<<(QDebug debug, const QMap<Key, T> &map)
495{
496 return QtPrivate::printAssociativeContainer(std::move(debug), "QMap", map);
497}
498
499template <class Key, class T>
500inline QDebugIfHasDebugStreamContainer<QMultiMap<Key, T>, Key, T> operator<<(QDebug debug, const QMultiMap<Key, T> &map)
501{
502 return QtPrivate::printAssociativeContainer(std::move(debug), "QMultiMap", map);
503}
504
505template <class Key, class T>
506inline QDebugIfHasDebugStreamContainer<QHash<Key, T>, Key, T> operator<<(QDebug debug, const QHash<Key, T> &hash)
507{
508 return QtPrivate::printAssociativeContainer(std::move(debug), "QHash", hash);
509}
510
511template <class Key, class T>
512inline QDebugIfHasDebugStreamContainer<QMultiHash<Key, T>, Key, T> operator<<(QDebug debug, const QMultiHash<Key, T> &hash)
513{
514 return QtPrivate::printAssociativeContainer(std::move(debug), "QMultiHash", hash);
515}
516
517template <class T1, class T2>
518inline QDebugIfHasDebugStream<T1, T2> operator<<(QDebug debug, const std::pair<T1, T2> &pair)
519{
520 const QDebugStateSaver saver(debug);
521 debug.nospace() << "std::pair(" << pair.first << ", " << pair.second << ')';
522 return debug;
523}
524
525template <typename T>
526inline QDebugIfHasDebugStreamContainer<QSet<T>, T> operator<<(QDebug debug, const QSet<T> &set)
527{
528 return QtPrivate::printSequentialContainer(std::move(debug), "QSet", set);
529}
530
531template <class T>
532inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const QContiguousCache<T> &cache)
533{
534 const QDebugStateSaver saver(debug);
535 debug.nospace() << "QContiguousCache(";
536 for (qsizetype i = cache.firstIndex(); i <= cache.lastIndex(); ++i) {
537 debug << cache[i];
538 if (i != cache.lastIndex())
539 debug << ", ";
540 }
541 debug << ')';
542 return debug;
543}
544
545#else
546template <class T>
547QDebug operator<<(QDebug debug, const QList<T> &list);
548
549template <class T, qsizetype P>
550QDebug operator<<(QDebug debug, const QVarLengthArray<T, P> &array);
551
552template <typename T, typename Alloc>
553QDebug operator<<(QDebug debug, const std::vector<T, Alloc> &vec);
554
555template <typename T, std::size_t N>
556QDebug operator<<(QDebug debug, const std::array<T, N> &array);
557
558template <typename T, typename Alloc>
559QDebug operator<<(QDebug debug, const std::list<T, Alloc> &vec);
560
561template <typename Key, typename T, typename Compare, typename Alloc>
562QDebug operator<<(QDebug debug, const std::map<Key, T, Compare, Alloc> &map);
563
564template <typename Key, typename T, typename Compare, typename Alloc>
565QDebug operator<<(QDebug debug, const std::multimap<Key, T, Compare, Alloc> &map);
566
567template <class Key, class T>
568QDebug operator<<(QDebug debug, const QMap<Key, T> &map);
569
570template <class Key, class T>
571QDebug operator<<(QDebug debug, const QMultiMap<Key, T> &map);
572
573template <class Key, class T>
574QDebug operator<<(QDebug debug, const QHash<Key, T> &hash);
575
576template <class Key, class T>
577QDebug operator<<(QDebug debug, const QMultiHash<Key, T> &hash);
578
579template <typename T>
580QDebug operator<<(QDebug debug, const QSet<T> &set);
581
582template <class T1, class T2>
583QDebug operator<<(QDebug debug, const std::pair<T1, T2> &pair);
584
585template <typename T>
586QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache);
587
588template <typename Key, typename Compare, typename Alloc>
589QDebug operator<<(QDebug debug, const std::multiset<Key, Compare, Alloc> &multiset);
590
591template <typename Key, typename Compare, typename Alloc>
592QDebug operator<<(QDebug debug, const std::set<Key, Compare, Alloc> &set);
593
594template <typename Key, typename T, typename Hash, typename KeyEqual, typename Alloc>
595QDebug operator<<(QDebug debug, const std::unordered_map<Key, T, Hash, KeyEqual, Alloc> &unordered_map);
596
597template <typename Key, typename Hash, typename KeyEqual, typename Alloc>
598QDebug operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc> &unordered_set);
599
600#endif // Q_QDOC
601
602template <class T>
603inline QDebug operator<<(QDebug debug, const QSharedPointer<T> &ptr)
604{
605 QDebugStateSaver saver(debug);
606 debug.nospace() << "QSharedPointer(" << ptr.data() << ")";
607 return debug;
608}
609
610template <typename T, typename Tag> class QTaggedPointer;
611
612template <typename T, typename Tag>
613inline QDebug operator<<(QDebug debug, const QTaggedPointer<T, Tag> &ptr)
614{
615 QDebugStateSaver saver(debug);
616 debug.nospace() << "QTaggedPointer(" << ptr.pointer() << ", " << ptr.tag() << ")";
617 return debug;
618}
619
620Q_CORE_EXPORT QDebug qt_QMetaEnum_debugOperator(QDebug&, qint64 value, const QMetaObject *meta, const char *name);
621Q_CORE_EXPORT QDebug qt_QMetaEnum_flagDebugOperator(QDebug &dbg, quint64 value, const QMetaObject *meta, const char *name);
622Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, uint value);
623Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, quint64 value);
624
625template <typename Int>
626void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, Int value)
627{
628 static_assert(std::is_unsigned_v<Int>,
629 "Cast value to an unsigned type before calling this function");
630 const QDebugStateSaver saver(debug);
631 debug.resetFormat();
632 debug.nospace() << "QFlags(" << Qt::hex << Qt::showbase;
633 bool needSeparator = false;
634 for (size_t i = 0; i < sizeofT * 8; ++i) {
635 if (value & (Int(1) << i)) {
636 if (needSeparator)
637 debug << '|';
638 else
639 needSeparator = true;
640 debug << (Int(1) << i);
641 }
642 }
643 debug << ')';
644}
645
646template <class Flags,
647 std::enable_if_t<QtPrivate::IsQFlags<Flags>::value, bool> = true>
648inline QDebug operator<<(QDebug debug, Flags flags)
649{
650 using T = typename Flags::enum_type;
651 using UInt = typename QIntegerForSizeof<T>::Unsigned;
652#if !defined(QT_NO_QOBJECT)
653 if constexpr (QtPrivate::IsQEnumHelper<T>::Value || QtPrivate::IsQEnumHelper<Flags>::Value) {
654 // if QFlags<T> is a Q_FLAG, we always zero-extend; if not, we need to
655 // allow it to sign-extend if it is signed (see QMetaEnum::valueToKeys)
656 using Int = std::conditional_t<QtPrivate::IsQEnumHelper<Flags>::Value, UInt,
657 std::underlying_type_t<T>>;
658 const QMetaObject *obj = qt_getEnumMetaObject(T());
659 const char *name = qt_getEnumName(T());
660 return qt_QMetaEnum_flagDebugOperator(debug, Int(flags.toInt()), obj, name);
661 } else
662#endif
663 {
664 qt_QMetaEnum_flagDebugOperator(debug, sizeof(T), UInt(flags.toInt()));
665 return debug;
666 }
667}
668
669#ifdef Q_QDOC
670template <typename T>
671QDebug operator<<(QDebug debug, const QFlags<T> &flags);
672#endif // Q_QDOC
673
674#if !defined(QT_NO_QOBJECT) && !defined(Q_QDOC)
675// Debugging of plain enums. There are three cases:
676// 1) the enum is part of a Q_DECLARE_FLAGS and there's a Q_FLAG for that
677// -> debugs as that QFlags (even if a Q_ENUM is present)
678// 2) the enum is declared a Q_ENUM but is not part of a Q_DECLARE_FLAGS
679// -> debugs via qt_QMetaEnum_debugOperator()
680// 3) the enum is not associated with a QMetaObject
681// -> no streaming
682// To avoid ambiguity in overload resolution, the template conditions are
683// mutually exclusive.
684
685namespace QtPrivate {
686template <typename T, bool IsEnum = std::is_enum_v<T>, bool = sizeof(T) <= sizeof(quint64)>
687struct EnumHasQFlag { static constexpr bool Value = false; };
688template <typename T> struct EnumHasQFlag<T, true, true> : QtPrivate::IsQEnumHelper<QFlags<T>> {};
689
690template <typename T, bool IsEnum = std::is_enum_v<T>, bool HasQFlag = EnumHasQFlag<T>::Value>
691struct EnumHasQEnum { static constexpr bool Value = false; };
692template <typename T> struct EnumHasQEnum<T, true, false> : QtPrivate::IsQEnumHelper<T> {};
693}
694
695template <typename T>
696std::enable_if_t<QtPrivate::EnumHasQFlag<T>::Value, QDebug> // case 1
697operator<<(QDebug debug, T flag)
698{
699 return debug << QFlags(flag);
700}
701
702template<typename T>
703std::enable_if_t<QtPrivate::EnumHasQEnum<T>::Value, QDebug> // case 2
704operator<<(QDebug dbg, T value)
705{
706 const QMetaObject *obj = qt_getEnumMetaObject(value);
707 const char *name = qt_getEnumName(value);
708 return qt_QMetaEnum_debugOperator(dbg, static_cast<typename std::underlying_type<T>::type>(value), obj, name);
709}
710#endif // !QT_NO_QOBJECT && !Q_QDOC
711
712inline QDebug operator<<(QDebug debug, QKeyCombination combination)
713{
714 QDebugStateSaver saver(debug);
715 debug.nospace() << "QKeyCombination("
716 << combination.keyboardModifiers()
717 << ", "
718 << combination.key()
719 << ")";
720 return debug;
721}
722
723#ifdef Q_OS_DARWIN
724
725// We provide QDebug stream operators for commonly used Core Foundation
726// and Core Graphics types, as well as NSObject. Additional CF/CG types
727// may be added by the user, using Q_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE.
728
729#define QT_FOR_EACH_CORE_FOUNDATION_TYPE(F)
730 F(CFArray)
731 F(CFURL)
732 F(CFData)
733 F(CFNumber)
734 F(CFDictionary)
735 F(CFLocale)
736 F(CFDate)
737 F(CFBoolean)
738 F(CFTimeZone)
739
740#define QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(F)
741 F(CFError)
742 F(CFBundle)
743
744#define QT_FOR_EACH_CORE_GRAPHICS_TYPE(F)
745 F(CGPath)
746
747#define QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(F)
748 F(CGColorSpace)
749 F(CGImage)
750 F(CGFont)
751 F(CGColor)
752
753#define QT_FORWARD_DECLARE_CF_TYPE(type) Q_FORWARD_DECLARE_CF_TYPE(type);
754#define QT_FORWARD_DECLARE_MUTABLE_CF_TYPE(type) Q_FORWARD_DECLARE_MUTABLE_CF_TYPE(type);
755#define QT_FORWARD_DECLARE_CG_TYPE(type) Q_FORWARD_DECLARE_CG_TYPE(type);
756#define QT_FORWARD_DECLARE_MUTABLE_CG_TYPE(type) Q_FORWARD_DECLARE_MUTABLE_CG_TYPE(type);
757
758QT_END_NAMESPACE
759Q_FORWARD_DECLARE_CF_TYPE(CFString);
760struct objc_object;
761Q_FORWARD_DECLARE_OBJC_CLASS(NSObject);
762QT_FOR_EACH_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_CF_TYPE)
763QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_MUTABLE_CF_TYPE)
764QT_FOR_EACH_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_CG_TYPE)
765QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_MUTABLE_CG_TYPE)
766QT_BEGIN_NAMESPACE
767
768#define QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE(CFType)
769 Q_CORE_EXPORT QDebug operator<<(QDebug, CFType##Ref);
770
771#define Q_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE(CFType)
772 QDebug operator<<(QDebug debug, CFType##Ref ref)
773 {
774 if (!ref)
775 return debug << QT_STRINGIFY(CFType) "Ref(0x0)";
776 if (CFStringRef description = CFCopyDescription(ref)) {
777 QDebugStateSaver saver(debug);
778 debug.noquote() << description;
779 CFRelease(description);
780 }
781 return debug;
782 }
783
784// Defined in qcore_mac_objc.mm
785#if defined(__OBJC__)
786Q_CORE_EXPORT QDebug operator<<(QDebug, id);
787#endif
788Q_CORE_EXPORT QDebug operator<<(QDebug, objc_object *);
789Q_CORE_EXPORT QDebug operator<<(QDebug, const NSObject *);
790Q_CORE_EXPORT QDebug operator<<(QDebug, CFStringRef);
791
792QT_FOR_EACH_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
793QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
794QT_FOR_EACH_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
795QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
796
797#undef QT_FORWARD_DECLARE_CF_TYPE
798#undef QT_FORWARD_DECLARE_MUTABLE_CF_TYPE
799#undef QT_FORWARD_DECLARE_CG_TYPE
800#undef QT_FORWARD_DECLARE_MUTABLE_CG_TYPE
801
802#endif // Q_OS_DARWIN
803
804QT_END_NAMESPACE
805
806#endif // QDEBUG_H
QImageIOHandler * create(QIODevice *device, const QByteArray &format=QByteArray()) const override
Creates and returns a QImageIOHandler subclass, with device and format set.
Definition main.cpp:41
Combined button and popup list for selecting options.
void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, uint value)
Definition qdebug.cpp:1465
QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2620
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2620
static constexpr bool Value
Definition qdebug.h:691
static constexpr bool Value
Definition qdebug.h:687