aboutsummaryrefslogtreecommitdiff
path: root/src/base/QXmppError.h
blob: 1433bb8f1bc3f918becfb7cbe0a7fa15d91bb14c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#ifndef QXMPPERROR_H
#define QXMPPERROR_H

#include "QXmppGlobal.h"

#include <any>
#include <optional>

class QFileDevice;
class QIODevice;
class QNetworkReply;

struct QXMPP_EXPORT QXmppError
{
    QString description;
    std::any error;

    static QXmppError fromIoDevice(const QIODevice &device);
    static QXmppError fromNetworkReply(const QNetworkReply &reply);
    static QXmppError fromFileDevice(const QFileDevice &file);

    bool isFileError() const;
    bool isNetworkError() const;
    bool isStanzaError() const;

    template<typename T>
    bool holdsType() const
    {
        return error.type().hash_code() == typeid(T).hash_code();
    }
    template<typename T>
    std::optional<T> value() const
    {
        // any_cast always checks this, to avoid an additional check we use exceptions
        try {
            return std::any_cast<T>(error);
        } catch (std::bad_any_cast) {
            return {};
        }
    }
    template<typename T>
    std::optional<T> takeValue()
    {
        // we can't use unchecked any_cast with moving because we can't access the error after a
        // failed any_cast
        if (error.type().hash_code() == typeid(T).hash_code()) {
            auto value = std::any_cast<T>(std::move(error));
            error = std::any();
            return value;
        }
        return {};
    }
};

#endif  // QXMPPERROR_H