Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qcborstreamreader.h
Go to the documentation of this file.
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
14QT_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
23
24class QIODevice;
25
27class Q_CORE_EXPORT QCborStreamReader
28{
30public:
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 };
50
52 EndOfString = 0,
53 Ok = 1,
54 Error = -1
55 };
56 template <typename Container> struct StringResult {
57 Container data;
59 };
61
63 QCborStreamReader(const char *data, qsizetype len);
65 explicit QCborStreamReader(const QByteArray &data);
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);
75 { addData(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; }
111 bool isTrue() const { return isSimpleType(QCborSimpleType::True); }
112 bool isBool() const { return isFalse() || isTrue(); }
113 bool isNull() const { return isSimpleType(QCborSimpleType::Null); }
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
124 { Q_ASSERT(isString()); return _readAndAppendToString_helper(dst); }
126 { Q_ASSERT(isString()); return _readAndAppendToUtf8String_helper(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); }
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
145 {
146 Q_ASSERT(isInteger());
147 qint64 v = qint64(value64);
148 if (isNegativeInteger())
149 return -v - 1;
150 return v;
151 }
153 {
154 QString dst;
155 if (!readAndAppendToString(dst))
156 dst = QString{};
157 return dst;
158 }
160 {
162 if (!readAndAppendToUtf8String(dst))
163 dst = QByteArray{};
164 return dst;
165 }
167 {
169 if (!readAndAppendToByteArray(dst))
170 dst = QByteArray{};
171 return dst;
172 }
173
174private:
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
196 quint64 value64;
197 QScopedPointer<QCborStreamReaderPrivate> d;
198 quint8 type_;
199 quint8 reserved[3] = {};
200};
201
203
204#if defined(QT_X11_DEFINES_FOUND)
205# define True 1
206# define False 0
207#endif
208
209#endif // QCBORSTREAMREADER_H
IOBluetoothDevice * device
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore\reentrant
bool isSimpleType() const
Returns true if the type of the current element is any CBOR simple type, including a boolean value (t...
bool readAndAppendToString(QString &dst)
double toDouble() const
Returns the 64-bit double-precision floating point value of the current element.
bool isNull() const
Returns true if the current element is the null value, false if it is anything else.
bool isByteArray() const
Returns true if the type of the current element is a byte array (that is, if type() returns QCborStre...
qfloat16 toFloat16() const
Returns the 16-bit half-precision floating point value of the current element.
float toFloat() const
Returns the 32-bit single-precision floating point value of the current element.
bool isNegativeInteger() const
Returns true if the type of the current element is a negative integer (that is if type() returns QCbo...
bool readAndAppendToByteArray(QByteArray &dst)
QCborSimpleType toSimpleType() const
Returns value of the current simple type.
QCborTag toTag() const
Returns the tag value of the current element.
bool isString() const
Returns true if the type of the current element is a text string (that is, if type() returns QCborStr...
bool isFloat() const
Returns true if the type of the current element is an IEEE 754 single-precision floating point (that ...
bool isArray() const
Returns true if the type of the current element is an array (that is, if type() returns QCborStreamRe...
bool isFalse() const
Returns true if the current element is the false value, false if it is anything else.
StringResult< QByteArray > readByteArray()
Decodes one byte array chunk from the CBOR string and returns it.
bool isMap() const
Returns true if the type of the current element is a map (that is, if type() returns QCborStreamReade...
void addData(const quint8 *data, qsizetype len)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool isInvalid() const
Returns true if the current element is invalid, false otherwise.
bool isTag() const
Returns true if the type of the current element is a CBOR tag (that is, if type() returns QCborStream...
bool enterContainer()
Enters the array or map that is the current item and prepares for iterating the elements contained in...
bool isUnsignedInteger() const
Returns true if the type of the current element is an unsigned integer (that is if type() returns QCb...
bool isFloat16() const
Returns true if the type of the current element is an IEEE 754 half-precision floating point (that is...
QCborNegativeInteger toNegativeInteger() const
Returns the negative integer value of the current element.
bool isValid() const
Returns true if the current element is valid, false otherwise.
StringResult< QByteArray > readUtf8String()
StringResult< QString > readString()
Decodes one string chunk from the CBOR string and returns it.
bool toBool() const
Returns the boolean value of the current element.
bool readAndAppendToUtf8String(QByteArray &dst)
QByteArray readAllByteArray()
bool isTrue() const
Returns true if the current element is the true value, false if it is anything else.
qsizetype currentStringChunkSize() const
Returns the size of the current text or byte string chunk.
bool isBool() const
Returns true if the current element is a boolean value (true or false), false if it is anything else.
quint64 toUnsignedInteger() const
Returns the unsigned integer value of the current element.
StringResultCode
This enum is returned by readString() and readByteArray() and is used to indicate what the status of ...
QByteArray readAllUtf8String()
bool isInteger() const
Returns true if the type of the current element is either an unsigned integer or a negative one (that...
bool isDouble() const
Returns true if the type of the current element is an IEEE 754 double-precision floating point (that ...
Type
This enumeration contains all possible CBOR types as decoded by QCborStreamReader.
bool isSimpleType(QCborSimpleType st) const
Returns true if the type of the current element is the simple type st, false otherwise.
qint64 toInteger() const
Returns the integer value of the current element, be it negative, positive or zero.
bool isUndefined() const
Returns true if the current element is the undefined value, false if it is anything else.
\inmodule QtCore \reentrant
Definition qiodevice.h:34
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
Definition qfloat16.h:47
b clear()
short next
Definition keywords.cpp:445
Combined button and popup list for selecting options.
@ Ok
Definition qbezier.cpp:173
QCborTag
Definition qcborcommon.h:30
QCborSimpleType
Definition qcborcommon.h:23
QCborNegativeInteger
#define Q_DECL_PURE_FUNCTION
quint32 Tag
static ControlElement< T > * ptr(QWidget *widget)
@ Invalid
GLsizei const GLfloat * v
[13]
GLenum GLuint GLenum GLsizei length
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat GLfloat f
GLenum type
GLenum GLenum dst
GLboolean reset
GLenum GLsizei len
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static bool hasNext(const Symbols &symbols, int i)
Definition main.cpp:78
#define QT_REQUIRE_CONFIG(feature)
#define Q_ENUM(x)
#define Q_GADGET
unsigned long long quint64
Definition qtypes.h:61
ptrdiff_t qsizetype
Definition qtypes.h:165
long long qint64
Definition qtypes.h:60
unsigned char quint8
Definition qtypes.h:46
QList< std::pair< QString, QString > > Map
value isSimpleType(QCborSimpleType(12))
[1]
\inmodule QtCore \inheaderfile QtCborCommon \reentrant
Definition qcborcommon.h:63
Definition moc.h:23