Add serializer/deserializer for Enumeration

Enum class are serialized using the declared size.

[ChangeLog][QtCore][QDataStream] Enumerations can now
be serialized through QDataStream without the need of
manually defining streaming operators.

Change-Id: Iae9a63eb62b5a5615b657766a3c4c66ba4d98d0e
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Reviewed-by: Paolo Dastoli <paolo.dastoli@gmail.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Paolo Dastoli 2019-02-16 15:23:31 +01:00
parent 7ac2263f30
commit 6b66108c1f
3 changed files with 100 additions and 0 deletions

View File

@ -98,6 +98,10 @@ QT_BEGIN_NAMESPACE
ensures that you get integers of the size you want and insulates ensures that you get integers of the size you want and insulates
you from compiler and platform differences. you from compiler and platform differences.
Enumerations can be serialized through QDataStream without the
need of manually defining streaming operators. Enum classes are
serialized using the declared size.
To take one example, a \c{char *} string is written as a 32-bit To take one example, a \c{char *} string is written as a 32-bit
integer equal to the length of the string including the '\\0' byte, integer equal to the length of the string including the '\\0' byte,
followed by all the characters of the string including the followed by all the characters of the string including the

View File

@ -377,6 +377,16 @@ template <typename Enum>
inline QDataStream &operator>>(QDataStream &s, QFlags<Enum> &e) inline QDataStream &operator>>(QDataStream &s, QFlags<Enum> &e)
{ return s >> e.i; } { return s >> e.i; }
template <typename T>
typename std::enable_if<std::is_enum<T>::value, QDataStream &>::type&
operator<<(QDataStream &s, const T &t)
{ return s << static_cast<typename std::underlying_type<T>::type>(t); }
template <typename T>
typename std::enable_if<std::is_enum<T>::value, QDataStream &>::type&
operator>>(QDataStream &s, T &t)
{ return s >> reinterpret_cast<typename std::underlying_type<T>::type &>(t); }
template <typename T> template <typename T>
inline QDataStream &operator>>(QDataStream &s, QList<T> &l) inline QDataStream &operator>>(QDataStream &s, QList<T> &l)
{ {

View File

@ -181,6 +181,8 @@ private slots:
void streamRealDataTypes(); void streamRealDataTypes();
void enumTest();
void floatingPointPrecision(); void floatingPointPrecision();
void compatibility_Qt3(); void compatibility_Qt3();
@ -3409,6 +3411,90 @@ void tst_QDataStream::floatingPointNaN()
} }
} }
void tst_QDataStream::enumTest()
{
QByteArray ba;
enum class E1 : qint8
{
A,
B,
C
};
{
QDataStream stream(&ba, QIODevice::WriteOnly);
stream << E1::A;
QCOMPARE(ba.size(), int(sizeof(E1)));
}
{
QDataStream stream(ba);
E1 e;
stream >> e;
QCOMPARE(e, E1::A);
}
ba.clear();
enum class E2 : qint16
{
A,
B,
C
};
{
QDataStream stream(&ba, QIODevice::WriteOnly);
stream << E2::B;
QCOMPARE(ba.size(), int(sizeof(E2)));
}
{
QDataStream stream(ba);
E2 e;
stream >> e;
QCOMPARE(e, E2::B);
}
ba.clear();
enum class E4 : qint32
{
A,
B,
C
};
{
QDataStream stream(&ba, QIODevice::WriteOnly);
stream << E4::C;
QCOMPARE(ba.size(), int(sizeof(E4)));
}
{
QDataStream stream(ba);
E4 e;
stream >> e;
QCOMPARE(e, E4::C);
}
ba.clear();
enum E
{
A,
B,
C,
D
};
{
QDataStream stream(&ba, QIODevice::WriteOnly);
stream << E::D;
QCOMPARE(ba.size(), 4);
}
{
QDataStream stream(ba);
E e;
stream >> e;
QCOMPARE(e, E::D);
}
ba.clear();
}
void tst_QDataStream::floatingPointPrecision() void tst_QDataStream::floatingPointPrecision()
{ {
QByteArray ba; QByteArray ba;