/*************************************************************************************************** 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 #include #ifdef __GNUC__ # pragma GCC diagnostic pop #endif class QDotNetException : public QException, public QDotNetRef { public: QDotNetException() : QDotNetException({ "System.Exception" }) {} QDotNetException(const QString &type) { const QDotNetFunction ctor = adapter().resolveConstructor({ type }); *this = ctor(); } QDotNetException(const QString &type, const QString &message) { const QDotNetFunction ctor = adapter().resolveConstructor( { type, QDotNetTypeOf::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 QDotNetFunction method(const QString &methodName, QDotNetFunction &func) const { if (!func.isValid()) { const QList parameters { QDotNetInbound::Parameter, QDotNetOutbound::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(); } 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 fnHResult = nullptr; mutable QDotNetFunction fnInnerException = nullptr; mutable QDotNetFunction fnMessage = nullptr; mutable QDotNetFunction fnSource = nullptr; mutable QDotNetFunction fnStackTrace = nullptr; mutable QDotNetFunction fnGetType = nullptr; mutable QDotNetFunction fnToString = nullptr; mutable QDotNetFunction fnEquals = nullptr; };