diff options
| author | Linus Jahn <lnj@kaidan.im> | 2020-07-11 22:36:38 +0200 |
|---|---|---|
| committer | LNJ <lnj@kaidan.im> | 2020-07-12 21:32:41 +0200 |
| commit | d1978692b8cde1e5d785e31e4d443ae7a52f143f (patch) | |
| tree | 2907a7900ecd3402ba8acfd4707ae69f71b49454 /src/base/QXmppStanza_p.h | |
| parent | 317c87325b709fc6b5a1b363120a57299092a80f (diff) | |
| download | qxmpp-d1978692b8cde1e5d785e31e4d443ae7a52f143f.tar.gz | |
QXmppStanza::Error: Use std::optional<> internally
This makes the variables for the error type and condition an
std::optional<> as this makes the meaning clearer than hidden -1 values
created by dubious casts.
For now, the API is not changed, because we can't replace the getter
easily. We could do something like type() and optionalType().
Diffstat (limited to 'src/base/QXmppStanza_p.h')
| -rw-r--r-- | src/base/QXmppStanza_p.h | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/src/base/QXmppStanza_p.h b/src/base/QXmppStanza_p.h index 03875639..89fa3209 100644 --- a/src/base/QXmppStanza_p.h +++ b/src/base/QXmppStanza_p.h @@ -26,6 +26,8 @@ #include "QXmppStanza.h" +#include <optional> + // W A R N I N G // ------------- // @@ -38,7 +40,7 @@ // We mean it. // -static QString strFromCondition(const QXmppStanza::Error::Condition& condition) +static QString conditionToString(QXmppStanza::Error::Condition condition) { switch (condition) { case QXmppStanza::Error::BadRequest: @@ -87,12 +89,11 @@ static QString strFromCondition(const QXmppStanza::Error::Condition& condition) return "undefined-condition"; case QXmppStanza::Error::UnexpectedRequest: return "unexpected-request"; - default: - return ""; } + return {}; } -static QXmppStanza::Error::Condition conditionFromStr(const QString& string) +static std::optional<QXmppStanza::Error::Condition> conditionFromString(const QString &string) { if (string == "bad-request") return QXmppStanza::Error::BadRequest; @@ -140,8 +141,39 @@ static QXmppStanza::Error::Condition conditionFromStr(const QString& string) return QXmppStanza::Error::UndefinedCondition; else if (string == "unexpected-request") return QXmppStanza::Error::UnexpectedRequest; - else - return static_cast<QXmppStanza::Error::Condition>(-1); + return std::nullopt; +} + +static QString typeToString(QXmppStanza::Error::Type type) +{ + switch (type) { + case QXmppStanza::Error::Cancel: + return QStringLiteral("cancel"); + case QXmppStanza::Error::Continue: + return QStringLiteral("continue"); + case QXmppStanza::Error::Modify: + return QStringLiteral("modify"); + case QXmppStanza::Error::Auth: + return QStringLiteral("auth"); + case QXmppStanza::Error::Wait: + return QStringLiteral("wait"); + } + return {}; +} + +static std::optional<QXmppStanza::Error::Type> typeFromString(const QString &string) +{ + if (string == QStringLiteral("cancel")) + return QXmppStanza::Error::Cancel; + else if (string == QStringLiteral("continue")) + return QXmppStanza::Error::Continue; + else if (string == QStringLiteral("modify")) + return QXmppStanza::Error::Modify; + else if (string == QStringLiteral("auth")) + return QXmppStanza::Error::Auth; + else if (string == QStringLiteral("wait")) + return QXmppStanza::Error::Wait; + return std::nullopt; } #endif |
