1 /***************************************************************************
2 * Copyright (C) 2005-2016 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
23 #include <type_traits>
28 #include <QDataStream>
29 #include <QTextStream>
30 #include <QHostAddress>
31 #include <QDataStream>
39 inline SignedId(int _id = 0) { id = _id; }
40 inline qint32 toInt() const { return id; }
41 inline bool isValid() const { return id > 0; }
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; }
55 inline SignedId operator++(int) { id++; return *this; }
56 //inline operator int() const { return toInt(); } // no automatic conversion!
58 friend QDataStream &operator>>(QDataStream &in, SignedId &signedId);
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()); }
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!
73 struct MsgId : public SignedId {
74 inline MsgId(int _id = 0) : SignedId(_id) {}
75 //inline operator QVariant() const { return QVariant::fromValue<MsgId>(*this); }
78 struct BufferId : public SignedId {
79 inline BufferId(int _id = 0) : SignedId(_id) {}
80 //inline operator QVariant() const { return QVariant::fromValue<BufferId>(*this); }
83 struct NetworkId : public SignedId {
84 inline NetworkId(int _id = 0) : SignedId(_id) {}
85 //inline operator QVariant() const { return QVariant::fromValue<NetworkId>(*this); }
88 struct IdentityId : public SignedId {
89 inline IdentityId(int _id = 0) : SignedId(_id) {}
90 //inline operator QVariant() const { return QVariant::fromValue<IdentityId>(*this); }
93 struct AccountId : public SignedId {
94 inline AccountId(int _id = 0) : SignedId(_id) {}
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)
104 Q_DECLARE_METATYPE(QHostAddress)
107 typedef QList<MsgId> MsgIdList;
108 typedef QList<BufferId> BufferIdList;
111 * Catch-all stream serialization operator for enum types.
113 * @param[in,out] out Stream to serialize to
114 * @param[in] value Value to serialize
115 * @returns A reference to the stream
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);
125 * Catch-all stream serialization operator for enum types.
127 * @param[in,out] in Stream to deserialize from
128 * @param[out] value Value to deserialize into
129 * @returns A reference to the stream
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;
136 value = static_cast<T>(v);