290d53f6cf83018b5f31800556a390f3fb9bbc60
[quassel.git] / src / common / types.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 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 #ifndef TYPES_H_
22 #define TYPES_H_
23
24 #include <QDebug>
25 #include <QString>
26 #include <QVariant>
27 #include <QDataStream>
28 #include <QTextStream>
29 #include <QHostAddress>
30 #include <QDataStream>
31
32 #include "peer.h"
33
34 // We need to special-case Peer* in attached signals/slots, so typedef it for the meta type system
35 typedef Peer * PeerPtr;
36 Q_DECLARE_METATYPE(PeerPtr)
37
38 class SignedId
39 {
40 protected:
41     qint32 id;
42
43 public:
44     inline SignedId(int _id = 0) { id = _id; }
45     inline qint32 toInt() const { return id; }
46     inline bool isValid() const { return id > 0; }
47
48     inline bool operator==(const SignedId &other) const { return id == other.id; }
49     inline bool operator!=(const SignedId &other) const { return id != other.id; }
50     inline bool operator<(const SignedId &other) const { return id < other.id; }
51     inline bool operator<=(const SignedId &other) const { return id <= other.id; }
52     inline bool operator>(const SignedId &other) const { return id > other.id; }
53     inline bool operator>=(const SignedId &other) const { return id >= other.id; }
54     inline bool operator==(int i) const { return id == i; }
55     inline bool operator!=(int i) const { return id != i; }
56     inline bool operator<(int i) const { return id < i; }
57     inline bool operator>(int i) const { return id > i; }
58     inline bool operator<=(int i) const { return id <= i; }
59
60     inline SignedId operator++(int) { id++; return *this; }
61     //inline operator int() const { return toInt(); } // no automatic conversion!
62
63     friend QDataStream &operator>>(QDataStream &in, SignedId &signedId);
64 };
65
66
67 inline QDataStream &operator<<(QDataStream &out, const SignedId &signedId) { out << signedId.toInt(); return out; }
68 inline QDataStream &operator>>(QDataStream &in, SignedId &signedId) { in >> signedId.id; return in; }
69 inline QTextStream &operator<<(QTextStream &out, const SignedId &signedId) { out << QString::number(signedId.toInt()); return out; }
70 inline QDebug operator<<(QDebug dbg, const SignedId &signedId) { dbg.space() << signedId.toInt(); return dbg; }
71 inline uint qHash(const SignedId &id) { return qHash(id.toInt()); }
72
73 struct UserId : public SignedId {
74     inline UserId(int _id = 0) : SignedId(_id) {}
75     //inline operator QVariant() const { return QVariant::fromValue<UserId>(*this); }  // no automatic conversion!
76 };
77
78 struct MsgId : public SignedId {
79     inline MsgId(int _id = 0) : SignedId(_id) {}
80     //inline operator QVariant() const { return QVariant::fromValue<MsgId>(*this); }
81 };
82
83 struct BufferId : public SignedId {
84     inline BufferId(int _id = 0) : SignedId(_id) {}
85     //inline operator QVariant() const { return QVariant::fromValue<BufferId>(*this); }
86 };
87
88 struct NetworkId : public SignedId {
89     inline NetworkId(int _id = 0) : SignedId(_id) {}
90     //inline operator QVariant() const { return QVariant::fromValue<NetworkId>(*this); }
91 };
92
93 struct IdentityId : public SignedId {
94     inline IdentityId(int _id = 0) : SignedId(_id) {}
95     //inline operator QVariant() const { return QVariant::fromValue<IdentityId>(*this); }
96 };
97
98 struct AccountId : public SignedId {
99     inline AccountId(int _id = 0) : SignedId(_id) {}
100 };
101
102 Q_DECLARE_METATYPE(UserId)
103 Q_DECLARE_METATYPE(MsgId)
104 Q_DECLARE_METATYPE(BufferId)
105 Q_DECLARE_METATYPE(NetworkId)
106 Q_DECLARE_METATYPE(IdentityId)
107 Q_DECLARE_METATYPE(AccountId)
108
109 Q_DECLARE_METATYPE(QHostAddress)
110
111 // a few typedefs
112 typedef QList<MsgId> MsgIdList;
113 typedef QList<BufferId> BufferIdList;
114
115 //! Base class for exceptions.
116 struct Exception {
117     Exception(QString msg = "Unknown Exception") : _msg(msg) {}
118     virtual ~Exception() {} // make gcc happy
119     virtual inline QString msg() { return _msg; }
120
121 protected:
122     QString _msg;
123 };
124
125 #endif