1 | // Copyright (C) 2018 Intel Corporation. |
---|---|
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 QCBORSTREAMREADER_H |
5 | #define QCBORSTREAMREADER_H |
6 | |
7 | #include <QtCore/qbytearray.h> |
8 | #include <QtCore/qcborcommon.h> |
9 | #include <QtCore/qfloat16.h> |
10 | #include <QtCore/qscopedpointer.h> |
11 | #include <QtCore/qstring.h> |
12 | #include <QtCore/qstringview.h> |
13 | |
14 | QT_REQUIRE_CONFIG(cborstreamreader); |
15 | |
16 | /* X11 headers use these values too, but as defines */ |
17 | #if defined(False) && defined(True) |
18 | # undef True |
19 | # undef False |
20 | #endif |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | class QIODevice; |
25 | |
26 | class QCborStreamReaderPrivate; |
27 | class Q_CORE_EXPORT QCborStreamReader |
28 | { |
29 | Q_GADGET |
30 | public: |
31 | enum Type : quint8 { |
32 | UnsignedInteger = 0x00, |
33 | NegativeInteger = 0x20, |
34 | ByteString = 0x40, |
35 | ByteArray = ByteString, |
36 | TextString = 0x60, |
37 | String = TextString, |
38 | Array = 0x80, |
39 | Map = 0xa0, |
40 | Tag = 0xc0, |
41 | SimpleType = 0xe0, |
42 | HalfFloat = 0xf9, |
43 | Float16 = HalfFloat, |
44 | Float = 0xfa, |
45 | Double = 0xfb, |
46 | |
47 | Invalid = 0xff |
48 | }; |
49 | Q_ENUM(Type) |
50 | |
51 | enum StringResultCode { |
52 | EndOfString = 0, |
53 | Ok = 1, |
54 | Error = -1 |
55 | }; |
56 | template <typename Container> struct StringResult { |
57 | Container data; |
58 | StringResultCode status = Error; |
59 | }; |
60 | Q_ENUM(StringResultCode) |
61 | |
62 | QCborStreamReader(); |
63 | QCborStreamReader(const char *data, qsizetype len); |
64 | QCborStreamReader(const quint8 *data, qsizetype len); |
65 | explicit QCborStreamReader(const QByteArray &data); |
66 | explicit QCborStreamReader(QIODevice *device); |
67 | ~QCborStreamReader(); |
68 | Q_DISABLE_COPY(QCborStreamReader) |
69 | |
70 | void setDevice(QIODevice *device); |
71 | QIODevice *device() const; |
72 | void addData(const QByteArray &data); |
73 | void addData(const char *data, qsizetype len); |
74 | void addData(const quint8 *data, qsizetype len) |
75 | { addData(data: reinterpret_cast<const char *>(data), len); } |
76 | void reparse(); |
77 | void clear(); |
78 | void reset(); |
79 | |
80 | #if QT_CORE_REMOVED_SINCE(6, 7) |
81 | QCborError lastError(); |
82 | #endif |
83 | QCborError lastError() const; |
84 | |
85 | qint64 currentOffset() const; |
86 | |
87 | bool isValid() const { return !isInvalid(); } |
88 | |
89 | int containerDepth() const; |
90 | QCborStreamReader::Type parentContainerType() const; |
91 | bool hasNext() const noexcept Q_DECL_PURE_FUNCTION; |
92 | bool next(int maxRecursion = 10000); |
93 | |
94 | Type type() const { return QCborStreamReader::Type(type_); } |
95 | bool isUnsignedInteger() const { return type() == UnsignedInteger; } |
96 | bool isNegativeInteger() const { return type() == NegativeInteger; } |
97 | bool isInteger() const { return quint8(type()) <= quint8(NegativeInteger); } |
98 | bool isByteArray() const { return type() == ByteArray; } |
99 | bool isString() const { return type() == String; } |
100 | bool isArray() const { return type() == Array; } |
101 | bool isMap() const { return type() == Map; } |
102 | bool isTag() const { return type() == Tag; } |
103 | bool isSimpleType() const { return type() == SimpleType; } |
104 | bool isFloat16() const { return type() == Float16; } |
105 | bool isFloat() const { return type() == Float; } |
106 | bool isDouble() const { return type() == Double; } |
107 | bool isInvalid() const { return type() == Invalid; } |
108 | |
109 | bool isSimpleType(QCborSimpleType st) const { return isSimpleType() && toSimpleType() == st; } |
110 | bool isFalse() const { return isSimpleType(st: QCborSimpleType::False); } |
111 | bool isTrue() const { return isSimpleType(st: QCborSimpleType::True); } |
112 | bool isBool() const { return isFalse() || isTrue(); } |
113 | bool isNull() const { return isSimpleType(st: QCborSimpleType::Null); } |
114 | bool isUndefined() const { return isSimpleType(st: QCborSimpleType::Undefined); } |
115 | |
116 | bool isLengthKnown() const noexcept Q_DECL_PURE_FUNCTION; |
117 | quint64 length() const; |
118 | |
119 | bool isContainer() const { return isMap() || isArray(); } |
120 | bool enterContainer() { Q_ASSERT(isContainer()); return _enterContainer_helper(); } |
121 | bool leaveContainer(); |
122 | |
123 | bool readAndAppendToString(QString &dst) |
124 | { Q_ASSERT(isString()); return _readAndAppendToString_helper(dst); } |
125 | bool readAndAppendToUtf8String(QByteArray &dst) |
126 | { Q_ASSERT(isString()); return _readAndAppendToUtf8String_helper(dst); } |
127 | bool readAndAppendToByteArray(QByteArray &dst) |
128 | { Q_ASSERT(isByteArray()); return _readAndAppendToByteArray_helper(dst); } |
129 | StringResult<QString> readString() { Q_ASSERT(isString()); return _readString_helper(); } |
130 | StringResult<QByteArray> readUtf8String() { Q_ASSERT(isString()); return _readUtf8String_helper(); } |
131 | StringResult<QByteArray> readByteArray(){ Q_ASSERT(isByteArray()); return _readByteArray_helper(); } |
132 | qsizetype currentStringChunkSize() const{ Q_ASSERT(isString() || isByteArray()); return _currentStringChunkSize(); } |
133 | StringResult<qsizetype> readStringChunk(char *ptr, qsizetype maxlen); |
134 | |
135 | bool toBool() const { Q_ASSERT(isBool()); return value64 - int(QCborSimpleType::False); } |
136 | QCborTag toTag() const { Q_ASSERT(isTag()); return QCborTag(value64); } |
137 | quint64 toUnsignedInteger() const { Q_ASSERT(isUnsignedInteger()); return value64; } |
138 | QCborNegativeInteger toNegativeInteger() const { Q_ASSERT(isNegativeInteger()); return QCborNegativeInteger(value64 + 1); } |
139 | QCborSimpleType toSimpleType() const{ Q_ASSERT(isSimpleType()); return QCborSimpleType(value64); } |
140 | qfloat16 toFloat16() const { Q_ASSERT(isFloat16()); return _toFloatingPoint<qfloat16>(); } |
141 | float toFloat() const { Q_ASSERT(isFloat()); return _toFloatingPoint<float>(); } |
142 | double toDouble() const { Q_ASSERT(isDouble()); return _toFloatingPoint<double>(); } |
143 | |
144 | qint64 toInteger() const |
145 | { |
146 | Q_ASSERT(isInteger()); |
147 | qint64 v = qint64(value64); |
148 | if (isNegativeInteger()) |
149 | return -v - 1; |
150 | return v; |
151 | } |
152 | QString readAllString() |
153 | { |
154 | QString dst; |
155 | if (!readAndAppendToString(dst)) |
156 | dst = QString{}; |
157 | return dst; |
158 | } |
159 | QByteArray readAllUtf8String() |
160 | { |
161 | QByteArray dst; |
162 | if (!readAndAppendToUtf8String(dst)) |
163 | dst = QByteArray{}; |
164 | return dst; |
165 | } |
166 | QByteArray readAllByteArray() |
167 | { |
168 | QByteArray dst; |
169 | if (!readAndAppendToByteArray(dst)) |
170 | dst = QByteArray{}; |
171 | return dst; |
172 | } |
173 | |
174 | private: |
175 | void preparse(); |
176 | bool _enterContainer_helper(); |
177 | StringResult<QString> _readString_helper(); |
178 | StringResult<QByteArray> _readUtf8String_helper(); |
179 | StringResult<QByteArray> _readByteArray_helper(); |
180 | qsizetype _currentStringChunkSize() const; |
181 | bool _readAndAppendToString_helper(QString &); |
182 | bool _readAndAppendToUtf8String_helper(QByteArray &); |
183 | bool _readAndAppendToByteArray_helper(QByteArray &); |
184 | |
185 | template <typename FP> FP _toFloatingPoint() const noexcept |
186 | { |
187 | using UIntFP = typename QIntegerForSizeof<FP>::Unsigned; |
188 | UIntFP u = UIntFP(value64); |
189 | FP f; |
190 | memcpy(static_cast<void *>(&f), &u, sizeof(f)); |
191 | return f; |
192 | } |
193 | |
194 | friend QCborStreamReaderPrivate; |
195 | friend class QCborContainerPrivate; |
196 | quint64 value64; |
197 | QScopedPointer<QCborStreamReaderPrivate> d; |
198 | quint8 type_; |
199 | quint8 reserved[3] = {}; |
200 | }; |
201 | |
202 | QT_END_NAMESPACE |
203 | |
204 | #if defined(QT_X11_DEFINES_FOUND) |
205 | # define True 1 |
206 | # define False 0 |
207 | #endif |
208 | |
209 | #endif // QCBORSTREAMREADER_H |
210 |
Definitions
- QCborStreamReader
- Type
- StringResultCode
- StringResult
- QCborStreamReader
- addData
- isValid
- type
- isUnsignedInteger
- isNegativeInteger
- isInteger
- isByteArray
- isString
- isArray
- isMap
- isTag
- isSimpleType
- isFloat16
- isFloat
- isDouble
- isInvalid
- isSimpleType
- isFalse
- isTrue
- isBool
- isNull
- isUndefined
- isContainer
- enterContainer
- readAndAppendToString
- readAndAppendToUtf8String
- readAndAppendToByteArray
- readString
- readUtf8String
- readByteArray
- currentStringChunkSize
- toBool
- toTag
- toUnsignedInteger
- toNegativeInteger
- toSimpleType
- toFloat16
- toFloat
- toDouble
- toInteger
- readAllString
- readAllUtf8String
- readAllByteArray
Start learning QML with our Intro Training
Find out more