123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- /*=============================================================================
- 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 <ctkCommandLineModulesCoreExport.h>
- #include "ctkCmdLineModuleResult.h"
- #include <QFutureInterface>
- class ctkCmdLineModuleFuture;
- class ctkCmdLineModuleFutureInterfacePrivate;
- /**
- * \ingroup CommandLineModulesCore
- *
- * \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<ctkCmdLineModuleResult> : 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<ctkCmdLineModuleResult> &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<ctkCmdLineModuleResult> results();
- QByteArray outputData(int position = 0, int size = -1) const;
- QByteArray errorData(int position = 0, int size = -1) const;
- private:
- friend struct ctkCmdLineModuleFutureWatcherPrivate;
- QtConcurrent::ResultStore<ctkCmdLineModuleResult> &resultStore()
- { return static_cast<QtConcurrent::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
- const QtConcurrent::ResultStore<ctkCmdLineModuleResult> &resultStore() const
- { return static_cast<const QtConcurrent::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
- ctkCmdLineModuleFutureInterfacePrivate* d;
- };
- inline void QFutureInterface<ctkCmdLineModuleResult>::reportResult(const ctkCmdLineModuleResult *result, int index)
- {
- QMutexLocker locker(mutex());
- if (this->queryState(Canceled) || this->queryState(Finished)) {
- return;
- }
- QtConcurrent::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
- 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<ctkCmdLineModuleResult>::reportResult(const ctkCmdLineModuleResult &result, int index)
- {
- reportResult(&result, index);
- }
- inline void QFutureInterface<ctkCmdLineModuleResult>::reportResults(const QVector<ctkCmdLineModuleResult> &_results, int beginIndex, int count)
- {
- QMutexLocker locker(mutex());
- if (this->queryState(Canceled) || this->queryState(Finished)) {
- return;
- }
- QtConcurrent::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
- 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<ctkCmdLineModuleResult>::reportFinished(const ctkCmdLineModuleResult *result)
- {
- if (result)
- reportResult(result);
- QFutureInterfaceBase::reportFinished();
- }
- inline const ctkCmdLineModuleResult &QFutureInterface<ctkCmdLineModuleResult>::resultReference(int index) const
- {
- QMutexLocker lock(mutex());
- return resultStore().resultAt(index).value();
- }
- inline const ctkCmdLineModuleResult *QFutureInterface<ctkCmdLineModuleResult>::resultPointer(int index) const
- {
- QMutexLocker lock(mutex());
- return resultStore().resultAt(index).pointer();
- }
- inline QList<ctkCmdLineModuleResult> QFutureInterface<ctkCmdLineModuleResult>::results()
- {
- if (this->isCanceled()) {
- exceptionStore().throwPossibleException();
- return QList<ctkCmdLineModuleResult>();
- }
- QFutureInterfaceBase::waitForResult(-1);
- QList<ctkCmdLineModuleResult> res;
- QMutexLocker lock(mutex());
- QtConcurrent::ResultIterator<ctkCmdLineModuleResult> it = resultStore().begin();
- while (it != resultStore().end()) {
- res.append(it.value());
- ++it;
- }
- return res;
- }
- typedef QFutureInterface<ctkCmdLineModuleResult> ctkCmdLineModuleFutureInterface;
- #endif // CTKCMDLINEMODULEFUTUREINTERFACE_H
|