64bfbbc784860e4993807a1977ac67eea873566c
[quassel.git] / src / common / types.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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
28 class SignedId {
29   protected:
30     qint32 id;
31
32   public:
33     inline SignedId(int _id = 0) { id = _id; }
34     inline qint32 toInt() const { return id; }
35
36     inline bool operator==(const SignedId &other) const { return id == other.id; }
37     inline bool operator!=(const SignedId &other) const { return id != other.id; }
38     inline bool operator<(const SignedId &other) const { return id < other.id; }
39     inline bool operator==(int i) const { return id == i; }
40     inline bool operator!=(int i) const { return id != i; }
41     inline bool operator<(int i) const { return id < i; }
42     inline bool operator>(int i) const { return id > i; }
43     inline bool operator<=(int i) const { return id <= i; }
44
45     inline SignedId operator++(int) { id++; return *this; }
46     //inline operator int() const { return toInt(); } // no automatic conversion!
47
48     friend QDataStream &operator>>(QDataStream &in, SignedId &signedId);
49 };
50
51 inline QDataStream &operator<<(QDataStream &out, const SignedId &signedId) { out << signedId.toInt(); return out; }
52 inline QDataStream &operator>>(QDataStream &in, SignedId &signedId) { in >> signedId.id; return in; }
53 inline QDebug operator<<(QDebug dbg, const SignedId &signedId) { dbg.space() << signedId.toInt(); return dbg; }
54 inline uint qHash(const SignedId &id) { return qHash(id.toInt()); }
55
56 struct UserId : public SignedId {
57   inline UserId(int _id = 0) : SignedId(_id) {};
58   //inline operator QVariant() const { return QVariant::fromValue<UserId>(*this); }  // no automatic conversion!
59 };
60
61 struct MsgId : public SignedId {
62   inline MsgId(int _id = 0) : SignedId(_id) {};
63   //inline operator QVariant() const { return QVariant::fromValue<MsgId>(*this); }
64 };
65
66 struct BufferId : public SignedId {
67   inline BufferId(int _id = 0) : SignedId(_id) {};
68   //inline operator QVariant() const { return QVariant::fromValue<BufferId>(*this); }
69 };
70
71 struct NetworkId : public SignedId {
72   inline NetworkId(int _id = 0) : SignedId(_id) {};
73   //inline operator QVariant() const { return QVariant::fromValue<NetworkId>(*this); }
74 };
75
76 struct IdentityId : public SignedId {
77   inline IdentityId(int _id = 0) : SignedId(_id) {};
78   //inline operator QVariant() const { return QVariant::fromValue<IdentityId>(*this); }
79 };
80
81 Q_DECLARE_METATYPE(UserId);
82 Q_DECLARE_METATYPE(MsgId);
83 Q_DECLARE_METATYPE(BufferId);
84 Q_DECLARE_METATYPE(NetworkId);
85 Q_DECLARE_METATYPE(IdentityId);
86
87 //! Base class for exceptions.
88 struct Exception {
89   Exception(QString msg = "Unknown Exception") : _msg(msg) {};
90   virtual ~Exception() {}; // make gcc happy
91   virtual inline QString msg() { return _msg; }
92
93   protected:
94     QString _msg;
95
96 };
97
98 #endif