aboutsummaryrefslogtreecommitdiffstats
path: root/include/qdotnetexception.h
blob: 563c66a839cde1f1b2dad1275cb5bfd58b59214f (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/***************************************************************************************************
 Copyright (C) 2023 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

#pragma once

#include "qdotnetref.h"

#ifdef __GNUC__
#   pragma GCC diagnostic push
#   pragma GCC diagnostic ignored "-Wconversion"
#endif
#include <QException>
#include <QString>
#ifdef __GNUC__
#   pragma GCC diagnostic pop
#endif

class QDotNetException : public QException, public QDotNetRef
{
public:
    QDotNetException()
        : QDotNetException({ "System.Exception" })
    {}

    QDotNetException(const QString &type)
    {
        const QDotNetFunction<QDotNetException> ctor = adapter().resolveConstructor({ type });
        *this = ctor();
    }

    QDotNetException(const QString &type, const QString &message)
    {
        const QDotNetFunction<QDotNetException, QString> ctor = adapter().resolveConstructor(
            { type, QDotNetTypeOf<QString>::MarshalAs });
        *this = ctor(message);
    }

    QDotNetException(const void *objRef = nullptr)
        : QDotNetRef(objRef)
    {}

    QDotNetException(const QDotNetException &cpySrc)
        : QDotNetRef(adapter().addObjectRef(&cpySrc))
    {}

    QDotNetException &operator =(const QDotNetException &cpySrc)
    {
        QDotNetRef::operator=(cpySrc);
        return *this;
    }

    QDotNetException(QDotNetException &&movSrc) noexcept
        : QDotNetRef(std::move(movSrc))
    {}

    void raise() const override { throw *this; }
    QDotNetException *clone() const override { return new QDotNetException(*this); }

    QDotNetException &operator=(QDotNetException &&movSrc) noexcept
    {
        QDotNetRef::operator=(std::move(movSrc));
        return *this;
    }

    template<typename TResult, typename ...TArg>
    QDotNetFunction<TResult, TArg...> method(const QString &methodName,
        QDotNetFunction<TResult, TArg...> &func) const
    {
        if (!func.isValid()) {
            const QList<QDotNetParameter> parameters
            {
                QDotNetInbound<TResult>::Parameter,
                QDotNetOutbound<TArg>::Parameter...
            };
            func = adapter().resolveInstanceMethod(*this, methodName, parameters);
        }
        return func;
    }

    qint32 hResult() const
    {
        return method("get_HResult", fnHResult).invoke(*this);
    }

    QDotNetException innerException() const
    {
        return method("get_InnerException", fnInnerException).invoke(*this)
            .cast<QDotNetException>();
    }

    QString message() const
    {
        return method("get_Message", fnMessage).invoke(*this);
    }

    QString source() const
    {
        return method("get_Source", fnSource).invoke(*this);
    }

    QString stackTrace() const
    {
        return method("get_StackTrace", fnStackTrace).invoke(*this);
    }

    QDotNetRef type() const
    {
        return method("GetType", fnGetType).invoke(*this);
    }

    QString toString() const
    {
        return method("ToString", fnToString).invoke(*this);
    }

    bool equals(const QDotNetRef &obj) const
    {
        return method("Equals", fnEquals).invoke(*this, obj);
    }

private:
    mutable QDotNetFunction<qint32> fnHResult = nullptr;
    mutable QDotNetFunction<QDotNetRef> fnInnerException = nullptr;
    mutable QDotNetFunction<QString> fnMessage = nullptr;
    mutable QDotNetFunction<QString> fnSource = nullptr;
    mutable QDotNetFunction<QString> fnStackTrace = nullptr;
    mutable QDotNetFunction<QDotNetRef> fnGetType = nullptr;
    mutable QDotNetFunction<QString> fnToString = nullptr;
    mutable QDotNetFunction<bool, QDotNetRef> fnEquals = nullptr;
};