4139aa4c88961b9ce80aa398c52ff00c387324f7
[quassel.git] / src / common / types.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 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 <QTextStream>
28 #include <QHostAddress>
29 #include <QDataStream>
30
31 class SignedId
32 {
33 protected:
34     qint32 id;
35
36 public:
37     inline SignedId(int _id = 0) { id = _id; }
38     inline qint32 toInt() const { return id; }
39     inline bool isValid() const { return id > 0; }
40
41     inline bool operator==(const SignedId &other) const { return id == other.id; }
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==(int i) const { return id == i; }
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
53     inline SignedId operator++(int) { id++; return *this; }
54     //inline operator int() const { return toInt(); } // no automatic conversion!
55
56     friend QDataStream &operator>>(QDataStream &in, SignedId &signedId);
57 };
58
59
60 inline QDataStream &operator<<(QDataStream &out, const SignedId &signedId) { out << signedId.toInt(); return out; }
61 inline QDataStream &operator>>(QDataStream &in, SignedId &signedId) { in >> signedId.id; return in; }
62 inline QTextStream &operator<<(QTextStream &out, const SignedId &signedId) { out << QString::number(signedId.toInt()); return out; }
63 inline QDebug operator<<(QDebug dbg, const SignedId &signedId) { dbg.space() << signedId.toInt(); return dbg; }
64 inline uint qHash(const SignedId &id) { return qHash(id.toInt()); }
65
66 struct UserId : public SignedId {
67     inline UserId(int _id = 0) : SignedId(_id) {}
68     //inline operator QVariant() const { return QVariant::fromValue<UserId>(*this); }  // no automatic conversion!
69 };
70
71 struct MsgId : public SignedId {
72     inline MsgId(int _id = 0) : SignedId(_id) {}
73     //inline operator QVariant() const { return QVariant::fromValue<MsgId>(*this); }
74 };
75
76 struct BufferId : public SignedId {
77     inline BufferId(int _id = 0) : SignedId(_id) {}
78     //inline operator QVariant() const { return QVariant::fromValue<BufferId>(*this); }
79 };
80
81 struct NetworkId : public SignedId {
82     inline NetworkId(int _id = 0) : SignedId(_id) {}
83     //inline operator QVariant() const { return QVariant::fromValue<NetworkId>(*this); }
84 };
85
86 struct IdentityId : public SignedId {
87     inline IdentityId(int _id = 0) : SignedId(_id) {}
88     //inline operator QVariant() const { return QVariant::fromValue<IdentityId>(*this); }
89 };
90
91 struct AccountId : public SignedId {
92     inline AccountId(int _id = 0) : SignedId(_id) {}
93 };
94
95 Q_DECLARE_METATYPE(UserId)
96 Q_DECLARE_METATYPE(MsgId)
97 Q_DECLARE_METATYPE(BufferId)
98 Q_DECLARE_METATYPE(NetworkId)
99 Q_DECLARE_METATYPE(IdentityId)
100 Q_DECLARE_METATYPE(AccountId)
101
102 Q_DECLARE_METATYPE(QHostAddress)
103
104 // a few typedefs
105 typedef QList<MsgId> MsgIdList;
106 typedef QList<BufferId> BufferIdList;
107
108 //! Base class for exceptions.
109 struct Exception {
110     Exception(QString msg = "Unknown Exception") : _msg(msg) {}
111     virtual ~Exception() {} // make gcc happy
112     virtual inline QString msg() { return _msg; }
113
114 protected:
115     QString _msg;
116 };
117
118 #endif