6.0.0-rc4Source

QCborStreamWriter Class

The QCborStreamWriter class is a simple CBOR encoder operating on a one-way stream. More…

Header#include <QCborStreamWriter>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
Since5.12

List of all members, including inherited members

Public Functions

QCborStreamWriter(QIODevice *device)
QCborStreamWriter(QByteArray *data)
~QCborStreamWriter()
voidsetDevice(QIODevice *device)
QIODevice *device() const
voidappend(quint64 u)
voidappend(qint64 i)
voidappend(T i)
voidappend(QCborNegativeInteger n)
voidappend(const QByteArray &ba)
voidappend(QLatin1StringView str)
voidappend(QStringView str)
voidappend(QCborTag tag)
voidappend(QCborKnownTags tag)
voidappend(QCborSimpleType st)
voidappend(std::nullptr_t)
voidappend(qfloat16 f)
voidappend(float f)
voidappend(double value)
voidappend(bool b)
voidappend(int i)
voidappend(uint u)
voidappend(const char *str, qsizetype size = -1)
voidappendByteString(const char *data, qsizetype len)
voidappendTextString(const char *utf8, qsizetype len)
voidappendNull()
voidappendUndefined()
voidstartArray()
voidstartArray(quint64 count)
boolendArray()
voidstartMap()
voidstartMap(quint64 count)
boolendMap()

Detailed Description

The writer writes CBOR (RFC 8949) item by item to a byte array or a device: numbers, strings, and arrays and maps whose contents are written between startArray() and endArray(), or startMap() and endMap(). In a map, the items alternate between keys and values.

QByteArray data;
QCborStreamWriter writer(&data);
writer.startMap(2);
writer.append("name");
writer.append("Nt");
writer.append("version");
writer.append(6);
writer.endMap();

An array or map started with a count must get exactly that many items; one started without a count is ended by endArray() or endMap() whatever it got. The writer does not check that what it is given makes sense beyond that: a tag must be followed by the item it applies to.

See also QCborStreamReader and QCborValue.

Differences from Qt

Writing to a device is not buffered: each item is written as it comes. Strings are always written whole, with their length, never in chunks. There is no overload for QUtf8StringView.

Member Function Documentation

[explicit] QCborStreamWriter::QCborStreamWriter(QIODevice *device)

Constructs a writer that writes to device, which must be open for writing.

See also setDevice().

[explicit] QCborStreamWriter::QCborStreamWriter(QByteArray *data)

Constructs a writer that appends to data.

QCborStreamWriter::~QCborStreamWriter()

Destroys the writer. Arrays and maps that are still open are not closed.

void QCborStreamWriter::append(quint64 u)

Writes the unsigned integer u.

void QCborStreamWriter::append(qint64 i)

This is an overloaded function.

Writes the integer i, as an unsigned integer if it is not negative and as a negative integer otherwise.

template <typename T, std::enable_if_t<std::is_same_v<T, long long> && !std::is_same_v<long long, qint64>, bool> = true>void QCborStreamWriter::append(T i)

This is an overloaded function.

Writes the integer i, where long long is another type than qint64.

void QCborStreamWriter::append(QCborNegativeInteger n)

This is an overloaded function.

Writes the negative integer n, which may be as small as -2^64.

void QCborStreamWriter::append(const QByteArray &ba)

This is an overloaded function.

Writes ba as a byte string.

See also appendByteString().

void QCborStreamWriter::append(QLatin1StringView str)

This is an overloaded function.

Writes str as a text string, converted from Latin-1 to UTF-8.

void QCborStreamWriter::append(QStringView str)

This is an overloaded function.

Writes str as a text string in UTF-8.

void QCborStreamWriter::append(QCborTag tag)

This is an overloaded function.

Writes the tag tag. The next item is what the tag applies to.

void QCborStreamWriter::append(QCborKnownTags tag)

This is an overloaded function.

Writes the tag tag. The next item is what the tag applies to.

void QCborStreamWriter::append(QCborSimpleType st)

This is an overloaded function.

Writes the simple type st.

void QCborStreamWriter::append(std::nullptr_t)

This is an overloaded function.

Writes null.

See also appendNull().

void QCborStreamWriter::append(qfloat16 f)

This is an overloaded function.

Writes the floating-point number f in 16 bits.

void QCborStreamWriter::append(float f)

This is an overloaded function.

Writes the floating-point number f in 32 bits.

void QCborStreamWriter::append(double value)

This is an overloaded function.

Writes the floating-point number value in 64 bits.

void QCborStreamWriter::append(bool b)

This is an overloaded function.

Writes true or false, according to b.

void QCborStreamWriter::append(int i)

This is an overloaded function.

Writes the integer i.

void QCborStreamWriter::append(uint u)

This is an overloaded function.

Writes the unsigned integer u.

void QCborStreamWriter::append(const char *str, qsizetype size = -1)

This is an overloaded function.

Writes the size bytes at str as a text string; they must be UTF-8. With a negative size, str is taken up to its terminating zero.

See also appendTextString().

void QCborStreamWriter::appendByteString(const char *data, qsizetype len)

Writes the len bytes at data as a byte string.

void QCborStreamWriter::appendNull()

Writes null.

void QCborStreamWriter::appendTextString(const char *utf8, qsizetype len)

Writes the len bytes at utf8 as a text string. They must be valid UTF-8; the writer does not check.

void QCborStreamWriter::appendUndefined()

Writes undefined.

QIODevice * QCborStreamWriter::device() const

Returns the device that the writer writes to, or nullptr if it writes to a byte array.

See also setDevice().

bool QCborStreamWriter::endArray()

Ends the array that was started last. Returns false if there is no array to end, or the array got another number of elements than its count.

See also startArray().

bool QCborStreamWriter::endMap()

Ends the map that was started last. Returns false if there is no map to end, or the map got another number of pairs than its count.

See also startMap().

void QCborStreamWriter::setDevice(QIODevice *device)

Makes the writer write to device from now on. Arrays and maps that are open stay open: their items go to the new device.

See also device().

void QCborStreamWriter::startArray()

Starts an array whose length is not known yet. Write its elements, then call endArray().

See also startMap().

void QCborStreamWriter::startArray(quint64 count)

This is an overloaded function.

Starts an array of count elements. Write them, then call endArray().

void QCborStreamWriter::startMap()

Starts a map whose length is not known yet. Write its keys and values one after the other, then call endMap().

See also startArray().

void QCborStreamWriter::startMap(quint64 count)

This is an overloaded function.

Starts a map of count pairs of a key and a value. Write them, then call endMap().