Skip to content

Instantly share code, notes, and snippets.

@studiofuga
Created February 24, 2023 11:29
Show Gist options
  • Save studiofuga/b5f8412aacbcd8d9b98dfc0b845ddceb to your computer and use it in GitHub Desktop.
Save studiofuga/b5f8412aacbcd8d9b98dfc0b845ddceb to your computer and use it in GitHub Desktop.
Allow using Enum Classes with QDataStream and stream operators
//
// Created by Federico Fuga <fuga@studiofuga.com> on 15/02/23.
//
// Allow using Enum Classes with QDataStream and stream operators
#ifndef QSTREAMENUM_H
#define QSTREAMENUM_H
#include <QDataStream>
template<typename T, typename U = std::enable_if_t<std::is_enum<T>::value>>
QDataStream &operator<<(QDataStream &stream, T enumValue)
{
stream << static_cast<std::underlying_type_t<T>>(enumValue);
return stream;
}
template<typename T, typename U = std::enable_if_t<std::is_enum<T>::value>>
QDataStream &operator>>(QDataStream &stream, T enumValue)
{
stream >> T{enumValue};
return stream;
}
#endif //QSTREAMENUM_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment