Warning, file /include/QtCore/qcborstreamreader.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004 #ifndef QCBORSTREAMREADER_H
0005 #define QCBORSTREAMREADER_H
0006
0007 #include <QtCore/qbytearray.h>
0008 #include <QtCore/qcborcommon.h>
0009 #include <QtCore/qfloat16.h>
0010 #include <QtCore/qscopedpointer.h>
0011 #include <QtCore/qstring.h>
0012 #include <QtCore/qstringview.h>
0013
0014 #include <memory>
0015
0016 QT_REQUIRE_CONFIG(cborstreamreader);
0017
0018
0019 #if defined(False) && defined(True)
0020 # undef True
0021 # undef False
0022 #endif
0023
0024 QT_BEGIN_NAMESPACE
0025
0026 class QIODevice;
0027
0028 class QCborStreamReaderPrivate;
0029 class Q_CORE_EXPORT QCborStreamReader
0030 {
0031 Q_GADGET
0032 public:
0033 enum Type : quint8 {
0034 UnsignedInteger = 0x00,
0035 NegativeInteger = 0x20,
0036 ByteString = 0x40,
0037 ByteArray = ByteString,
0038 TextString = 0x60,
0039 String = TextString,
0040 Array = 0x80,
0041 Map = 0xa0,
0042 Tag = 0xc0,
0043 SimpleType = 0xe0,
0044 HalfFloat = 0xf9,
0045 Float16 = HalfFloat,
0046 Float = 0xfa,
0047 Double = 0xfb,
0048
0049 Invalid = 0xff
0050 };
0051 Q_ENUM(Type)
0052
0053 enum StringResultCode {
0054 EndOfString = 0,
0055 Ok = 1,
0056 Error = -1
0057 };
0058 template <typename Container> struct StringResult {
0059 Container data;
0060 StringResultCode status = Error;
0061 };
0062 Q_ENUM(StringResultCode)
0063
0064 QCborStreamReader();
0065 QCborStreamReader(const char *data, qsizetype len);
0066 QCborStreamReader(const quint8 *data, qsizetype len);
0067 explicit QCborStreamReader(const QByteArray &data);
0068 explicit QCborStreamReader(QIODevice *device);
0069 ~QCborStreamReader();
0070 Q_DISABLE_COPY(QCborStreamReader)
0071
0072 void setDevice(QIODevice *device);
0073 QIODevice *device() const;
0074 void addData(const QByteArray &data);
0075 void addData(const char *data, qsizetype len);
0076 void addData(const quint8 *data, qsizetype len)
0077 { addData(reinterpret_cast<const char *>(data), len); }
0078 void reparse();
0079 void clear();
0080 void reset();
0081
0082 #if QT_CORE_REMOVED_SINCE(6, 7)
0083 QCborError lastError();
0084 #endif
0085 QCborError lastError() const;
0086
0087 qint64 currentOffset() const;
0088
0089 bool isValid() const { return !isInvalid(); }
0090
0091 int containerDepth() const;
0092 QCborStreamReader::Type parentContainerType() const;
0093 bool hasNext() const noexcept Q_DECL_PURE_FUNCTION;
0094 bool next(int maxRecursion = 10000);
0095
0096 Type type() const { return QCborStreamReader::Type(type_); }
0097 bool isUnsignedInteger() const { return type() == UnsignedInteger; }
0098 bool isNegativeInteger() const { return type() == NegativeInteger; }
0099 bool isInteger() const { return quint8(type()) <= quint8(NegativeInteger); }
0100 bool isByteArray() const { return type() == ByteArray; }
0101 bool isString() const { return type() == String; }
0102 bool isArray() const { return type() == Array; }
0103 bool isMap() const { return type() == Map; }
0104 bool isTag() const { return type() == Tag; }
0105 bool isSimpleType() const { return type() == SimpleType; }
0106 bool isFloat16() const { return type() == Float16; }
0107 bool isFloat() const { return type() == Float; }
0108 bool isDouble() const { return type() == Double; }
0109 bool isInvalid() const { return type() == Invalid; }
0110
0111 bool isSimpleType(QCborSimpleType st) const { return isSimpleType() && toSimpleType() == st; }
0112 bool isFalse() const { return isSimpleType(QCborSimpleType::False); }
0113 bool isTrue() const { return isSimpleType(QCborSimpleType::True); }
0114 bool isBool() const { return isFalse() || isTrue(); }
0115 bool isNull() const { return isSimpleType(QCborSimpleType::Null); }
0116 bool isUndefined() const { return isSimpleType(QCborSimpleType::Undefined); }
0117
0118 bool isLengthKnown() const noexcept Q_DECL_PURE_FUNCTION;
0119 quint64 length() const;
0120
0121 bool isContainer() const { return isMap() || isArray(); }
0122 bool enterContainer() { Q_ASSERT(isContainer()); return _enterContainer_helper(); }
0123 bool leaveContainer();
0124
0125 bool readAndAppendToString(QString &dst)
0126 { Q_ASSERT(isString()); return _readAndAppendToString_helper(dst); }
0127 bool readAndAppendToUtf8String(QByteArray &dst)
0128 { Q_ASSERT(isString()); return _readAndAppendToUtf8String_helper(dst); }
0129 bool readAndAppendToByteArray(QByteArray &dst)
0130 { Q_ASSERT(isByteArray()); return _readAndAppendToByteArray_helper(dst); }
0131 StringResult<QString> readString() { Q_ASSERT(isString()); return _readString_helper(); }
0132 StringResult<QByteArray> readUtf8String() { Q_ASSERT(isString()); return _readUtf8String_helper(); }
0133 StringResult<QByteArray> readByteArray(){ Q_ASSERT(isByteArray()); return _readByteArray_helper(); }
0134 qsizetype currentStringChunkSize() const{ Q_ASSERT(isString() || isByteArray()); return _currentStringChunkSize(); }
0135 StringResult<qsizetype> readStringChunk(char *ptr, qsizetype maxlen);
0136
0137 bool toBool() const { Q_ASSERT(isBool()); return value64 - int(QCborSimpleType::False); }
0138 QCborTag toTag() const { Q_ASSERT(isTag()); return QCborTag(value64); }
0139 quint64 toUnsignedInteger() const { Q_ASSERT(isUnsignedInteger()); return value64; }
0140 QCborNegativeInteger toNegativeInteger() const { Q_ASSERT(isNegativeInteger()); return QCborNegativeInteger(value64 + 1); }
0141 QCborSimpleType toSimpleType() const{ Q_ASSERT(isSimpleType()); return QCborSimpleType(value64); }
0142 qfloat16 toFloat16() const { Q_ASSERT(isFloat16()); return _toFloatingPoint<qfloat16>(); }
0143 float toFloat() const { Q_ASSERT(isFloat()); return _toFloatingPoint<float>(); }
0144 double toDouble() const { Q_ASSERT(isDouble()); return _toFloatingPoint<double>(); }
0145
0146 qint64 toInteger() const
0147 {
0148 Q_ASSERT(isInteger());
0149 qint64 v = qint64(value64);
0150 if (isNegativeInteger())
0151 return -v - 1;
0152 return v;
0153 }
0154 QString readAllString()
0155 {
0156 QString dst;
0157 if (!readAndAppendToString(dst))
0158 dst = QString{};
0159 return dst;
0160 }
0161 QByteArray readAllUtf8String()
0162 {
0163 QByteArray dst;
0164 if (!readAndAppendToUtf8String(dst))
0165 dst = QByteArray{};
0166 return dst;
0167 }
0168 QByteArray readAllByteArray()
0169 {
0170 QByteArray dst;
0171 if (!readAndAppendToByteArray(dst))
0172 dst = QByteArray{};
0173 return dst;
0174 }
0175
0176 private:
0177 void preparse();
0178 bool _enterContainer_helper();
0179 StringResult<QString> _readString_helper();
0180 StringResult<QByteArray> _readUtf8String_helper();
0181 StringResult<QByteArray> _readByteArray_helper();
0182 qsizetype _currentStringChunkSize() const;
0183 bool _readAndAppendToString_helper(QString &);
0184 bool _readAndAppendToUtf8String_helper(QByteArray &);
0185 bool _readAndAppendToByteArray_helper(QByteArray &);
0186
0187 template <typename FP> FP _toFloatingPoint() const noexcept
0188 {
0189 using UIntFP = typename QIntegerForSizeof<FP>::Unsigned;
0190 UIntFP u = UIntFP(value64);
0191 FP f;
0192 memcpy(static_cast<void *>(&f), &u, sizeof(f));
0193 return f;
0194 }
0195
0196 friend QCborStreamReaderPrivate;
0197 friend class QCborContainerPrivate;
0198 quint64 value64;
0199 std::unique_ptr<QCborStreamReaderPrivate> d;
0200 quint8 type_;
0201 quint8 reserved[3] = {};
0202 };
0203
0204 QT_END_NAMESPACE
0205
0206 #endif