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