ctkCmdLineModuleFutureInterface.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. class ctkCmdLineModuleFuture;
  21. class ctkCmdLineModuleFutureInterfacePrivate;
  22. /**
  23. * \ingroup CommandLineModulesCore
  24. *
  25. * \brief A QFutureInterface specialization.
  26. *
  27. * This QFutureInterface must be used by custom backend implementations to retrieve
  28. * a suitable QFuture object and to report state changes to it via this interface.
  29. */
  30. template <>
  31. class CTK_CMDLINEMODULECORE_EXPORT QFutureInterface<ctkCmdLineModuleResult> : public QFutureInterfaceBase
  32. {
  33. public:
  34. QFutureInterface(State initialState = NoState);
  35. QFutureInterface(const QFutureInterface &other);
  36. ~QFutureInterface();
  37. static QFutureInterface canceledResult();
  38. QFutureInterface& operator=(const QFutureInterface& other);
  39. inline ctkCmdLineModuleFuture future(); // implemented in ctkCmdLineModuleFuture.h
  40. bool canCancel() const;
  41. void setCanCancel(bool canCancel);
  42. bool canPause() const;
  43. void setCanPause(bool canPause);
  44. inline void reportResult(const ctkCmdLineModuleResult *result, int index = -1);
  45. inline void reportResult(const ctkCmdLineModuleResult &result, int index = -1);
  46. inline void reportResults(const QVector<ctkCmdLineModuleResult> &results, int beginIndex = -1, int count = -1);
  47. inline void reportFinished(const ctkCmdLineModuleResult *result = 0);
  48. void reportOutputData(const QByteArray& outputData);
  49. void reportErrorData(const QByteArray& errorData);
  50. inline const ctkCmdLineModuleResult &resultReference(int index) const;
  51. inline const ctkCmdLineModuleResult *resultPointer(int index) const;
  52. inline QList<ctkCmdLineModuleResult> results();
  53. QByteArray outputData(int position = 0, int size = -1) const;
  54. QByteArray errorData(int position = 0, int size = -1) const;
  55. private:
  56. friend struct ctkCmdLineModuleFutureWatcherPrivate;
  57. QtConcurrent::ResultStore<ctkCmdLineModuleResult> &resultStore()
  58. { return static_cast<QtConcurrent::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
  59. const QtConcurrent::ResultStore<ctkCmdLineModuleResult> &resultStore() const
  60. { return static_cast<const QtConcurrent::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
  61. ctkCmdLineModuleFutureInterfacePrivate* d;
  62. };
  63. inline void QFutureInterface<ctkCmdLineModuleResult>::reportResult(const ctkCmdLineModuleResult *result, int index)
  64. {
  65. QMutexLocker locker(mutex());
  66. if (this->queryState(Canceled) || this->queryState(Finished)) {
  67. return;
  68. }
  69. QtConcurrent::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
  70. if (store.filterMode()) {
  71. const int resultCountBefore = store.count();
  72. store.addResult(index, result);
  73. this->reportResultsReady(resultCountBefore, resultCountBefore + store.count());
  74. } else {
  75. const int insertIndex = store.addResult(index, result);
  76. this->reportResultsReady(insertIndex, insertIndex + 1);
  77. }
  78. }
  79. inline void QFutureInterface<ctkCmdLineModuleResult>::reportResult(const ctkCmdLineModuleResult &result, int index)
  80. {
  81. reportResult(&result, index);
  82. }
  83. inline void QFutureInterface<ctkCmdLineModuleResult>::reportResults(const QVector<ctkCmdLineModuleResult> &_results, int beginIndex, int count)
  84. {
  85. QMutexLocker locker(mutex());
  86. if (this->queryState(Canceled) || this->queryState(Finished)) {
  87. return;
  88. }
  89. QtConcurrent::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
  90. if (store.filterMode()) {
  91. const int resultCountBefore = store.count();
  92. store.addResults(beginIndex, &_results, count);
  93. this->reportResultsReady(resultCountBefore, store.count());
  94. } else {
  95. const int insertIndex = store.addResults(beginIndex, &_results, count);
  96. this->reportResultsReady(insertIndex, insertIndex + _results.count());
  97. }
  98. }
  99. inline void QFutureInterface<ctkCmdLineModuleResult>::reportFinished(const ctkCmdLineModuleResult *result)
  100. {
  101. if (result)
  102. reportResult(result);
  103. QFutureInterfaceBase::reportFinished();
  104. }
  105. inline const ctkCmdLineModuleResult &QFutureInterface<ctkCmdLineModuleResult>::resultReference(int index) const
  106. {
  107. QMutexLocker lock(mutex());
  108. return resultStore().resultAt(index).value();
  109. }
  110. inline const ctkCmdLineModuleResult *QFutureInterface<ctkCmdLineModuleResult>::resultPointer(int index) const
  111. {
  112. QMutexLocker lock(mutex());
  113. return resultStore().resultAt(index).pointer();
  114. }
  115. inline QList<ctkCmdLineModuleResult> QFutureInterface<ctkCmdLineModuleResult>::results()
  116. {
  117. if (this->isCanceled()) {
  118. exceptionStore().throwPossibleException();
  119. return QList<ctkCmdLineModuleResult>();
  120. }
  121. QFutureInterfaceBase::waitForResult(-1);
  122. QList<ctkCmdLineModuleResult> res;
  123. QMutexLocker lock(mutex());
  124. QtConcurrent::ResultIterator<ctkCmdLineModuleResult> it = resultStore().begin();
  125. while (it != resultStore().end()) {
  126. res.append(it.value());
  127. ++it;
  128. }
  129. return res;
  130. }
  131. typedef QFutureInterface<ctkCmdLineModuleResult> ctkCmdLineModuleFutureInterface;
  132. #endif // CTKCMDLINEMODULEFUTUREINTERFACE_H