File: types.h

package info (click to toggle)
quassel 1:0.13.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 25,640 kB
  • sloc: cpp: 66,953; perl: 15,837; ansic: 4,418; sql: 1,225; sh: 328; xml: 263; python: 224; makefile: 24
file content (187 lines) | stat: -rw-r--r-- 7,654 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/***************************************************************************
 *   Copyright (C) 2005-2019 by the Quassel Project                        *
 *   devel@quassel-irc.org                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) version 3.                                           *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#pragma once

#include <type_traits>

#include <QDebug>
#include <QString>
#include <QVariant>
#include <QDataStream>
#include <QTextStream>
#include <QHostAddress>
#include <QDataStream>

class SignedId
{
protected:
    qint32 id;

public:
    inline SignedId(int _id = 0) { id = _id; }
    inline qint32 toInt() const { return id; }
    inline bool isValid() const { return id > 0; }

    inline bool operator==(const SignedId &other) const { return id == other.id; }
    inline bool operator!=(const SignedId &other) const { return id != other.id; }
    inline bool operator<(const SignedId &other) const { return id < other.id; }
    inline bool operator<=(const SignedId &other) const { return id <= other.id; }
    inline bool operator>(const SignedId &other) const { return id > other.id; }
    inline bool operator>=(const SignedId &other) const { return id >= other.id; }
    inline bool operator==(int i) const { return id == i; }
    inline bool operator!=(int i) const { return id != i; }
    inline bool operator<(int i) const { return id < i; }
    inline bool operator>(int i) const { return id > i; }
    inline bool operator<=(int i) const { return id <= i; }

    inline SignedId operator++(int) { id++; return *this; }
    //inline operator int() const { return toInt(); } // no automatic conversion!

    friend QDataStream &operator>>(QDataStream &in, SignedId &signedId);
};

inline QDataStream &operator<<(QDataStream &out, const SignedId &signedId) { out << signedId.toInt(); return out; }
inline QDataStream &operator>>(QDataStream &in, SignedId &signedId) { in >> signedId.id; return in; }
inline QTextStream &operator<<(QTextStream &out, const SignedId &signedId) { out << QString::number(signedId.toInt()); return out; }
inline QDebug operator<<(QDebug dbg, const SignedId &signedId) { dbg.space() << signedId.toInt(); return dbg; }
inline uint qHash(const SignedId &id) { return qHash(id.toInt()); }

class SignedId64
{
protected:
    qint64 id;

public:
    inline SignedId64(qint64 _id = 0) { id = _id; }
    inline qint64 toQint64() const { return id; }
    inline bool isValid() const { return id > 0; }

    inline bool operator==(const SignedId64 &other) const { return id == other.id; }
    inline bool operator!=(const SignedId64 &other) const { return id != other.id; }
    inline bool operator<(const SignedId64 &other) const { return id < other.id; }
    inline bool operator<=(const SignedId64 &other) const { return id <= other.id; }
    inline bool operator>(const SignedId64 &other) const { return id > other.id; }
    inline bool operator>=(const SignedId64 &other) const { return id >= other.id; }
    inline bool operator==(qint64 i) const { return id == i; }
    inline bool operator!=(qint64 i) const { return id != i; }
    inline bool operator<(qint64 i) const { return id < i; }
    inline bool operator>(qint64 i) const { return id > i; }
    inline bool operator<=(qint64 i) const { return id <= i; }

    inline SignedId64 operator++(int) { id++; return *this; }
    //inline operator int() const { return toQint64(); } // no automatic conversion!

    friend QDataStream &operator>>(QDataStream &in, SignedId64 &signedId);
};

QDataStream &operator<<(QDataStream &out, const SignedId64 &signedId);
QDataStream &operator>>(QDataStream &in, SignedId64 &signedId);
inline QTextStream &operator<<(QTextStream &out, const SignedId64 &signedId) { out << QString::number(signedId.toQint64()); return out; }
inline QDebug operator<<(QDebug dbg, const SignedId64 &signedId) { dbg.space() << signedId.toQint64(); return dbg; }
inline uint qHash(const SignedId64 &id) { return qHash(id.toQint64()); }

struct UserId : public SignedId {
    inline UserId(int _id = 0) : SignedId(_id) {}
    //inline operator QVariant() const { return QVariant::fromValue(*this); }  // no automatic conversion!
};

struct MsgId : public SignedId64 {
    inline MsgId(qint64 _id = 0) : SignedId64(_id) {}
    //inline operator QVariant() const { return QVariant::fromValue(*this); }
};

struct BufferId : public SignedId {
    inline BufferId(int _id = 0) : SignedId(_id) {}
    //inline operator QVariant() const { return QVariant::fromValue(*this); }
};

struct NetworkId : public SignedId {
    inline NetworkId(int _id = 0) : SignedId(_id) {}
    //inline operator QVariant() const { return QVariant::fromValue(*this); }
};

struct IdentityId : public SignedId {
    inline IdentityId(int _id = 0) : SignedId(_id) {}
    //inline operator QVariant() const { return QVariant::fromValue(*this); }
};

struct AccountId : public SignedId {
    inline AccountId(int _id = 0) : SignedId(_id) {}
};

Q_DECLARE_METATYPE(UserId)
Q_DECLARE_METATYPE(MsgId)
Q_DECLARE_METATYPE(BufferId)
Q_DECLARE_METATYPE(NetworkId)
Q_DECLARE_METATYPE(IdentityId)
Q_DECLARE_METATYPE(AccountId)

Q_DECLARE_METATYPE(QHostAddress)

// a few typedefs
typedef QList<MsgId> MsgIdList;
typedef QList<BufferId> BufferIdList;

#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
/**
 * Catch-all stream serialization operator for enum types.
 *
 * @param[in,out] out   Stream to serialize to
 * @param[in]     value Value to serialize
 * @returns A reference to the stream
 */
template<typename T,
         typename = typename std::enable_if<std::is_enum<T>::value>::type>
QDataStream &operator<<(QDataStream &out, T value) {
    out << static_cast<typename std::underlying_type<T>::type>(value);
    return out;
}

/**
 * Catch-all stream serialization operator for enum types.
 *
 * @param[in,out] in    Stream to deserialize from
 * @param[out]    value Value to deserialize into
 * @returns A reference to the stream
 */
template<typename T,
         typename = typename std::enable_if<std::is_enum<T>::value>::type>
QDataStream &operator>>(QDataStream &in, T &value) {
    typename std::underlying_type<T>::type v;
    in >> v;
    value = static_cast<T>(v);
    return in;
}
#endif

// Exceptions

/**
 * Thrown during initialization to enforce exiting the application before the event loop is started
 */
struct ExitException
{
    int exitCode;
    QString errorString;

    ExitException(int code = EXIT_FAILURE, QString error = {})
        : exitCode(code), errorString(std::move(error)) {}
};