summaryrefslogtreecommitdiffstats
path: root/src/corelib/serialization
diff options
context:
space:
mode:
authorPaolo Dastoli <paolo.dastoli@gmail.com>2019-02-16 15:23:31 +0100
committerPaolo Dastoli <paolo.dastoli@gmail.com>2019-04-05 14:54:25 +0000
commit6b66108c1ffc48ee1dc06c4e25d9210bf2d41084 (patch)
tree391137ed108787fc70a6b4c6c3f3cdc4d643da12 /src/corelib/serialization
parent7ac2263f30bf5649af00d1b800c1386015feba8f (diff)
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>
Diffstat (limited to 'src/corelib/serialization')
-rw-r--r--src/corelib/serialization/qdatastream.cpp4
-rw-r--r--src/corelib/serialization/qdatastream.h10
2 files changed, 14 insertions, 0 deletions
diff --git a/src/corelib/serialization/qdatastream.cpp b/src/corelib/serialization/qdatastream.cpp
index 3ea518907b..b44d55cb8f 100644
--- a/src/corelib/serialization/qdatastream.cpp
+++ b/src/corelib/serialization/qdatastream.cpp
@@ -98,6 +98,10 @@ QT_BEGIN_NAMESPACE
ensures that you get integers of the size you want and insulates
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
integer equal to the length of the string including the '\\0' byte,
followed by all the characters of the string including the
diff --git a/src/corelib/serialization/qdatastream.h b/src/corelib/serialization/qdatastream.h
index 6e358df02e..2acf7d8c4b 100644
--- a/src/corelib/serialization/qdatastream.h
+++ b/src/corelib/serialization/qdatastream.h
@@ -378,6 +378,16 @@ inline QDataStream &operator>>(QDataStream &s, QFlags<Enum> &e)
{ 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>
inline QDataStream &operator>>(QDataStream &s, QList<T> &l)
{
return QtPrivate::readArrayBasedContainer(s, l);