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
qqmlstringconverters_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-parser
4
5#ifndef QQMLSTRINGCONVERTERS_P_H
6#define QQMLSTRINGCONVERTERS_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 purely as an
13// implementation detail. 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/qglobal.h>
20#include <QtCore/qvariant.h>
21
22#include <private/qtqmlglobal_p.h>
23
24QT_BEGIN_NAMESPACE
25
26class QPointF;
27class QSizeF;
28class QRectF;
29class QString;
30class QByteArray;
31
33{
34 Q_QML_EXPORT QVariant variantFromString(const QString &, QMetaType preferredType, bool *ok = nullptr);
35
36 Q_QML_EXPORT QVariant colorFromString(const QString &, bool *ok = nullptr);
37 Q_QML_EXPORT unsigned rgbaFromString(const QString &, bool *ok = nullptr);
38
39#if QT_CONFIG(datestring)
40 Q_QML_EXPORT QDate dateFromString(const QString &, bool *ok = nullptr);
41 Q_QML_EXPORT QTime timeFromString(const QString &, bool *ok = nullptr);
42 Q_QML_EXPORT QDateTime dateTimeFromString(const QString &, bool *ok = nullptr);
43#endif
44 Q_QML_EXPORT QPointF pointFFromString(const QString &, bool *ok = nullptr);
45 Q_QML_EXPORT QSizeF sizeFFromString(const QString &, bool *ok = nullptr);
46 Q_QML_EXPORT QRectF rectFFromString(const QString &, bool *ok = nullptr);
47
48 // checks if the string contains a list of doubles separated by separators, like "double1
49 // separators1 double2 separators2 ..." for example.
50 template<int NumParams, char16_t... separators>
51 bool isValidNumberString(const QString &s, std::array<double, NumParams> *numbers = nullptr)
52 {
53 Q_STATIC_ASSERT_X(
54 NumParams == 2 || NumParams == 3 || NumParams == 4 || NumParams == 16,
55 "Unsupported number of params; add an additional case below if necessary.");
56 constexpr std::array<char16_t, NumParams - 1> separatorArray{ separators... };
57 // complain about missing separators when first or last entry is initialized with 0
58 Q_STATIC_ASSERT_X(separatorArray[0] != 0,
59 "Did not specify any separators for isValidNumberString.");
60 Q_STATIC_ASSERT_X(separatorArray[NumParams - 2] != 0,
61 "Did not specify enough separators for isValidNumberString.");
62
63 bool floatOk = true;
64 QStringView view(s);
65 for (qsizetype i = 0; i < NumParams - 1; ++i) {
66 const qsizetype commaIndex = view.indexOf(separatorArray[i]);
67 if (commaIndex == -1)
68 return false;
69 const auto current = view.first(commaIndex).toDouble(&floatOk);
70 if (!floatOk)
71 return false;
72 if (numbers)
73 (*numbers)[i] = current;
74
75 view = view.sliced(commaIndex + 1);
76 }
77 const auto current = view.toDouble(&floatOk);
78 if (!floatOk)
79 return false;
80 if (numbers)
81 (*numbers)[NumParams - 1] = current;
82
83 return true;
84 }
85
86 // Constructs a value type T from the given string that contains NumParams double values
87 // separated by separators, like "double1 separators1 double2 separators2 ..." for example.
88 template<typename T, int NumParams, char16_t... separators>
89 T valueTypeFromNumberString(const QString &s, bool *ok = nullptr)
90 {
91 Q_STATIC_ASSERT_X(
92 NumParams == 2 || NumParams == 3 || NumParams == 4 || NumParams == 16,
93 "Unsupported number of params; add an additional case below if necessary.");
94
95 std::array<double, NumParams> parameters;
96 if (!isValidNumberString<NumParams, separators...>(s, &parameters)) {
97 if (ok)
98 *ok = false;
99 return T{};
100 }
101
102 if (ok)
103 *ok = true;
104
105 if constexpr (NumParams == 2) {
106 return T(parameters[0], parameters[1]);
107 } else if constexpr (NumParams == 3) {
108 return T(parameters[0], parameters[1], parameters[2]);
109 } else if constexpr (NumParams == 4) {
110 return T(parameters[0], parameters[1], parameters[2], parameters[3]);
111 } else if constexpr (NumParams == 16) {
112 return T(parameters[0], parameters[1], parameters[2], parameters[3], parameters[4],
113 parameters[5], parameters[6], parameters[7], parameters[8], parameters[9],
114 parameters[10], parameters[11], parameters[12], parameters[13], parameters[14],
115 parameters[15]);
116 }
117
118 Q_UNREACHABLE_RETURN(T{});
119 }
120}
121
122QT_END_NAMESPACE
123
124#endif // QQMLSTRINGCONVERTERS_P_H
\inmodule QtCore\reentrant
Definition qpoint.h:231
T valueTypeFromNumberString(const QString &s, bool *ok=nullptr)
Q_QML_EXPORT unsigned rgbaFromString(const QString &, bool *ok=nullptr)
bool isValidNumberString(const QString &s, std::array< double, NumParams > *numbers=nullptr)