| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QTCORE_QEXCEPTION_H |
| 5 | #define QTCORE_QEXCEPTION_H |
| 6 | |
| 7 | #include <QtCore/qatomic.h> |
| 8 | #include <QtCore/qshareddata.h> |
| 9 | |
| 10 | #ifndef QT_NO_EXCEPTIONS |
| 11 | # include <exception> |
| 12 | #endif |
| 13 | |
| 14 | QT_REQUIRE_CONFIG(future); |
| 15 | |
| 16 | QT_BEGIN_NAMESPACE |
| 17 | |
| 18 | |
| 19 | #if !defined(QT_NO_EXCEPTIONS) || defined(Q_QDOC) |
| 20 | |
| 21 | class Q_CORE_EXPORT QException : public std::exception |
| 22 | { |
| 23 | public: |
| 24 | QException() = default; |
| 25 | ~QException() noexcept; |
| 26 | QException(const QException &) = default; |
| 27 | QException &operator=(const QException &) = default; |
| 28 | virtual void raise() const; |
| 29 | virtual QException *clone() const; |
| 30 | }; |
| 31 | |
| 32 | class QUnhandledExceptionPrivate; |
| 33 | class Q_CORE_EXPORT QUnhandledException final : public QException |
| 34 | { |
| 35 | public: |
| 36 | QUnhandledException(std::exception_ptr exception = nullptr) noexcept; |
| 37 | ~QUnhandledException() noexcept override; |
| 38 | |
| 39 | QUnhandledException(QUnhandledException &&other) noexcept; |
| 40 | QUnhandledException(const QUnhandledException &other) noexcept; |
| 41 | |
| 42 | void swap(QUnhandledException &other) noexcept { d.swap(other.d); } |
| 43 | |
| 44 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QUnhandledException) |
| 45 | QUnhandledException &operator=(const QUnhandledException &other) noexcept; |
| 46 | |
| 47 | void raise() const override; |
| 48 | QUnhandledException *clone() const override; |
| 49 | |
| 50 | std::exception_ptr exception() const; |
| 51 | |
| 52 | private: |
| 53 | QSharedDataPointer<QUnhandledExceptionPrivate> d; |
| 54 | }; |
| 55 | |
| 56 | namespace QtPrivate { |
| 57 | |
| 58 | class Q_CORE_EXPORT ExceptionStore |
| 59 | { |
| 60 | public: |
| 61 | void setException(const QException &e); |
| 62 | void setException(std::exception_ptr e); |
| 63 | bool hasException() const; |
| 64 | std::exception_ptr exception() const; |
| 65 | void throwPossibleException(); |
| 66 | Q_NORETURN void rethrowException() const; |
| 67 | std::exception_ptr exceptionHolder; |
| 68 | }; |
| 69 | |
| 70 | } // namespace QtPrivate |
| 71 | |
| 72 | #else // QT_NO_EXCEPTIONS |
| 73 | |
| 74 | namespace QtPrivate { |
| 75 | |
| 76 | class Q_CORE_EXPORT ExceptionStore |
| 77 | { |
| 78 | public: |
| 79 | ExceptionStore() { } |
| 80 | inline void throwPossibleException() {} |
| 81 | inline void rethrowException() const { } |
| 82 | }; |
| 83 | |
| 84 | } // namespace QtPrivate |
| 85 | |
| 86 | #endif // QT_NO_EXCEPTIONS |
| 87 | |
| 88 | QT_END_NAMESPACE |
| 89 | |
| 90 | #endif |
| 91 | |