src/corelib/concurrent/qfuturewatcher.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtCore module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QFUTUREWATCHER_H
       
    43 #define QFUTUREWATCHER_H
       
    44 
       
    45 #include <QtCore/qfuture.h>
       
    46 
       
    47 #ifndef QT_NO_QFUTURE
       
    48 
       
    49 #include <QtCore/qobject.h>
       
    50 
       
    51 QT_BEGIN_HEADER
       
    52 QT_BEGIN_NAMESPACE
       
    53 
       
    54 QT_MODULE(Core)
       
    55 
       
    56 class QEvent;
       
    57 
       
    58 class QFutureWatcherBasePrivate;
       
    59 class Q_CORE_EXPORT QFutureWatcherBase : public QObject
       
    60 {
       
    61     Q_OBJECT
       
    62     Q_DECLARE_PRIVATE(QFutureWatcherBase)
       
    63 
       
    64 public:
       
    65     QFutureWatcherBase(QObject *parent = 0);
       
    66 
       
    67     int progressValue() const;
       
    68     int progressMinimum() const;
       
    69     int progressMaximum() const;
       
    70     QString progressText() const;
       
    71 
       
    72     bool isStarted() const;
       
    73     bool isFinished() const;
       
    74     bool isRunning() const;
       
    75     bool isCanceled() const;
       
    76     bool isPaused() const;
       
    77 
       
    78     void waitForFinished();
       
    79 
       
    80     void setPendingResultsLimit(int limit);
       
    81 
       
    82     bool event(QEvent *event);
       
    83 
       
    84 Q_SIGNALS:
       
    85     void started();
       
    86     void finished();
       
    87     void canceled();
       
    88     void paused();
       
    89     void resumed();
       
    90     void resultReadyAt(int resultIndex);
       
    91     void resultsReadyAt(int beginIndex, int endIndex);
       
    92     void progressRangeChanged(int minimum, int maximum);
       
    93     void progressValueChanged(int progressValue);
       
    94     void progressTextChanged(const QString &progressText);
       
    95 
       
    96 public Q_SLOTS:
       
    97     void cancel();
       
    98     void setPaused(bool paused);
       
    99     void pause();
       
   100     void resume();
       
   101     void togglePaused();
       
   102 
       
   103 protected:
       
   104     void connectNotify (const char * signal);
       
   105     void disconnectNotify (const char * signal);
       
   106 
       
   107     // called from setFuture() implemented in template sub-classes
       
   108     void connectOutputInterface();
       
   109     void disconnectOutputInterface(bool pendingAssignment = false);
       
   110 
       
   111 private:
       
   112     // implemented in the template sub-classes
       
   113     virtual const QFutureInterfaceBase &futureInterface() const = 0;
       
   114     virtual QFutureInterfaceBase &futureInterface() = 0;
       
   115 };
       
   116 
       
   117 template <typename T>
       
   118 class QFutureWatcher : public QFutureWatcherBase
       
   119 {
       
   120 public:
       
   121     QFutureWatcher(QObject *_parent = 0)
       
   122         : QFutureWatcherBase(_parent)
       
   123     { }
       
   124     ~QFutureWatcher()
       
   125     { disconnectOutputInterface(); }
       
   126 
       
   127     void setFuture(const QFuture<T> &future);
       
   128     QFuture<T> future() const
       
   129     { return m_future; }
       
   130 
       
   131     T result() const { return m_future.result(); }
       
   132     T resultAt(int index) const { return m_future.resultAt(index); }
       
   133 
       
   134 #ifdef qdoc
       
   135     int progressValue() const;
       
   136     int progressMinimum() const;
       
   137     int progressMaximum() const;
       
   138     QString progressText() const;
       
   139 
       
   140     bool isStarted() const;
       
   141     bool isFinished() const;
       
   142     bool isRunning() const;
       
   143     bool isCanceled() const;
       
   144     bool isPaused() const;
       
   145 
       
   146     void waitForFinished();
       
   147 
       
   148     void setPendingResultsLimit(int limit);
       
   149 
       
   150 Q_SIGNALS:
       
   151     void started();
       
   152     void finished();
       
   153     void canceled();
       
   154     void paused();
       
   155     void resumed();
       
   156     void resultReadyAt(int resultIndex);
       
   157     void resultsReadyAt(int beginIndex, int endIndex);
       
   158     void progressRangeChanged(int minimum, int maximum);
       
   159     void progressValueChanged(int progressValue);
       
   160     void progressTextChanged(const QString &progressText);
       
   161 
       
   162 public Q_SLOTS:
       
   163     void cancel();
       
   164     void setPaused(bool paused);
       
   165     void pause();
       
   166     void resume();
       
   167     void togglePaused();
       
   168 #endif
       
   169 
       
   170 private:
       
   171     QFuture<T> m_future;
       
   172     const QFutureInterfaceBase &futureInterface() const { return m_future.d; }
       
   173     QFutureInterfaceBase &futureInterface() { return m_future.d; }
       
   174 };
       
   175 
       
   176 template <typename T>
       
   177 Q_INLINE_TEMPLATE void QFutureWatcher<T>::setFuture(const QFuture<T> &_future)
       
   178 {
       
   179     if (_future == m_future)
       
   180         return;
       
   181 
       
   182     disconnectOutputInterface(true);
       
   183     m_future = _future;
       
   184     connectOutputInterface();
       
   185 }
       
   186 
       
   187 template <>
       
   188 class QFutureWatcher<void> : public QFutureWatcherBase
       
   189 {
       
   190 public:
       
   191     QFutureWatcher(QObject *_parent = 0)
       
   192         : QFutureWatcherBase(_parent)
       
   193     { }
       
   194     ~QFutureWatcher()
       
   195     { disconnectOutputInterface(); }
       
   196 
       
   197     void setFuture(const QFuture<void> &future);
       
   198     QFuture<void> future() const
       
   199     { return m_future; }
       
   200 
       
   201 private:
       
   202     QFuture<void> m_future;
       
   203     const QFutureInterfaceBase &futureInterface() const { return m_future.d; }
       
   204     QFutureInterfaceBase &futureInterface() { return m_future.d; }
       
   205 };
       
   206 
       
   207 Q_INLINE_TEMPLATE void QFutureWatcher<void>::setFuture(const QFuture<void> &_future)
       
   208 {
       
   209     if (_future == m_future)
       
   210         return;
       
   211 
       
   212     disconnectOutputInterface(true);
       
   213     m_future = _future;
       
   214     connectOutputInterface();
       
   215 }
       
   216 
       
   217 QT_END_NAMESPACE
       
   218 QT_END_HEADER
       
   219 
       
   220 #endif // QT_NO_CONCURRENT
       
   221 
       
   222 #endif // QFUTUREWATCHER_H