qtconcurrentrunbase.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QTCONCURRENT_RUNBASE_H
00043 #define QTCONCURRENT_RUNBASE_H
00044 
00045 #include <QtCore/qglobal.h>
00046 
00047 #ifndef QT_NO_CONCURRENT
00048 
00049 #include <QtCore/qfuture.h>
00050 #include <QtCore/qrunnable.h>
00051 #include <QtCore/qthreadpool.h>
00052 
00053 QT_BEGIN_HEADER
00054 QT_BEGIN_NAMESPACE
00055 
00056 QT_MODULE(Core)
00057 
00058 #ifndef qdoc
00059 
00060 namespace QtConcurrent {
00061 
00062 template <typename T>
00063 struct SelectSpecialization
00064 {
00065     template <class Normal, class Void>
00066     struct Type { typedef Normal type; };
00067 };
00068 
00069 template <>
00070 struct SelectSpecialization<void>
00071 {
00072     template <class Normal, class Void>
00073     struct Type { typedef Void type; };
00074 };
00075 
00076 template <typename T>
00077 class RunFunctionTaskBase : public QFutureInterface<T> , public QRunnable
00078 {
00079 public:
00080     QFuture<T> start()
00081     {
00082         this->setRunnable(this);
00083         this->reportStarted();
00084         QFuture<T> future = this->future();
00085         QThreadPool::globalInstance()->start(this, /*m_priority*/ 0);
00086         return future;
00087     }
00088 
00089     void run() {}
00090     virtual void runFunctor() = 0;
00091 };
00092 
00093 template <typename T>
00094 class RunFunctionTask : public RunFunctionTaskBase<T>
00095 {
00096 public:
00097     void run()
00098     {
00099         if (this->isCanceled()) {
00100             this->reportFinished();
00101             return;
00102         }
00103         this->runFunctor();
00104         this->reportResult(result);
00105         this->reportFinished();
00106     }
00107     T result;
00108 };
00109 
00110 template <>
00111 class RunFunctionTask<void> : public RunFunctionTaskBase<void>
00112 {
00113 public:
00114     void run()
00115     {
00116         if (this->isCanceled()) {
00117             this->reportFinished();
00118             return;
00119         }
00120         this->runFunctor();
00121         this->reportFinished();
00122     }
00123 };
00124 
00125 } //namespace QtConcurrent
00126 
00127 #endif //qdoc
00128 
00129 QT_END_NAMESPACE
00130 QT_END_HEADER
00131 
00132 #endif // QT_NO_CONCURRENT
00133 
00134 #endif