| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/thread/qexception.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||
| 2 | ** | - | ||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
| 5 | ** | - | ||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||
| 7 | ** | - | ||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
| 9 | ** Commercial License Usage | - | ||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
| 16 | ** | - | ||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
| 19 | ** General Public License version 3 as published by the Free Software | - | ||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
| 24 | ** | - | ||||||
| 25 | ** GNU General Public License Usage | - | ||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
| 35 | ** | - | ||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||
| 37 | ** | - | ||||||
| 38 | ****************************************************************************/ | - | ||||||
| 39 | - | |||||||
| 40 | #include "qexception.h" | - | ||||||
| 41 | #include "QtCore/qshareddata.h" | - | ||||||
| 42 | - | |||||||
| 43 | #ifndef QT_NO_QFUTURE | - | ||||||
| 44 | #ifndef QT_NO_EXCEPTIONS | - | ||||||
| 45 | - | |||||||
| 46 | QT_BEGIN_NAMESPACE | - | ||||||
| 47 | - | |||||||
| 48 | /*! | - | ||||||
| 49 | \class QException | - | ||||||
| 50 | \inmodule QtCore | - | ||||||
| 51 | \brief The QException class provides a base class for exceptions that can transferred across threads. | - | ||||||
| 52 | \since 5.0 | - | ||||||
| 53 | - | |||||||
| 54 | Qt Concurrent supports throwing and catching exceptions across thread | - | ||||||
| 55 | boundaries, provided that the exception inherit from QException | - | ||||||
| 56 | and implement two helper functions: | - | ||||||
| 57 | - | |||||||
| 58 | \snippet code/src_corelib_thread_qexception.cpp 0 | - | ||||||
| 59 | - | |||||||
| 60 | QException subclasses must be thrown by value and | - | ||||||
| 61 | caught by reference: | - | ||||||
| 62 | - | |||||||
| 63 | \snippet code/src_corelib_thread_qexception.cpp 1 | - | ||||||
| 64 | - | |||||||
| 65 | If you throw an exception that is not a subclass of QException, | - | ||||||
| 66 | the Qt functions will throw a QUnhandledException | - | ||||||
| 67 | in the receiver thread. | - | ||||||
| 68 | - | |||||||
| 69 | When using QFuture, transferred exceptions will be thrown when calling the following functions: | - | ||||||
| 70 | \list | - | ||||||
| 71 | \li QFuture::waitForFinished() | - | ||||||
| 72 | \li QFuture::result() | - | ||||||
| 73 | \li QFuture::resultAt() | - | ||||||
| 74 | \li QFuture::results() | - | ||||||
| 75 | \endlist | - | ||||||
| 76 | */ | - | ||||||
| 77 | - | |||||||
| 78 | /*! | - | ||||||
| 79 | \fn QException::raise() const | - | ||||||
| 80 | In your QException subclass, reimplement raise() like this: | - | ||||||
| 81 | - | |||||||
| 82 | \snippet code/src_corelib_thread_qexception.cpp 2 | - | ||||||
| 83 | */ | - | ||||||
| 84 | - | |||||||
| 85 | /*! | - | ||||||
| 86 | \fn QException::clone() const | - | ||||||
| 87 | In your QException subclass, reimplement clone() like this: | - | ||||||
| 88 | - | |||||||
| 89 | \snippet code/src_corelib_thread_qexception.cpp 3 | - | ||||||
| 90 | */ | - | ||||||
| 91 | - | |||||||
| 92 | /*! | - | ||||||
| 93 | \class QUnhandledException | - | ||||||
| 94 | \inmodule QtCore | - | ||||||
| 95 | - | |||||||
| 96 | \brief The UnhandledException class represents an unhandled exception in a worker thread. | - | ||||||
| 97 | \since 5.0 | - | ||||||
| 98 | - | |||||||
| 99 | If a worker thread throws an exception that is not a subclass of QException, | - | ||||||
| 100 | the Qt functions will throw a QUnhandledException | - | ||||||
| 101 | on the receiver thread side. | - | ||||||
| 102 | - | |||||||
| 103 | Inheriting from this class is not supported. | - | ||||||
| 104 | */ | - | ||||||
| 105 | - | |||||||
| 106 | /*! | - | ||||||
| 107 | \fn QUnhandledException::raise() const | - | ||||||
| 108 | \internal | - | ||||||
| 109 | */ | - | ||||||
| 110 | - | |||||||
| 111 | /*! | - | ||||||
| 112 | \fn QUnhandledException::clone() const | - | ||||||
| 113 | \internal | - | ||||||
| 114 | */ | - | ||||||
| 115 | - | |||||||
| 116 | QException::~QException() | - | ||||||
| 117 | #ifdef Q_COMPILER_NOEXCEPT | - | ||||||
| 118 | noexcept | - | ||||||
| 119 | #else | - | ||||||
| 120 | throw() | - | ||||||
| 121 | #endif | - | ||||||
| 122 | { | - | ||||||
| 123 | // must stay empty until ### Qt 6 | - | ||||||
| 124 | } | - | ||||||
| 125 | - | |||||||
| 126 | void QException::raise() const | - | ||||||
| 127 | { | - | ||||||
| 128 | QException e = *this; | - | ||||||
| 129 | throw e; executed 16 times by 4 tests: throw e;Executed by:
| 16 | ||||||
| 130 | } | - | ||||||
| 131 | - | |||||||
| 132 | QException *QException::clone() const | - | ||||||
| 133 | { | - | ||||||
| 134 | return new QException(*this); executed 16 times by 4 tests: return new QException(*this);Executed by:
| 16 | ||||||
| 135 | } | - | ||||||
| 136 | - | |||||||
| 137 | QUnhandledException::~QUnhandledException() | - | ||||||
| 138 | #ifdef Q_COMPILER_NOEXCEPT | - | ||||||
| 139 | noexcept | - | ||||||
| 140 | #else | - | ||||||
| 141 | throw() | - | ||||||
| 142 | #endif | - | ||||||
| 143 | { | - | ||||||
| 144 | // must stay empty until ### Qt 6 | - | ||||||
| 145 | } | - | ||||||
| 146 | - | |||||||
| 147 | void QUnhandledException::raise() const | - | ||||||
| 148 | { | - | ||||||
| 149 | QUnhandledException e = *this; | - | ||||||
| 150 | throw e; executed 3 times by 1 test: throw e;Executed by:
| 3 | ||||||
| 151 | } | - | ||||||
| 152 | - | |||||||
| 153 | QUnhandledException *QUnhandledException::clone() const | - | ||||||
| 154 | { | - | ||||||
| 155 | return new QUnhandledException(*this); executed 3 times by 1 test: return new QUnhandledException(*this);Executed by:
| 3 | ||||||
| 156 | } | - | ||||||
| 157 | - | |||||||
| 158 | #ifndef Q_QDOC | - | ||||||
| 159 | - | |||||||
| 160 | namespace QtPrivate { | - | ||||||
| 161 | - | |||||||
| 162 | class Base : public QSharedData | - | ||||||
| 163 | { | - | ||||||
| 164 | public: | - | ||||||
| 165 | Base(QException *exception) | - | ||||||
| 166 | : exception(exception), hasThrown(false) { } executed 21 times by 4 tests: end of blockExecuted by:
| 21 | ||||||
| 167 | ~Base() { delete exception; } executed 21 times by 4 tests: end of blockExecuted by:
| 21 | ||||||
| 168 | - | |||||||
| 169 | QException *exception; | - | ||||||
| 170 | bool hasThrown; | - | ||||||
| 171 | }; | - | ||||||
| 172 | - | |||||||
| 173 | ExceptionHolder::ExceptionHolder(QException *exception) | - | ||||||
| 174 | : executed 209799 times by 12 tests: base(exception ? new Base(exception) : Q_NULLPTR) {}end of blockExecuted by:
executed 209799 times by 12 tests: end of blockExecuted by:
| 209799 | ||||||
| 175 | - | |||||||
| 176 | ExceptionHolder::ExceptionHolder(const ExceptionHolder &other) | - | ||||||
| 177 | : base(other.base) | - | ||||||
| 178 | {} never executed: end of block | 0 | ||||||
| 179 | - | |||||||
| 180 | void ExceptionHolder::operator=(const ExceptionHolder &other) | - | ||||||
| 181 | { | - | ||||||
| 182 | base = other.base; | - | ||||||
| 183 | } executed 21 times by 4 tests: end of blockExecuted by:
| 21 | ||||||
| 184 | - | |||||||
| 185 | ExceptionHolder::~ExceptionHolder() | - | ||||||
| 186 | {} | - | ||||||
| 187 | - | |||||||
| 188 | QException *ExceptionHolder::exception() const | - | ||||||
| 189 | { | - | ||||||
| 190 | if (!base)
| 46-233369 | ||||||
| 191 | return Q_NULLPTR; executed 233369 times by 12 tests: return nullptr;Executed by:
| 233369 | ||||||
| 192 | return base->exception; executed 46 times by 4 tests: return base->exception;Executed by:
| 46 | ||||||
| 193 | } | - | ||||||
| 194 | - | |||||||
| 195 | void ExceptionStore::setException(const QException &e) | - | ||||||
| 196 | { | - | ||||||
| 197 | if (hasException() == false)
| 4-21 | ||||||
| 198 | exceptionHolder = ExceptionHolder(e.clone()); executed 21 times by 4 tests: exceptionHolder = ExceptionHolder(e.clone());Executed by:
| 21 | ||||||
| 199 | } executed 25 times by 4 tests: end of blockExecuted by:
| 25 | ||||||
| 200 | - | |||||||
| 201 | bool ExceptionStore::hasException() const | - | ||||||
| 202 | { | - | ||||||
| 203 | return (exceptionHolder.exception() != 0); executed 233394 times by 12 tests: return (exceptionHolder.exception() != 0);Executed by:
| 233394 | ||||||
| 204 | } | - | ||||||
| 205 | - | |||||||
| 206 | ExceptionHolder ExceptionStore::exception() | - | ||||||
| 207 | { | - | ||||||
| 208 | return exceptionHolder; never executed: return exceptionHolder; | 0 | ||||||
| 209 | } | - | ||||||
| 210 | - | |||||||
| 211 | void ExceptionStore::throwPossibleException() | - | ||||||
| 212 | { | - | ||||||
| 213 | if (hasException() ) {
| 21-233348 | ||||||
| 214 | exceptionHolder.base->hasThrown = true; | - | ||||||
| 215 | exceptionHolder.exception()->raise(); | - | ||||||
| 216 | } never executed: end of block | 0 | ||||||
| 217 | } executed 233348 times by 12 tests: end of blockExecuted by:
| 233348 | ||||||
| 218 | - | |||||||
| 219 | bool ExceptionStore::hasThrown() const { return exceptionHolder.base->hasThrown; } never executed: return exceptionHolder.base->hasThrown; | 0 | ||||||
| 220 | - | |||||||
| 221 | } // namespace QtPrivate | - | ||||||
| 222 | - | |||||||
| 223 | #endif //Q_QDOC | - | ||||||
| 224 | - | |||||||
| 225 | QT_END_NAMESPACE | - | ||||||
| 226 | - | |||||||
| 227 | #endif // QT_NO_EXCEPTIONS | - | ||||||
| 228 | #endif // QT_NO_QFUTURE | - | ||||||
| Source code | Switch to Preprocessed file |