Qt 6.x
The Qt SDK
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 {
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 QCborError lastError();
81
82 qint64 currentOffset() const;
83
84 bool isValid() const { return !isInvalid(); }
85
86 int containerDepth() const;
87 QCborStreamReader::Type parentContainerType() const;
88 bool hasNext() const noexcept Q_DECL_PURE_FUNCTION;
89 bool next(int maxRecursion = 10000);
90
91 Type type() const { return QCborStreamReader::Type(type_); }
92 bool isUnsignedInteger() const { return type() == UnsignedInteger; }
93 bool isNegativeInteger() const { return type() == NegativeInteger; }
94 bool isInteger() const { return quint8(type()) <= quint8(NegativeInteger); }
95 bool isByteArray() const { return type() == ByteArray; }
96 bool isString() const { return type() == String; }
97 bool isArray() const { return type() == Array; }
98 bool isMap() const { return type() == Map; }
99 bool isTag() const { return type() == Tag; }
100 bool isSimpleType() const { return type() == SimpleType; }
101 bool isFloat16() const { return type() == Float16; }
102 bool isFloat() const { return type() == Float; }
103 bool isDouble() const { return type() == Double; }
104 bool isInvalid() const { return type() == Invalid; }
105
106 bool isSimpleType(QCborSimpleType st) const { return isSimpleType() && toSimpleType() == st; }
108 bool isTrue() const { return isSimpleType(QCborSimpleType::True); }
109 bool isBool() const { return isFalse() || isTrue(); }
110 bool isNull() const { return isSimpleType(QCborSimpleType::Null); }
112
113 bool isLengthKnown() const noexcept Q_DECL_PURE_FUNCTION;
114 quint64 length() const;
115
116 bool isContainer() const { return isMap() || isArray(); }
117 bool enterContainer() { Q_ASSERT(isContainer()); return _enterContainer_helper(); }
118 bool leaveContainer();
119
120 StringResult<QString> readString() { Q_ASSERT(isString()); return _readString_helper(); }
121 StringResult<QByteArray> readByteArray(){ Q_ASSERT(isByteArray()); return _readByteArray_helper(); }
122 qsizetype currentStringChunkSize() const{ Q_ASSERT(isString() || isByteArray()); return _currentStringChunkSize(); }
123 StringResult<qsizetype> readStringChunk(char *ptr, qsizetype maxlen);
124
125 bool toBool() const { Q_ASSERT(isBool()); return value64 - int(QCborSimpleType::False); }
126 QCborTag toTag() const { Q_ASSERT(isTag()); return QCborTag(value64); }
127 quint64 toUnsignedInteger() const { Q_ASSERT(isUnsignedInteger()); return value64; }
128 QCborNegativeInteger toNegativeInteger() const { Q_ASSERT(isNegativeInteger()); return QCborNegativeInteger(value64 + 1); }
130 qfloat16 toFloat16() const { Q_ASSERT(isFloat16()); return _toFloatingPoint<qfloat16>(); }
131 float toFloat() const { Q_ASSERT(isFloat()); return _toFloatingPoint<float>(); }
132 double toDouble() const { Q_ASSERT(isDouble()); return _toFloatingPoint<double>(); }
133
135 {
136 Q_ASSERT(isInteger());
137 qint64 v = qint64(value64);
138 if (isNegativeInteger())
139 return -v - 1;
140 return v;
141 }
142
143private:
144 void preparse();
145 bool _enterContainer_helper();
146 StringResult<QString> _readString_helper();
147 StringResult<QByteArray> _readByteArray_helper();
148 qsizetype _currentStringChunkSize() const;
149
150 template <typename FP> FP _toFloatingPoint() const noexcept
151 {
152 using UIntFP = typename QIntegerForSizeof<FP>::Unsigned;
153 UIntFP u = UIntFP(value64);
154 FP f;
155 memcpy(static_cast<void *>(&f), &u, sizeof(f));
156 return f;
157 }
158
161 quint64 value64;
163 quint8 type_;
164 quint8 reserved[3] = {};
165};
166
168
169#if defined(QT_X11_DEFINES_FOUND)
170# define True 1
171# define False 0
172#endif
173
174#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...
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...
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< 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 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 ...
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
\inmodule QtCore
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
Definition qfloat16.h:46
b clear()
short next
Definition keywords.cpp:445
Combined button and popup list for selecting options.
@ Ok
Definition qbezier.cpp:275
QCborTag
Definition qcborcommon.h:30
QCborSimpleType
Definition qcborcommon.h:23
QCborNegativeInteger
#define Q_DECL_PURE_FUNCTION
static ControlElement< T > * ptr(QWidget *widget)
@ Invalid
GLsizei const GLfloat * v
[13]
GLenum GLuint GLenum GLsizei length
GLfloat GLfloat f
GLenum type
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLboolean reset
GLenum GLsizei len
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static bool hasNext(const Symbols &symbols, int i)
Definition main.cpp:66
#define QT_REQUIRE_CONFIG(feature)
#define Q_ENUM(x)
#define Q_GADGET
Tag
unsigned long long quint64
Definition qtypes.h:56
ptrdiff_t qsizetype
Definition qtypes.h:70
long long qint64
Definition qtypes.h:55
unsigned char quint8
Definition qtypes.h:41
QList< QPair< QString, QString > > Map
value isSimpleType(QCborSimpleType(12))
[1]
\inmodule QtCore \inheaderfile QtCborCommon \reentrant
Definition qcborcommon.h:63
Definition moc.h:24