1 /***************************************************************************
2 * Copyright (C) 2005-2018 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>
25 #include <QDataStream>
27 #include <QHostAddress>
29 #include <QTextStream>
38 inline SignedId(int _id = 0) { id = _id; }
39 inline qint32 toInt() const { return id; }
40 inline bool isValid() const { return id > 0; }
42 inline bool operator==(const SignedId& other) const { return id == other.id; }
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==(int i) const { return id == i; }
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; }
54 inline SignedId operator++(int)
59 // inline operator int() const { return toInt(); } // no automatic conversion!
61 friend QDataStream& operator>>(QDataStream& in, SignedId& signedId);
64 inline QDataStream& operator<<(QDataStream& out, const SignedId& signedId)
66 out << signedId.toInt();
69 inline QDataStream& operator>>(QDataStream& in, SignedId& signedId)
74 inline QTextStream& operator<<(QTextStream& out, const SignedId& signedId)
76 out << QString::number(signedId.toInt());
79 inline QDebug operator<<(QDebug dbg, const SignedId& signedId)
81 dbg.space() << signedId.toInt();
84 inline uint qHash(const SignedId& id)
86 return qHash(id.toInt());
95 inline SignedId64(qint64 _id = 0) { id = _id; }
96 inline qint64 toQint64() const { return id; }
97 inline bool isValid() const { return id > 0; }
99 inline bool operator==(const SignedId64& other) const { return id == other.id; }
100 inline bool operator!=(const SignedId64& other) const { return id != other.id; }
101 inline bool operator<(const SignedId64& other) const { return id < other.id; }
102 inline bool operator<=(const SignedId64& other) const { return id <= other.id; }
103 inline bool operator>(const SignedId64& other) const { return id > other.id; }
104 inline bool operator>=(const SignedId64& other) const { return id >= other.id; }
105 inline bool operator==(qint64 i) const { return id == i; }
106 inline bool operator!=(qint64 i) const { return id != i; }
107 inline bool operator<(qint64 i) const { return id < i; }
108 inline bool operator>(qint64 i) const { return id > i; }
109 inline bool operator<=(qint64 i) const { return id <= i; }
111 inline SignedId64 operator++(int)
116 // inline operator int() const { return toQint64(); } // no automatic conversion!
118 friend QDataStream& operator>>(QDataStream& in, SignedId64& signedId);
121 QDataStream& operator<<(QDataStream& out, const SignedId64& signedId);
122 QDataStream& operator>>(QDataStream& in, SignedId64& signedId);
123 inline QTextStream& operator<<(QTextStream& out, const SignedId64& signedId)
125 out << QString::number(signedId.toQint64());
128 inline QDebug operator<<(QDebug dbg, const SignedId64& signedId)
130 dbg.space() << signedId.toQint64();
133 inline uint qHash(const SignedId64& id)
135 return qHash(id.toQint64());
138 struct UserId : public SignedId
140 inline UserId(int _id = 0)
143 // inline operator QVariant() const { return QVariant::fromValue<UserId>(*this); } // no automatic conversion!
146 struct MsgId : public SignedId64
148 inline MsgId(qint64 _id = 0)
151 // inline operator QVariant() const { return QVariant::fromValue<MsgId>(*this); }
154 struct BufferId : public SignedId
156 inline BufferId(int _id = 0)
159 // inline operator QVariant() const { return QVariant::fromValue<BufferId>(*this); }
162 struct NetworkId : public SignedId
164 inline NetworkId(int _id = 0)
167 // inline operator QVariant() const { return QVariant::fromValue<NetworkId>(*this); }
170 struct IdentityId : public SignedId
172 inline IdentityId(int _id = 0)
175 // inline operator QVariant() const { return QVariant::fromValue<IdentityId>(*this); }
178 struct AccountId : public SignedId
180 inline AccountId(int _id = 0)
185 Q_DECLARE_METATYPE(UserId)
186 Q_DECLARE_METATYPE(MsgId)
187 Q_DECLARE_METATYPE(BufferId)
188 Q_DECLARE_METATYPE(NetworkId)
189 Q_DECLARE_METATYPE(IdentityId)
190 Q_DECLARE_METATYPE(AccountId)
192 Q_DECLARE_METATYPE(QHostAddress)
195 using MsgIdList = QList<MsgId>;
196 using BufferIdList = QList<BufferId>;
199 * Catch-all stream serialization operator for enum types.
201 * @param[in,out] out Stream to serialize to
202 * @param[in] value Value to serialize
203 * @returns A reference to the stream
205 template<typename T, typename = typename std::enable_if<std::is_enum<T>::value>::type>
206 QDataStream& operator<<(QDataStream& out, T value)
208 out << static_cast<typename std::underlying_type<T>::type>(value);
213 * Catch-all stream serialization operator for enum types.
215 * @param[in,out] in Stream to deserialize from
216 * @param[out] value Value to deserialize into
217 * @returns A reference to the stream
219 template<typename T, typename = typename std::enable_if<std::is_enum<T>::value>::type>
220 QDataStream& operator>>(QDataStream& in, T& value)
222 typename std::underlying_type<T>::type v;
224 value = static_cast<T>(v);
228 // STL-compliant hash functor for Qt types
232 uint operator()(const T& t) const
241 * Thrown during initialization to enforce exiting the application before the event loop is started
248 ExitException(int code = EXIT_FAILURE, QString error = {})
250 , errorString(std::move(error))