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_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical reason:data-parsing
4
5#ifndef QDEBUG_P_H
6#define QDEBUG_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of other Qt classes. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/private/qglobal_p.h>
20#include "QtCore/qdebug.h"
21#include "QtCore/qmetaobject.h"
22#include "QtCore/qflags.h"
23#include "QtCore/qbytearray.h"
24
25QT_BEGIN_NAMESPACE
26
27class QRect;
28
29namespace QtDebugUtils {
30
31Q_CORE_EXPORT QByteArray toPrintable(const char *data, qint64 len, qsizetype maxSize);
32
33// inline helpers for formatting basic classes.
34
35template <class Point>
36static inline void formatQPoint(QDebug &debug, const Point &point)
37{
38 debug << point.x() << ',' << point.y();
39}
40
41template <class Size>
42static inline void formatQSize(QDebug &debug, const Size &size)
43{
44 debug << size.width() << ", " << size.height();
45}
46
47template <class Rect>
48static inline void formatQRect(QDebug &debug, const Rect &rect)
49{
50 debug << rect.x() << ',' << rect.y() << ' ';
51 if constexpr (std::is_same_v<Rect, QRect>) {
52 // QRect may overflow. Calculate width and height in higher precision.
53 const qint64 w = qint64(rect.right()) - rect.left() + 1;
54 const qint64 h = qint64(rect.bottom()) - rect.top() + 1;
55 debug << w << 'x' << h;
56
57 constexpr qint64 M = (std::numeric_limits<int>::max)();
58 if (w > M || h > M)
59 debug << " (oversized)";
60 } else {
61 debug << rect.width() << 'x' << rect.height();
62 }
63}
64
65template <class Margins>
66static inline void formatQMargins(QDebug &debug, const Margins &margins)
67{
68 debug << margins.left() << ", " << margins.top() << ", " << margins.right()
69 << ", " << margins.bottom();
70}
71
72#ifndef QT_NO_QOBJECT
73template <class QEnum>
74static inline void formatQEnum(QDebug &debug, QEnum value)
75{
76 const QMetaObject *metaObject = qt_getEnumMetaObject(value);
77 const QMetaEnum me = metaObject->enumerator(metaObject->indexOfEnumerator(qt_getEnumName(value)));
78 if (const char *key = me.valueToKey(int(value)))
79 debug << key;
80 else
81 debug << int(value);
82}
83
84template <class QEnum>
85static inline void formatNonNullQEnum(QDebug &debug, const char *prefix, QEnum value)
86{
87 if (value) {
88 debug << prefix;
89 formatQEnum(debug, value);
90 }
91}
92
93template <class Enum>
94static inline void formatQFlags(QDebug &debug, const QFlags<Enum> &value)
95{
96 const QMetaEnum me = QMetaEnum::fromType<QFlags<Enum>>();
97 const QDebugStateSaver saver(debug);
98 debug.noquote();
99 debug << me.valueToKeys(value.toInt());
100}
101
102template <class Enum>
103static inline void formatNonNullQFlags(QDebug &debug, const char *prefix, const QFlags<Enum> &value)
104{
105 if (value) {
106 debug << prefix;
107 formatQFlags(debug, value);
108 }
109}
110
111#endif // !QT_NO_QOBJECT
112
113} // namespace QtDebugUtils
114
115QT_END_NAMESPACE
116
117#endif // QDEBUG_P_H
static void formatNonNullQEnum(QDebug &debug, const char *prefix, QEnum value)
Definition qdebug_p.h:85
static void formatQEnum(QDebug &debug, QEnum value)
Definition qdebug_p.h:74
static void formatQRect(QDebug &debug, const Rect &rect)
Definition qdebug_p.h:48
static void formatQMargins(QDebug &debug, const Margins &margins)
Definition qdebug_p.h:66
static void formatQFlags(QDebug &debug, const QFlags< Enum > &value)
Definition qdebug_p.h:94
static void formatQSize(QDebug &debug, const Size &size)
Definition qdebug_p.h:42
static void formatQPoint(QDebug &debug, const Point &point)
Definition qdebug_p.h:36
static void formatNonNullQFlags(QDebug &debug, const char *prefix, const QFlags< Enum > &value)
Definition qdebug_p.h:103