common: Make frequently called util methods more efficient
[quassel.git] / src / common / types.h
index aa0521b..eb0c0ca 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2012 by the Quassel Project                        *
+ *   Copyright (C) 2005-2016 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
 
-#ifndef TYPES_H_
-#define TYPES_H_
+#pragma once
+
+#include <type_traits>
 
 #include <QDebug>
 #include <QString>
 #include <QVariant>
+#include <QDataStream>
 #include <QTextStream>
 #include <QHostAddress>
+#include <QDataStream>
 
 class SignedId
 {
@@ -104,14 +107,32 @@ Q_DECLARE_METATYPE(QHostAddress)
 typedef QList<MsgId> MsgIdList;
 typedef QList<BufferId> BufferIdList;
 
-//! Base class for exceptions.
-struct Exception {
-    Exception(QString msg = "Unknown Exception") : _msg(msg) {}
-    virtual ~Exception() {} // make gcc happy
-    virtual inline QString msg() { return _msg; }
-
-protected:
-    QString _msg;
-};
-
-#endif
+/**
+ * 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<typename T,
+         typename = typename std::enable_if<std::is_enum<T>::value>::type>
+QDataStream &operator<<(QDataStream &out, T value) {
+    out << static_cast<typename std::underlying_type<T>::type>(value);
+    return out;
+}
+
+/**
+ * Catch-all stream serialization operator for enum types.
+ *
+ * @param[in,out] in    Stream to deserialize from
+ * @param[out]    value Value to deserialize into
+ * @returns A reference to the stream
+ */
+template<typename T,
+         typename = typename std::enable_if<std::is_enum<T>::value>::type>
+QDataStream &operator>>(QDataStream &in, T &value) {
+    typename std::underlying_type<T>::type v;
+    in >> v;
+    value = static_cast<T>(v);
+    return in;
+}