Fix (de)serialize enums into QDataStream.
It is very important to use exactly the same way across all Qt versions we need to support. Otherwise, it will break interchange between Valentina versions built on different Qt versions.
This commit is contained in:
parent
ab569feff8
commit
a009e25004
|
@ -34,26 +34,22 @@
|
||||||
|
|
||||||
// (de)serialize enums into QDataStream
|
// (de)serialize enums into QDataStream
|
||||||
|
|
||||||
|
// It is very important to use exactly the same way across all Qt versions we need to support. Otherwise, it will break
|
||||||
|
// interchange between Valentina versions built on different Qt versions.
|
||||||
|
|
||||||
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
|
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
|
||||||
//a function that can serialize any enum into QDataStream
|
template <typename T>
|
||||||
//it stores the enum in a qint64
|
typename std::enable_if<std::is_enum<T>::value, QDataStream &>::type&
|
||||||
template<typename Enum,
|
operator<<(QDataStream &s, const T &t)
|
||||||
typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
|
|
||||||
inline QDataStream& operator<<(QDataStream& stream, const Enum& e)
|
|
||||||
{
|
{
|
||||||
return stream << static_cast<qint64>(e);
|
return s << static_cast<typename std::underlying_type<T>::type>(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
//a function that can deserialize any enum from QDataStream
|
template <typename T>
|
||||||
//it reads the enum as if it was stored in qint64
|
typename std::enable_if<std::is_enum<T>::value, QDataStream &>::type&
|
||||||
template<typename Enum,
|
operator>>(QDataStream &s, T &t)
|
||||||
typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
|
|
||||||
inline QDataStream& operator>>(QDataStream& stream, Enum& e)
|
|
||||||
{
|
{
|
||||||
qint64 v;
|
return s >> reinterpret_cast<typename std::underlying_type<T>::type &>(t);
|
||||||
stream >> v;
|
|
||||||
e = static_cast<Enum>(v);
|
|
||||||
return stream;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user