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