/*============================================================================= Library: CTK Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =============================================================================*/ #ifndef CTKCMDLINEMODULEFUTUREINTERFACE_H #define CTKCMDLINEMODULEFUTUREINTERFACE_H #include #include "ctkCmdLineModuleResult.h" #include #if (QT_VERSION < 0x50000) #include #else #include #include #endif class ctkCmdLineModuleFuture; class ctkCmdLineModuleFutureInterfacePrivate; /** * \ingroup CommandLineModulesCore_API * * \brief A QFutureInterface specialization. * * This QFutureInterface must be used by custom backend implementations to retrieve * a suitable QFuture object and to report state changes to it via this interface. */ template <> class CTK_CMDLINEMODULECORE_EXPORT QFutureInterface : public QFutureInterfaceBase { public: QFutureInterface(State initialState = NoState); QFutureInterface(const QFutureInterface &other); ~QFutureInterface(); static QFutureInterface canceledResult(); QFutureInterface& operator=(const QFutureInterface& other); inline ctkCmdLineModuleFuture future(); // implemented in ctkCmdLineModuleFuture.h bool canCancel() const; void setCanCancel(bool canCancel); bool canPause() const; void setCanPause(bool canPause); inline void reportResult(const ctkCmdLineModuleResult *result, int index = -1); inline void reportResult(const ctkCmdLineModuleResult &result, int index = -1); inline void reportResults(const QVector &results, int beginIndex = -1, int count = -1); inline void reportFinished(const ctkCmdLineModuleResult *result = 0); void reportOutputData(const QByteArray& outputData); void reportErrorData(const QByteArray& errorData); inline const ctkCmdLineModuleResult &resultReference(int index) const; inline const ctkCmdLineModuleResult *resultPointer(int index) const; inline QList results(); QByteArray outputData(int position = 0, int size = -1) const; QByteArray errorData(int position = 0, int size = -1) const; private: friend struct ctkCmdLineModuleFutureWatcherPrivate; #if (QT_VERSION < 0x50000) QtConcurrent::ResultStore &resultStore() { return static_cast &>(resultStoreBase()); } const QtConcurrent::ResultStore &resultStore() const { return static_cast &>(resultStoreBase()); } #else QtPrivate::ResultStore &resultStore() { return static_cast &>(resultStoreBase()); } const QtPrivate::ResultStore &resultStore() const { return static_cast &>(resultStoreBase()); } #endif ctkCmdLineModuleFutureInterfacePrivate* d; }; inline void QFutureInterface::reportResult(const ctkCmdLineModuleResult *result, int index) { QMutexLocker locker(mutex()); if (this->queryState(Canceled) || this->queryState(Finished)) { return; } #if (QT_VERSION < 0x50000) QtConcurrent::ResultStore &store = resultStore(); #else QtPrivate::ResultStore &store = resultStore(); #endif if (store.filterMode()) { const int resultCountBefore = store.count(); store.addResult(index, result); this->reportResultsReady(resultCountBefore, resultCountBefore + store.count()); } else { const int insertIndex = store.addResult(index, result); this->reportResultsReady(insertIndex, insertIndex + 1); } } inline void QFutureInterface::reportResult(const ctkCmdLineModuleResult &result, int index) { reportResult(&result, index); } inline void QFutureInterface::reportResults(const QVector &_results, int beginIndex, int count) { QMutexLocker locker(mutex()); if (this->queryState(Canceled) || this->queryState(Finished)) { return; } #if (QT_VERSION < 0x50000) QtConcurrent::ResultStore &store = resultStore(); #else QtPrivate::ResultStore &store = resultStore(); #endif if (store.filterMode()) { const int resultCountBefore = store.count(); store.addResults(beginIndex, &_results, count); this->reportResultsReady(resultCountBefore, store.count()); } else { const int insertIndex = store.addResults(beginIndex, &_results, count); this->reportResultsReady(insertIndex, insertIndex + _results.count()); } } inline void QFutureInterface::reportFinished(const ctkCmdLineModuleResult *result) { if (result) reportResult(result); QFutureInterfaceBase::reportFinished(); } inline const ctkCmdLineModuleResult &QFutureInterface::resultReference(int index) const { QMutexLocker lock(mutex()); return resultStore().resultAt(index).value(); } inline const ctkCmdLineModuleResult *QFutureInterface::resultPointer(int index) const { QMutexLocker lock(mutex()); return resultStore().resultAt(index).pointer(); } inline QList QFutureInterface::results() { if (this->isCanceled()) { exceptionStore().throwPossibleException(); return QList(); } QFutureInterfaceBase::waitForResult(-1); QList res; QMutexLocker lock(mutex()); #if (QT_VERSION <= 0x50000) QtConcurrent::ResultIterator it = resultStore().begin(); #else QtPrivate::ResultIterator it = resultStore().begin(); #endif while (it != resultStore().end()) { res.append(it.value()); ++it; } return res; } typedef QFutureInterface ctkCmdLineModuleFutureInterface; #endif // CTKCMDLINEMODULEFUTUREINTERFACE_H