Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmldomstringdumper_p.h
Go to the documentation of this file.
1// Copyright (C) 2020 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
4#ifndef DUMPER_H
5#define DUMPER_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qqmldom_global.h"
19#include "qqmldomconstants_p.h"
21
22#include <QtCore/QString>
23#include <QtCore/QStringView>
24#include <QtCore/QDebug>
25
26#include <type_traits>
27
29
30namespace QQmlJS {
31namespace Dom {
32
34using SinkF = std::function<void(QStringView)>;
35using DumperFunction = std::function<void(Sink)>;
36
37class Dumper{
38public:
40private:
41 // We want to avoid the limit of one user conversion:
42 // after doing (* -> QStringView) we cannot have QStringView -> Dumper, as it
43 // would be the second user defined conversion.
44 // For a similar reason we have a template to accept function_ref<void(Sink)> .
45 // The end result is that void f(Dumper) can be called nicely, and avoid overloads:
46 // f(u"bla"), f(QLatin1String("bla")), f(QString()), f([](Sink s){...}),...
47 template <typename T>
48 using if_compatible_dumper = typename
49 std::enable_if<std::is_convertible<T, DumperFunction>::value, bool>::type;
50
51 template<typename T>
52 using if_string_view_convertible = typename
53 std::enable_if<std::is_convertible_v<T, QStringView>, bool>::type;
54
55public:
57 dumper([s](Sink sink){ sink(s); }) {}
58
59 Dumper(std::nullptr_t): Dumper(QStringView(nullptr)) {}
60
61 template <typename Stringy, if_string_view_convertible<Stringy> = true>
62 Dumper(Stringy string):
64
65 template <typename U, if_compatible_dumper<U> = true>
66 Dumper(U f): dumper(f) {}
67
68 void operator()(Sink s) { dumper(s); }
69};
70
71template <typename T>
72void sinkInt(Sink s, T i) {
73 const int BUFSIZE = 42; // safe up to 128 bits
75 int ibuf = BUFSIZE;
76 buf[--ibuf] = QChar(0);
77 bool neg = false;
78 if (i < 0)
79 neg=true;
80 int digit = i % 10;
81 i = i / 10;
82 if constexpr (std::is_signed_v<T>) {
83 if (neg) { // we change the sign here because -numeric_limits<T>::min() == numeric_limits<T>::min()
84 i = -i;
85 digit = - digit;
86 }
87 }
88 buf[--ibuf] = QChar::fromLatin1('0' + digit);
89 while (i > 0 && ibuf > 0) {
90 digit = i % 10;
91 buf[--ibuf] = QChar::fromLatin1('0' + digit);
92 i = i / 10;
93 }
94 if (neg && ibuf > 0)
95 buf[--ibuf] = QChar::fromLatin1('-');
96 s(QStringView(&buf[ibuf], BUFSIZE - ibuf -1));
97}
98
100
103
104inline void devNull(QStringView) {}
105
106QMLDOM_EXPORT void sinkIndent(Sink s, int indent);
107
108QMLDOM_EXPORT void sinkNewline(Sink s, int indent = 0);
109
111
112QMLDOM_EXPORT void dumperToQDebug(Dumper dumper, QDebug debug);
113
115
116QMLDOM_EXPORT QDebug operator<<(QDebug d, Dumper dumper);
117
118} // end namespace Dom
119} // end namespace QQmlJS
121
122#endif // DUMPER_H
\inmodule QtCore
Definition qchar.h:48
static constexpr QChar fromLatin1(char c) noexcept
Converts the Latin-1 character c to its equivalent QChar.
Definition qchar.h:461
\inmodule QtCore
A Sink is a function that accepts a QStringView as input.
\inmodule QtCore
Definition qstringview.h:76
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
void devNull(QStringView)
function_ref< void(QStringView)> Sink
QDebug operator<<(QDebug d, AST::Node *n)
std::function< void(QStringView)> SinkF
void dumpErrorLevel(Sink s, ErrorLevel level)
Dumps a string describing the given error level (ErrorLevel::Error -> Error,...)
std::function< void(Sink)> DumperFunction
void sinkEscaped(Sink sink, QStringView s, EscapeOptions options)
dumps a string as quoted string (escaping things like quotes or newlines)
void sinkNewline(Sink s, int indent)
sinks a neline and indents by the given amount
void sinkInt(Sink s, T i)
QString dumperToString(Dumper writer)
Converts a dumper to a string.
void sinkIndent(Sink s, int indent)
sinks the requested amount of spaces
void dumperToQDebug(Dumper dumper, QDebug debug)
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
#define BUFSIZE
GLenum GLuint GLint level
GLfloat GLfloat f
GLenum type
GLenum GLuint GLenum GLsizei const GLchar * buf
GLsizei GLenum GLboolean sink
GLdouble s
[6]
Definition qopenglext.h:235
GLsizei const GLchar *const * string
[0]
Definition qopenglext.h:694
#define QMLDOM_EXPORT
QObject::connect nullptr