X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Ftypes.h;h=fb85012d47b3e0a61a66be742b2d6bb20001116b;hp=290d53f6cf83018b5f31800556a390f3fb9bbc60;hb=900cce213a6ed000b7131a05a0dec7d04b35b023;hpb=84cd3561e97167ffb98ecab0fd2b884ba1d13ada diff --git a/src/common/types.h b/src/common/types.h index 290d53f6..fb85012d 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-2014 by the Quassel Project * + * Copyright (C) 2005-2018 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -18,8 +18,9 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ -#ifndef TYPES_H_ -#define TYPES_H_ +#pragma once + +#include #include #include @@ -29,12 +30,6 @@ #include #include -#include "peer.h" - -// We need to special-case Peer* in attached signals/slots, so typedef it for the meta type system -typedef Peer * PeerPtr; -Q_DECLARE_METATYPE(PeerPtr) - class SignedId { protected: @@ -63,20 +58,53 @@ public: friend QDataStream &operator>>(QDataStream &in, SignedId &signedId); }; - inline QDataStream &operator<<(QDataStream &out, const SignedId &signedId) { out << signedId.toInt(); return out; } inline QDataStream &operator>>(QDataStream &in, SignedId &signedId) { in >> signedId.id; return in; } inline QTextStream &operator<<(QTextStream &out, const SignedId &signedId) { out << QString::number(signedId.toInt()); return out; } inline QDebug operator<<(QDebug dbg, const SignedId &signedId) { dbg.space() << signedId.toInt(); return dbg; } inline uint qHash(const SignedId &id) { return qHash(id.toInt()); } +class SignedId64 +{ +protected: + qint64 id; + +public: + inline SignedId64(qint64 _id = 0) { id = _id; } + inline qint64 toQint64() const { return id; } + inline bool isValid() const { return id > 0; } + + inline bool operator==(const SignedId64 &other) const { return id == other.id; } + inline bool operator!=(const SignedId64 &other) const { return id != other.id; } + inline bool operator<(const SignedId64 &other) const { return id < other.id; } + inline bool operator<=(const SignedId64 &other) const { return id <= other.id; } + inline bool operator>(const SignedId64 &other) const { return id > other.id; } + inline bool operator>=(const SignedId64 &other) const { return id >= other.id; } + inline bool operator==(qint64 i) const { return id == i; } + inline bool operator!=(qint64 i) const { return id != i; } + inline bool operator<(qint64 i) const { return id < i; } + inline bool operator>(qint64 i) const { return id > i; } + inline bool operator<=(qint64 i) const { return id <= i; } + + inline SignedId64 operator++(int) { id++; return *this; } + //inline operator int() const { return toQint64(); } // no automatic conversion! + + friend QDataStream &operator>>(QDataStream &in, SignedId64 &signedId); +}; + +QDataStream &operator<<(QDataStream &out, const SignedId64 &signedId); +QDataStream &operator>>(QDataStream &in, SignedId64 &signedId); +inline QTextStream &operator<<(QTextStream &out, const SignedId64 &signedId) { out << QString::number(signedId.toQint64()); return out; } +inline QDebug operator<<(QDebug dbg, const SignedId64 &signedId) { dbg.space() << signedId.toQint64(); return dbg; } +inline uint qHash(const SignedId64 &id) { return qHash(id.toQint64()); } + struct UserId : public SignedId { inline UserId(int _id = 0) : SignedId(_id) {} //inline operator QVariant() const { return QVariant::fromValue(*this); } // no automatic conversion! }; -struct MsgId : public SignedId { - inline MsgId(int _id = 0) : SignedId(_id) {} +struct MsgId : public SignedId64 { + inline MsgId(qint64 _id = 0) : SignedId64(_id) {} //inline operator QVariant() const { return QVariant::fromValue(*this); } }; @@ -109,17 +137,49 @@ Q_DECLARE_METATYPE(AccountId) Q_DECLARE_METATYPE(QHostAddress) // a few typedefs -typedef QList MsgIdList; -typedef QList BufferIdList; - -//! Base class for exceptions. -struct Exception { - Exception(QString msg = "Unknown Exception") : _msg(msg) {} - virtual ~Exception() {} // make gcc happy - virtual inline QString msg() { return _msg; } +using MsgIdList = QList; +using BufferIdList = QList; + +/** + * Catch-all stream serialization operator for enum types. + * + * @param[in,out] out Stream to serialize to + * @param[in] value Value to serialize + * @returns A reference to the stream + */ +template::value>::type> +QDataStream &operator<<(QDataStream &out, T value) { + out << static_cast::type>(value); + return out; +} + +/** + * Catch-all stream serialization operator for enum types. + * + * @param[in,out] in Stream to deserialize from + * @param[out] value Value to deserialize into + * @returns A reference to the stream + */ +template::value>::type> +QDataStream &operator>>(QDataStream &in, T &value) { + typename std::underlying_type::type v; + in >> v; + value = static_cast(v); + return in; +} + +// Exceptions + +/** + * Thrown during initialization to enforce exiting the application before the event loop is started + */ +struct ExitException +{ + int exitCode; + QString errorString; -protected: - QString _msg; + ExitException(int code = EXIT_FAILURE, QString error = {}) + : exitCode(code), errorString(std::move(error)) {} }; - -#endif