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>
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);
61 inline QDataStream &operator<<(QDataStream &out, const SignedId &signedId) { out << signedId.toInt(); return out; }
62 inline QDataStream &operator>>(QDataStream &in, SignedId &signedId) { in >> signedId.id; return in; }
63 inline QTextStream &operator<<(QTextStream &out, const SignedId &signedId) { out << QString::number(signedId.toInt()); return out; }
64 inline QDebug operator<<(QDebug dbg, const SignedId &signedId) { dbg.space() << signedId.toInt(); return dbg; }
65 inline uint qHash(const SignedId &id) { return qHash(id.toInt()); }
73 inline SignedId64(qint64 _id = 0) { id = _id; }
74 inline qint64 toQint64() const { return id; }
75 inline bool isValid() const { return id > 0; }
77 inline bool operator==(const SignedId64 &other) const { return id == other.id; }
78 inline bool operator!=(const SignedId64 &other) const { return id != other.id; }
79 inline bool operator<(const SignedId64 &other) const { return id < other.id; }
80 inline bool operator<=(const SignedId64 &other) const { return id <= other.id; }
81 inline bool operator>(const SignedId64 &other) const { return id > other.id; }
82 inline bool operator>=(const SignedId64 &other) const { return id >= other.id; }
83 inline bool operator==(qint64 i) const { return id == i; }
84 inline bool operator!=(qint64 i) const { return id != i; }
85 inline bool operator<(qint64 i) const { return id < i; }
86 inline bool operator>(qint64 i) const { return id > i; }
87 inline bool operator<=(qint64 i) const { return id <= i; }
89 inline SignedId64 operator++(int) { id++; return *this; }
90 //inline operator int() const { return toQint64(); } // no automatic conversion!
92 friend QDataStream &operator>>(QDataStream &in, SignedId64 &signedId);
95 QDataStream &operator<<(QDataStream &out, const SignedId64 &signedId);
96 QDataStream &operator>>(QDataStream &in, SignedId64 &signedId);
97 inline QTextStream &operator<<(QTextStream &out, const SignedId64 &signedId) { out << QString::number(signedId.toQint64()); return out; }
98 inline QDebug operator<<(QDebug dbg, const SignedId64 &signedId) { dbg.space() << signedId.toQint64(); return dbg; }
99 inline uint qHash(const SignedId64 &id) { return qHash(id.toQint64()); }
101 struct UserId : public SignedId {
102 inline UserId(int _id = 0) : SignedId(_id) {}
103 //inline operator QVariant() const { return QVariant::fromValue<UserId>(*this); } // no automatic conversion!
106 struct MsgId : public SignedId64 {
107 inline MsgId(qint64 _id = 0) : SignedId64(_id) {}
108 //inline operator QVariant() const { return QVariant::fromValue<MsgId>(*this); }
111 struct BufferId : public SignedId {
112 inline BufferId(int _id = 0) : SignedId(_id) {}
113 //inline operator QVariant() const { return QVariant::fromValue<BufferId>(*this); }
116 struct NetworkId : public SignedId {
117 inline NetworkId(int _id = 0) : SignedId(_id) {}
118 //inline operator QVariant() const { return QVariant::fromValue<NetworkId>(*this); }
121 struct IdentityId : public SignedId {
122 inline IdentityId(int _id = 0) : SignedId(_id) {}
123 //inline operator QVariant() const { return QVariant::fromValue<IdentityId>(*this); }
126 struct AccountId : public SignedId {
127 inline AccountId(int _id = 0) : SignedId(_id) {}
130 Q_DECLARE_METATYPE(UserId)
131 Q_DECLARE_METATYPE(MsgId)
132 Q_DECLARE_METATYPE(BufferId)
133 Q_DECLARE_METATYPE(NetworkId)
134 Q_DECLARE_METATYPE(IdentityId)
135 Q_DECLARE_METATYPE(AccountId)
137 Q_DECLARE_METATYPE(QHostAddress)
140 typedef QList<MsgId> MsgIdList;
141 typedef QList<BufferId> BufferIdList;
144 * Catch-all stream serialization operator for enum types.
146 * @param[in,out] out Stream to serialize to
147 * @param[in] value Value to serialize
148 * @returns A reference to the stream
151 typename = typename std::enable_if<std::is_enum<T>::value>::type>
152 QDataStream &operator<<(QDataStream &out, T value) {
153 out << static_cast<typename std::underlying_type<T>::type>(value);
158 * Catch-all stream serialization operator for enum types.
160 * @param[in,out] in Stream to deserialize from
161 * @param[out] value Value to deserialize into
162 * @returns A reference to the stream
165 typename = typename std::enable_if<std::is_enum<T>::value>::type>
166 QDataStream &operator>>(QDataStream &in, T &value) {
167 typename std::underlying_type<T>::type v;
169 value = static_cast<T>(v);
176 * Thrown during initialization to enforce exiting the application before the event loop is started
183 ExitException(int code = EXIT_FAILURE, QString error = {})
184 : exitCode(code), errorString(std::move(error)) {}