From: Manuel Nickschas Date: Tue, 4 Oct 2016 20:53:01 +0000 (+0200) Subject: common: Add generic stream operators for enum types X-Git-Tag: travis-deploy-test~354 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=933e1bf5264b6bbd91941f1f06e6e9675e24221a common: Add generic stream operators for enum types These operators use the underlying type of a given enum to (de)serialize to and from QDataStream. --- diff --git a/src/common/types.h b/src/common/types.h index d9eb5641..5c800952 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -18,8 +18,9 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ -#ifndef TYPES_H_ -#define TYPES_H_ +#pragma once + +#include #include #include @@ -106,6 +107,36 @@ Q_DECLARE_METATYPE(QHostAddress) typedef QList MsgIdList; typedef QList BufferIdList; +/** + * Catch-all stream serialization operator for enum types. + * + * @param[in,out] out Stream to serialize to + * @param[in] value Value to serialize + * @returns A reference to the stream + */ +template{}, int>::type = 0> +QDataStream &operator<<(QDataStream &out, T value) { + out << static_cast::type>(value); + return out; +} + +/** + * Catch-all stream serialization operator for enum types. + * + * @param[in,out] in Stream to deserialize from + * @param[in] value Value to deserialize into + * @returns A reference to the stream + */ +template{}, int>::type = 0> +QDataStream &operator>>(QDataStream &in, T &value) { + typename std::underlying_type::type v; + in >> v; + value = static_cast(v); + return in; +} + //! Base class for exceptions. struct Exception { Exception(QString msg = "Unknown Exception") : _msg(msg) {} @@ -115,5 +146,3 @@ struct Exception { protected: QString _msg; }; - -#endif