common: Make frequently called util methods more efficient
[quassel.git] / src / common / types.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include <type_traits>
24
25 #include <QDebug>
26 #include <QString>
27 #include <QVariant>
28 #include <QDataStream>
29 #include <QTextStream>
30 #include <QHostAddress>
31 #include <QDataStream>
32
33 class SignedId
34 {
35 protected:
36     qint32 id;
37
38 public:
39     inline SignedId(int _id = 0) { id = _id; }
40     inline qint32 toInt() const { return id; }
41     inline bool isValid() const { return id > 0; }
42
43     inline bool operator==(const SignedId &other) const { return id == other.id; }
44     inline bool operator!=(const SignedId &other) const { return id != other.id; }
45     inline bool operator<(const SignedId &other) const { return id < other.id; }
46     inline bool operator<=(const SignedId &other) const { return id <= other.id; }
47     inline bool operator>(const SignedId &other) const { return id > other.id; }
48     inline bool operator>=(const SignedId &other) const { return id >= other.id; }
49     inline bool operator==(int i) const { return id == i; }
50     inline bool operator!=(int i) const { return id != i; }
51     inline bool operator<(int i) const { return id < i; }
52     inline bool operator>(int i) const { return id > i; }
53     inline bool operator<=(int i) const { return id <= i; }
54
55     inline SignedId operator++(int) { id++; return *this; }
56     //inline operator int() const { return toInt(); } // no automatic conversion!
57
58     friend QDataStream &operator>>(QDataStream &in, SignedId &signedId);
59 };
60
61
62 inline QDataStream &operator<<(QDataStream &out, const SignedId &signedId) { out << signedId.toInt(); return out; }
63 inline QDataStream &operator>>(QDataStream &in, SignedId &signedId) { in >> signedId.id; return in; }
64 inline QTextStream &operator<<(QTextStream &out, const SignedId &signedId) { out << QString::number(signedId.toInt()); return out; }
65 inline QDebug operator<<(QDebug dbg, const SignedId &signedId) { dbg.space() << signedId.toInt(); return dbg; }
66 inline uint qHash(const SignedId &id) { return qHash(id.toInt()); }
67
68 struct UserId : public SignedId {
69     inline UserId(int _id = 0) : SignedId(_id) {}
70     //inline operator QVariant() const { return QVariant::fromValue<UserId>(*this); }  // no automatic conversion!
71 };
72
73 struct MsgId : public SignedId {
74     inline MsgId(int _id = 0) : SignedId(_id) {}
75     //inline operator QVariant() const { return QVariant::fromValue<MsgId>(*this); }
76 };
77
78 struct BufferId : public SignedId {
79     inline BufferId(int _id = 0) : SignedId(_id) {}
80     //inline operator QVariant() const { return QVariant::fromValue<BufferId>(*this); }
81 };
82
83 struct NetworkId : public SignedId {
84     inline NetworkId(int _id = 0) : SignedId(_id) {}
85     //inline operator QVariant() const { return QVariant::fromValue<NetworkId>(*this); }
86 };
87
88 struct IdentityId : public SignedId {
89     inline IdentityId(int _id = 0) : SignedId(_id) {}
90     //inline operator QVariant() const { return QVariant::fromValue<IdentityId>(*this); }
91 };
92
93 struct AccountId : public SignedId {
94     inline AccountId(int _id = 0) : SignedId(_id) {}
95 };
96
97 Q_DECLARE_METATYPE(UserId)
98 Q_DECLARE_METATYPE(MsgId)
99 Q_DECLARE_METATYPE(BufferId)
100 Q_DECLARE_METATYPE(NetworkId)
101 Q_DECLARE_METATYPE(IdentityId)
102 Q_DECLARE_METATYPE(AccountId)
103
104 Q_DECLARE_METATYPE(QHostAddress)
105
106 // a few typedefs
107 typedef QList<MsgId> MsgIdList;
108 typedef QList<BufferId> BufferIdList;
109
110 /**
111  * Catch-all stream serialization operator for enum types.
112  *
113  * @param[in,out] out   Stream to serialize to
114  * @param[in]     value Value to serialize
115  * @returns A reference to the stream
116  */
117 template<typename T,
118          typename = typename std::enable_if<std::is_enum<T>::value>::type>
119 QDataStream &operator<<(QDataStream &out, T value) {
120     out << static_cast<typename std::underlying_type<T>::type>(value);
121     return out;
122 }
123
124 /**
125  * Catch-all stream serialization operator for enum types.
126  *
127  * @param[in,out] in    Stream to deserialize from
128  * @param[out]    value Value to deserialize into
129  * @returns A reference to the stream
130  */
131 template<typename T,
132          typename = typename std::enable_if<std::is_enum<T>::value>::type>
133 QDataStream &operator>>(QDataStream &in, T &value) {
134     typename std::underlying_type<T>::type v;
135     in >> v;
136     value = static_cast<T>(v);
137     return in;
138 }