CTK  0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
ctkCmdLineModuleFutureInterface.h
Go to the documentation of this file.
1 /*=============================================================================
2 
3  Library: CTK
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 =============================================================================*/
21 
22 #ifndef CTKCMDLINEMODULEFUTUREINTERFACE_H
23 #define CTKCMDLINEMODULEFUTUREINTERFACE_H
24 
25 #include <ctkCommandLineModulesCoreExport.h>
26 
27 #include "ctkCmdLineModuleResult.h"
28 
29 #include <QFutureInterface>
30 #if (QT_VERSION < 0x50000)
31 #include <QtCore>
32 #else
33 #include <QtConcurrent>
34 #include <qresultstore.h>
35 #endif
36 
37 
39 class ctkCmdLineModuleFutureInterfacePrivate;
40 
49 template <>
50 class CTK_CMDLINEMODULECORE_EXPORT QFutureInterface<ctkCmdLineModuleResult> : public QFutureInterfaceBase
51 {
52 
53 public:
54 
55  QFutureInterface(State initialState = NoState);
56 
57  QFutureInterface(const QFutureInterface &other);
58 
59  ~QFutureInterface();
60 
61  static QFutureInterface canceledResult();
62 
63  QFutureInterface& operator=(const QFutureInterface& other);
64 
65  inline ctkCmdLineModuleFuture future(); // implemented in ctkCmdLineModuleFuture.h
66 
67  bool canCancel() const;
68  void setCanCancel(bool canCancel);
69  bool canPause() const;
70  void setCanPause(bool canPause);
71 
72  inline void reportResult(const ctkCmdLineModuleResult *result, int index = -1);
73  inline void reportResult(const ctkCmdLineModuleResult &result, int index = -1);
74  inline void reportResults(const QVector<ctkCmdLineModuleResult> &results, int beginIndex = -1, int count = -1);
75  inline void reportFinished(const ctkCmdLineModuleResult *result = 0);
76 
77  void reportOutputData(const QByteArray& outputData);
78  void reportErrorData(const QByteArray& errorData);
79 
80  inline const ctkCmdLineModuleResult &resultReference(int index) const;
81  inline const ctkCmdLineModuleResult *resultPointer(int index) const;
82  inline QList<ctkCmdLineModuleResult> results();
83 
84  QByteArray outputData(int position = 0, int size = -1) const;
85  QByteArray errorData(int position = 0, int size = -1) const;
86 
87 private:
88 
89  friend struct ctkCmdLineModuleFutureWatcherPrivate;
90 
91 #if (QT_VERSION < 0x50000)
92  QtConcurrent::ResultStore<ctkCmdLineModuleResult> &resultStore()
93  { return static_cast<QtConcurrent::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
94  const QtConcurrent::ResultStore<ctkCmdLineModuleResult> &resultStore() const
95  { return static_cast<const QtConcurrent::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
96 #else
97  QtPrivate::ResultStore<ctkCmdLineModuleResult> &resultStore()
98  { return static_cast<QtPrivate::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
99  const QtPrivate::ResultStore<ctkCmdLineModuleResult> &resultStore() const
100  { return static_cast<const QtPrivate::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
101 #endif
102 
103  ctkCmdLineModuleFutureInterfacePrivate* d;
104 };
105 
107 {
108  QMutexLocker locker(mutex());
109  if (this->queryState(Canceled) || this->queryState(Finished)) {
110  return;
111  }
112 
113 #if (QT_VERSION < 0x50000)
114  QtConcurrent::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
115 #else
116  QtPrivate::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
117 #endif
118 
119  if (store.filterMode()) {
120  const int resultCountBefore = store.count();
121  store.addResult(index, result);
122  this->reportResultsReady(resultCountBefore, resultCountBefore + store.count());
123  } else {
124  const int insertIndex = store.addResult(index, result);
125  this->reportResultsReady(insertIndex, insertIndex + 1);
126  }
127 }
128 
130 {
131  reportResult(&result, index);
132 }
133 
134 inline void QFutureInterface<ctkCmdLineModuleResult>::reportResults(const QVector<ctkCmdLineModuleResult> &_results, int beginIndex, int count)
135 {
136  QMutexLocker locker(mutex());
137  if (this->queryState(Canceled) || this->queryState(Finished)) {
138  return;
139  }
140 
141 #if (QT_VERSION < 0x50000)
142  QtConcurrent::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
143 #else
144  QtPrivate::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
145 #endif
146 
147  if (store.filterMode()) {
148  const int resultCountBefore = store.count();
149  store.addResults(beginIndex, &_results, count);
150  this->reportResultsReady(resultCountBefore, store.count());
151  } else {
152  const int insertIndex = store.addResults(beginIndex, &_results, count);
153  this->reportResultsReady(insertIndex, insertIndex + _results.count());
154  }
155 }
156 
158 {
159  if (result)
160  reportResult(result);
161  QFutureInterfaceBase::reportFinished();
162 }
163 
165 {
166  QMutexLocker lock(mutex());
167  return resultStore().resultAt(index).value();
168 }
169 
171 {
172  QMutexLocker lock(mutex());
173  return resultStore().resultAt(index).pointer();
174 }
175 
177 {
178  if (this->isCanceled()) {
179  exceptionStore().throwPossibleException();
181  }
182  QFutureInterfaceBase::waitForResult(-1);
183 
185  QMutexLocker lock(mutex());
186 
187 #if (QT_VERSION <= 0x50000)
188  QtConcurrent::ResultIterator<ctkCmdLineModuleResult> it = resultStore().begin();
189 #else
190  QtPrivate::ResultIterator<ctkCmdLineModuleResult> it = resultStore().begin();
191 #endif
192  while (it != resultStore().end()) {
193  res.append(it.value());
194  ++it;
195  }
196 
197  return res;
198 }
199 
201 
202 #endif // CTKCMDLINEMODULEFUTUREINTERFACE_H
Describes a reported result of a command line module.
void reportResult(const ctkCmdLineModuleResult *result, int index=-1)
const ctkCmdLineModuleResult * resultPointer(int index) const
void reportResults(const QVector< ctkCmdLineModuleResult > &results, int beginIndex=-1, int count=-1)
QFuture sub-class for enhanced communication with running modules.Please see the QFuture documentatio...
QFutureInterface< ctkCmdLineModuleResult > ctkCmdLineModuleFutureInterface
const ctkCmdLineModuleResult & resultReference(int index) const
void reportFinished(const ctkCmdLineModuleResult *result=0)