Qt 4.8
qtconcurrentexception.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qtconcurrentexception.h"
43 
44 #ifndef QT_NO_QFUTURE
45 #ifndef QT_NO_EXCEPTIONS
46 
48 
125 namespace QtConcurrent
126 {
127 
128 void Exception::raise() const
129 {
130  Exception e = *this;
131  throw e;
132 }
133 
135 {
136  return new Exception(*this);
137 }
138 
140 {
141  UnhandledException e = *this;
142  throw e;
143 }
144 
146 {
147  return new UnhandledException(*this);
148 }
149 
150 #ifndef qdoc
151 
152 namespace internal {
153 
154 class Base
155 {
156 public:
157  Base(Exception *exception)
158  : exception(exception), refCount(1), hasThrown(false) { }
159  ~Base() { delete exception; }
160 
161  Exception *exception;
162  QAtomicInt refCount;
163  bool hasThrown;
164 };
165 
166 ExceptionHolder::ExceptionHolder(Exception *exception)
167 : base(new Base(exception)) {}
168 
169 ExceptionHolder::ExceptionHolder(const ExceptionHolder &other)
170 : base(other.base)
171 {
172  base->refCount.ref();
173 }
174 
175 void ExceptionHolder::operator=(const ExceptionHolder &other)
176 {
177  if (base == other.base)
178  return;
179 
180  if (base->refCount.deref() == false)
181  delete base;
182 
183  base = other.base;
184  base->refCount.ref();
185 }
186 
187 ExceptionHolder::~ExceptionHolder()
188 {
189  if (base->refCount.deref() == 0)
190  delete base;
191 }
192 
193 Exception *ExceptionHolder::exception() const
194 {
195  return base->exception;
196 }
197 
198 void ExceptionStore::setException(const Exception &e)
199 {
200  if (hasException() == false)
201  exceptionHolder = ExceptionHolder(e.clone());
202 }
203 
204 bool ExceptionStore::hasException() const
205 {
206  return (exceptionHolder.exception() != 0);
207 }
208 
209 ExceptionHolder ExceptionStore::exception()
210 {
211  return exceptionHolder;
212 }
213 
214 void ExceptionStore::throwPossibleException()
215 {
216  if (hasException() ) {
217  exceptionHolder.base->hasThrown = true;
218  exceptionHolder.exception()->raise();
219  }
220 }
221 
222 bool ExceptionStore::hasThrown() const { return exceptionHolder.base->hasThrown; }
223 
224 } // namespace internal
225 
226 #endif //qdoc
227 
228 } // namespace QtConcurrent
229 
231 
232 #endif // QT_NO_EXCEPTIONS
233 #endif // QT_NO_CONCURRENT
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
The UnhandledException class represents an unhandled exception in a worker thread.
static const uint base
Definition: qurl.cpp:268
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The Exception class provides a base class for exceptions that can transferred across threads...
The QtConcurrent namespace provides high-level APIs that make it possible to write multi-threaded pro...
virtual void raise() const
In your QtConcurrent::Exception subclass, reimplement raise() like this:
virtual Exception * clone() const
In your QtConcurrent::Exception subclass, reimplement clone() like this: