Implement QDataStream operator for main CBOR classes

Change-Id: Ib3b68b1963625432c68d02be3bf2ec3c4baf1104
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Jędrzej Nowacki 2018-09-10 08:00:50 +02:00
parent 0e84e3866c
commit e74cb37159
9 changed files with 200 additions and 4 deletions

View File

@ -39,6 +39,7 @@
#include "qcborarray.h"
#include "qcborvalue_p.h"
#include "qdatastream.h"
QT_BEGIN_NAMESPACE
@ -1210,4 +1211,23 @@ QDebug operator<<(QDebug dbg, const QCborArray &a)
}
#endif
#ifndef QT_NO_DATASTREAM
QDataStream &operator<<(QDataStream &stream, const QCborArray &value)
{
stream << value.toCborValue().toCbor();
return stream;
}
QDataStream &operator>>(QDataStream &stream, QCborArray &value)
{
QByteArray buffer;
stream >> buffer;
QCborParserError parseError{};
value = QCborValue::fromCbor(buffer, &parseError).toArray();
if (parseError.error)
stream.setStatus(QDataStream::ReadCorruptData);
return stream;
}
#endif
QT_END_NAMESPACE

View File

@ -47,6 +47,7 @@
QT_BEGIN_NAMESPACE
class QJsonArray;
class QDataStream;
class QCborContainerPrivate;
class Q_CORE_EXPORT QCborArray
@ -293,6 +294,11 @@ Q_CORE_EXPORT uint qHash(const QCborArray &array, uint seed = 0);
Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborArray &a);
#endif
#ifndef QT_NO_DATASTREAM
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborArray &);
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborArray &);
#endif
QT_END_NAMESPACE
#endif // QCBORARRAY_H

View File

@ -1763,4 +1763,23 @@ QDebug operator<<(QDebug dbg, const QCborMap &m)
}
#endif
#ifndef QT_NO_DATASTREAM
QDataStream &operator<<(QDataStream &stream, const QCborMap &value)
{
stream << value.toCborValue().toCbor();
return stream;
}
QDataStream &operator>>(QDataStream &stream, QCborMap &value)
{
QByteArray buffer;
stream >> buffer;
QCborParserError parseError{};
value = QCborValue::fromCbor(buffer, &parseError).toMap();
if (parseError.error)
stream.setStatus(QDataStream::ReadCorruptData);
return stream;
}
#endif
QT_END_NAMESPACE

View File

@ -52,6 +52,7 @@ typedef QMap<QString, QVariant> QVariantMap;
template <class Key, class T> class QHash;
typedef QHash<QString, QVariant> QVariantHash;
class QJsonObject;
class QDataStream;
class QCborContainerPrivate;
class Q_CORE_EXPORT QCborMap
@ -344,6 +345,12 @@ Q_CORE_EXPORT uint qHash(const QCborMap &map, uint seed = 0);
Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborMap &m);
#endif
#ifndef QT_NO_DATASTREAM
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborMap &);
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborMap &);
#endif
QT_END_NAMESPACE
#endif // QCBORMAP_H

View File

@ -39,7 +39,7 @@
#include "qcborvalue.h"
#include "qcborvalue_p.h"
#include "qdatastream.h"
#include "qcborarray.h"
#include "qcbormap.h"
#include "qcborstream.h"
@ -2469,6 +2469,26 @@ QDebug operator<<(QDebug dbg, const QCborValue &v)
}
#endif
#ifndef QT_NO_DATASTREAM
QDataStream &operator<<(QDataStream &stream, const QCborValue &value)
{
stream << QCborValue(value).toCbor();
return stream;
}
QDataStream &operator>>(QDataStream &stream, QCborValue &value)
{
QByteArray buffer;
stream >> buffer;
QCborParserError parseError{};
value = QCborValue::fromCbor(buffer, &parseError);
if (parseError.error)
stream.setStatus(QDataStream::ReadCorruptData);
return stream;
}
#endif
QT_END_NAMESPACE
#include "qcborarray.cpp"

View File

@ -67,6 +67,7 @@ class QCborArray;
class QCborMap;
class QCborStreamReader;
class QCborStreamWriter;
class QDataStream;
struct QCborParserError
{
@ -457,6 +458,11 @@ Q_CORE_EXPORT uint qHash(const QCborValue &value, uint seed = 0);
Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborValue &v);
#endif
#ifndef QT_NO_DATASTREAM
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborValue &);
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborValue &);
#endif
QT_END_NAMESPACE
#if defined(QT_X11_DEFINES_FOUND)

View File

@ -1821,9 +1821,6 @@ DECLARE_NONSTREAMABLE(void)
DECLARE_NONSTREAMABLE(void*)
DECLARE_NONSTREAMABLE(QModelIndex)
DECLARE_NONSTREAMABLE(QPersistentModelIndex)
DECLARE_NONSTREAMABLE(QCborValue)
DECLARE_NONSTREAMABLE(QCborArray)
DECLARE_NONSTREAMABLE(QCborMap)
DECLARE_NONSTREAMABLE(QObject*)
DECLARE_NONSTREAMABLE(QWidget*)

View File

@ -97,6 +97,10 @@ private slots:
void validation();
void toDiagnosticNotation_data();
void toDiagnosticNotation();
void datastreamSerialization_data();
void datastreamSerialization();
void streamVariantSerialization();
};
// Get the validation data from TinyCBOR (see src/3rdparty/tinycbor/tests/parser/data.cpp)
@ -1687,6 +1691,83 @@ void tst_QCborValue::toDiagnosticNotation()
QCOMPARE(result, expected);
}
void tst_QCborValue::datastreamSerialization_data()
{
addCommonCborData();
}
void tst_QCborValue::datastreamSerialization()
{
QFETCH(QCborValue, v);
QByteArray buffer;
{
QDataStream save(&buffer, QIODevice::WriteOnly);
save << v;
QDataStream load(buffer);
QCborValue output;
load >> output;
QCOMPARE(output, v);
}
if (v.isArray()) {
QCborArray array = v.toArray();
QDataStream save(&buffer, QIODevice::WriteOnly);
save << array;
QDataStream load(buffer);
QCborValue output;
load >> output;
QCOMPARE(output, array);
} else if (v.isMap()) {
QCborMap map = v.toMap();
QDataStream save(&buffer, QIODevice::WriteOnly);
save << map;
QDataStream load(buffer);
QCborValue output;
load >> output;
QCOMPARE(output, map);
}
}
void tst_QCborValue::streamVariantSerialization()
{
// Check interface only, implementation is tested through to and from
// cbor functions.
QByteArray buffer;
{
QCborArray array{665, 666, 667};
QVariant output;
QVariant variant = QVariant::fromValue(array);
QDataStream save(&buffer, QIODevice::WriteOnly);
save << variant;
QDataStream load(buffer);
load >> output;
QCOMPARE(output.userType(), QMetaType::QCborArray);
QCOMPARE(qvariant_cast<QCborArray>(output), array);
}
{
QCborMap obj{{"foo", 42}};
QVariant output;
QVariant variant = QVariant::fromValue(obj);
QDataStream save(&buffer, QIODevice::WriteOnly);
save << variant;
QDataStream load(buffer);
load >> output;
QCOMPARE(output.userType(), QMetaType::QCborMap);
QCOMPARE(qvariant_cast<QCborMap>(output), obj);
}
{
QCborValue value{42};
QVariant output;
QVariant variant = QVariant::fromValue(value);
QDataStream save(&buffer, QIODevice::WriteOnly);
save << variant;
QDataStream load(buffer);
load >> output;
QCOMPARE(output.userType(), QMetaType::QCborValue);
QCOMPARE(qvariant_cast<QCborValue>(output), value);
}
}
QTEST_MAIN(tst_QCborValue)
#include "tst_qcborvalue.moc"

View File

@ -140,6 +140,10 @@ private slots:
void stream_QJsonObject();
void stream_QJsonValue();
void stream_QCborArray();
void stream_QCborMap();
void stream_QCborValue();
void setVersion_data();
void setVersion();
@ -2196,6 +2200,42 @@ void tst_QDataStream::stream_QJsonValue()
}
}
void tst_QDataStream::stream_QCborArray()
{
QByteArray buffer;
QDataStream save(&buffer, QIODevice::WriteOnly);
QCborArray arraySave({1, 2, 3});
save << arraySave;
QDataStream load(&buffer, QIODevice::ReadOnly);
QCborArray arrayLoad;
load >> arrayLoad;
QCOMPARE(arrayLoad, arraySave);
}
void tst_QDataStream::stream_QCborMap()
{
QByteArray buffer;
QDataStream save(&buffer, QIODevice::WriteOnly);
QCborMap objSave{{"foo", 1}, {"bar", 2}};
save << objSave;
QDataStream load(&buffer, QIODevice::ReadOnly);
QCborMap objLoad;
load >> objLoad;
QCOMPARE(objLoad, objSave);
}
void tst_QDataStream::stream_QCborValue()
{
QByteArray buffer;
QDataStream save(&buffer, QIODevice::WriteOnly);
QCborValue valueSave{42};
save << valueSave;
QDataStream load(&buffer, QIODevice::ReadOnly);
QCborValue valueLoad;
load >> valueLoad;
QCOMPARE(valueLoad, valueSave);
}
void tst_QDataStream::setVersion_data()
{
QTest::addColumn<int>("vers");