ctkCmdLineModuleFutureInterface.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef CTKCMDLINEMODULEFUTUREINTERFACE_H
  16. #define CTKCMDLINEMODULEFUTUREINTERFACE_H
  17. #include <ctkCommandLineModulesCoreExport.h>
  18. #include "ctkCmdLineModuleResult.h"
  19. #include <QFutureInterface>
  20. #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
  21. #include <QtCore>
  22. #else
  23. #include <QtConcurrent>
  24. #include <qresultstore.h>
  25. #endif
  26. class ctkCmdLineModuleFuture;
  27. class ctkCmdLineModuleFutureInterfacePrivate;
  28. /**
  29. * \ingroup CommandLineModulesCore_API
  30. *
  31. * \brief A QFutureInterface specialization.
  32. *
  33. * This QFutureInterface must be used by custom backend implementations to retrieve
  34. * a suitable QFuture object and to report state changes to it via this interface.
  35. */
  36. template <>
  37. class CTK_CMDLINEMODULECORE_EXPORT QFutureInterface<ctkCmdLineModuleResult> : public QFutureInterfaceBase
  38. {
  39. public:
  40. QFutureInterface(State initialState = NoState);
  41. QFutureInterface(const QFutureInterface &other);
  42. ~QFutureInterface();
  43. static QFutureInterface canceledResult();
  44. QFutureInterface& operator=(const QFutureInterface& other);
  45. inline ctkCmdLineModuleFuture future(); // implemented in ctkCmdLineModuleFuture.h
  46. bool canCancel() const;
  47. void setCanCancel(bool canCancel);
  48. bool canPause() const;
  49. void setCanPause(bool canPause);
  50. inline void reportResult(const ctkCmdLineModuleResult *result, int index = -1);
  51. inline void reportResult(const ctkCmdLineModuleResult &result, int index = -1);
  52. inline void reportResults(const QVector<ctkCmdLineModuleResult> &results, int beginIndex = -1, int count = -1);
  53. inline void reportFinished(const ctkCmdLineModuleResult *result = 0);
  54. void reportOutputData(const QByteArray& outputData);
  55. void reportErrorData(const QByteArray& errorData);
  56. inline const ctkCmdLineModuleResult &resultReference(int index) const;
  57. inline const ctkCmdLineModuleResult *resultPointer(int index) const;
  58. inline QList<ctkCmdLineModuleResult> results();
  59. QByteArray outputData(int position = 0, int size = -1) const;
  60. QByteArray errorData(int position = 0, int size = -1) const;
  61. private:
  62. friend struct ctkCmdLineModuleFutureWatcherPrivate;
  63. #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
  64. QtConcurrent::ResultStore<ctkCmdLineModuleResult> &resultStore()
  65. { return static_cast<QtConcurrent::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
  66. const QtConcurrent::ResultStore<ctkCmdLineModuleResult> &resultStore() const
  67. { return static_cast<const QtConcurrent::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
  68. #elif (QT_VERSION < QT_VERSION_CHECK(5, 9, 0))
  69. QtPrivate::ResultStore<ctkCmdLineModuleResult> &resultStore()
  70. { return static_cast<QtPrivate::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
  71. const QtPrivate::ResultStore<ctkCmdLineModuleResult> &resultStore() const
  72. { return static_cast<const QtPrivate::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
  73. #else
  74. QtPrivate::ResultStoreBase &resultStore()
  75. { return static_cast<QtPrivate::ResultStoreBase &>(resultStoreBase()); }
  76. const QtPrivate::ResultStoreBase &resultStore() const
  77. { return static_cast<const QtPrivate::ResultStoreBase &>(resultStoreBase()); }
  78. #endif
  79. ctkCmdLineModuleFutureInterfacePrivate* d;
  80. };
  81. inline void QFutureInterface<ctkCmdLineModuleResult>::reportResult(const ctkCmdLineModuleResult *result, int index)
  82. {
  83. QMutexLocker locker(mutex());
  84. if (this->queryState(Canceled) || this->queryState(Finished)) {
  85. return;
  86. }
  87. #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
  88. QtConcurrent::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
  89. #elif (QT_VERSION < QT_VERSION_CHECK(5, 9, 0))
  90. QtPrivate::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
  91. #else
  92. QtPrivate::ResultStoreBase &store = resultStore();
  93. #endif
  94. if (store.filterMode()) {
  95. const int resultCountBefore = store.count();
  96. store.addResult(index, result);
  97. this->reportResultsReady(resultCountBefore, resultCountBefore + store.count());
  98. } else {
  99. const int insertIndex = store.addResult(index, result);
  100. this->reportResultsReady(insertIndex, insertIndex + 1);
  101. }
  102. }
  103. inline void QFutureInterface<ctkCmdLineModuleResult>::reportResult(const ctkCmdLineModuleResult &result, int index)
  104. {
  105. reportResult(&result, index);
  106. }
  107. inline void QFutureInterface<ctkCmdLineModuleResult>::reportResults(const QVector<ctkCmdLineModuleResult> &_results, int beginIndex, int count)
  108. {
  109. QMutexLocker locker(mutex());
  110. if (this->queryState(Canceled) || this->queryState(Finished)) {
  111. return;
  112. }
  113. #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
  114. QtConcurrent::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
  115. #elif (QT_VERSION < QT_VERSION_CHECK(5, 9, 0))
  116. QtPrivate::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
  117. #else
  118. QtPrivate::ResultStoreBase &store = resultStore();
  119. #endif
  120. if (store.filterMode()) {
  121. const int resultCountBefore = store.count();
  122. store.addResults(beginIndex, &_results, count);
  123. this->reportResultsReady(resultCountBefore, store.count());
  124. } else {
  125. const int insertIndex = store.addResults(beginIndex, &_results, count);
  126. this->reportResultsReady(insertIndex, insertIndex + _results.count());
  127. }
  128. }
  129. inline void QFutureInterface<ctkCmdLineModuleResult>::reportFinished(const ctkCmdLineModuleResult *result)
  130. {
  131. if (result)
  132. reportResult(result);
  133. QFutureInterfaceBase::reportFinished();
  134. }
  135. inline const ctkCmdLineModuleResult &QFutureInterface<ctkCmdLineModuleResult>::resultReference(int index) const
  136. {
  137. QMutexLocker lock(mutex());
  138. #if (QT_VERSION < QT_VERSION_CHECK(5, 9, 0))
  139. return resultStore().resultAt(index).value();
  140. #else
  141. return resultStore().resultAt(index).value<ctkCmdLineModuleResult>();
  142. #endif
  143. }
  144. inline const ctkCmdLineModuleResult *QFutureInterface<ctkCmdLineModuleResult>::resultPointer(int index) const
  145. {
  146. QMutexLocker lock(mutex());
  147. #if (QT_VERSION < QT_VERSION_CHECK(5, 9, 0))
  148. return resultStore().resultAt(index).pointer();
  149. #else
  150. return resultStore().resultAt(index).pointer<ctkCmdLineModuleResult>();
  151. #endif
  152. }
  153. inline QList<ctkCmdLineModuleResult> QFutureInterface<ctkCmdLineModuleResult>::results()
  154. {
  155. if (this->isCanceled()) {
  156. exceptionStore().throwPossibleException();
  157. return QList<ctkCmdLineModuleResult>();
  158. }
  159. QFutureInterfaceBase::waitForResult(-1);
  160. QList<ctkCmdLineModuleResult> res;
  161. QMutexLocker lock(mutex());
  162. #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
  163. QtConcurrent::ResultIterator<ctkCmdLineModuleResult> it = resultStore().begin();
  164. #elif (QT_VERSION < QT_VERSION_CHECK(5, 9, 0))
  165. QtPrivate::ResultIterator<ctkCmdLineModuleResult> it = resultStore().begin();
  166. #else
  167. QtPrivate::ResultIteratorBase it = resultStore().begin();
  168. #endif
  169. while (it != resultStore().end()) {
  170. #if (QT_VERSION < QT_VERSION_CHECK(5, 9, 0))
  171. res.append(it.value());
  172. #else
  173. res.append(it.value<ctkCmdLineModuleResult>());
  174. #endif
  175. ++it;
  176. }
  177. return res;
  178. }
  179. typedef QFutureInterface<ctkCmdLineModuleResult> ctkCmdLineModuleFutureInterface;
  180. #endif // CTKCMDLINEMODULEFUTUREINTERFACE_H