uisupport: Provide helpers for dealing with widget changes
[quassel.git] / src / common / types.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 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()); }
66
67 class SignedId64
68 {
69 protected:
70     qint64 id;
71
72 public:
73     inline SignedId64(qint64 _id = 0) { id = _id; }
74     inline qint64 toQint64() const { return id; }
75     inline bool isValid() const { return id > 0; }
76
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; }
88
89     inline SignedId64 operator++(int) { id++; return *this; }
90     //inline operator int() const { return toQint64(); } // no automatic conversion!
91
92     friend QDataStream &operator>>(QDataStream &in, SignedId64 &signedId);
93 };
94
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()); }
100
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!
104 };
105
106 struct MsgId : public SignedId64 {
107     inline MsgId(qint64 _id = 0) : SignedId64(_id) {}
108     //inline operator QVariant() const { return QVariant::fromValue<MsgId>(*this); }
109 };
110
111 struct BufferId : public SignedId {
112     inline BufferId(int _id = 0) : SignedId(_id) {}
113     //inline operator QVariant() const { return QVariant::fromValue<BufferId>(*this); }
114 };
115
116 struct NetworkId : public SignedId {
117     inline NetworkId(int _id = 0) : SignedId(_id) {}
118     //inline operator QVariant() const { return QVariant::fromValue<NetworkId>(*this); }
119 };
120
121 struct IdentityId : public SignedId {
122     inline IdentityId(int _id = 0) : SignedId(_id) {}
123     //inline operator QVariant() const { return QVariant::fromValue<IdentityId>(*this); }
124 };
125
126 struct AccountId : public SignedId {
127     inline AccountId(int _id = 0) : SignedId(_id) {}
128 };
129
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)
136
137 Q_DECLARE_METATYPE(QHostAddress)
138
139 // a few typedefs
140 using MsgIdList = QList<MsgId>;
141 using BufferIdList = QList<BufferId>;
142
143 /**
144  * Catch-all stream serialization operator for enum types.
145  *
146  * @param[in,out] out   Stream to serialize to
147  * @param[in]     value Value to serialize
148  * @returns A reference to the stream
149  */
150 template<typename T,
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);
154     return out;
155 }
156
157 /**
158  * Catch-all stream serialization operator for enum types.
159  *
160  * @param[in,out] in    Stream to deserialize from
161  * @param[out]    value Value to deserialize into
162  * @returns A reference to the stream
163  */
164 template<typename T,
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;
168     in >> v;
169     value = static_cast<T>(v);
170     return in;
171 }
172
173 // Exceptions
174
175 /**
176  * Thrown during initialization to enforce exiting the application before the event loop is started
177  */
178 struct ExitException
179 {
180     int exitCode;
181     QString errorString;
182
183     ExitException(int code = EXIT_FAILURE, QString error = {})
184         : exitCode(code), errorString(std::move(error)) {}
185 };