ctkCmdLineModuleFutureInterface.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "ctkCmdLineModuleResult.h"
  18. #include <QFutureInterface>
  19. class ctkCmdLineModuleFuture;
  20. template <>
  21. class QFutureInterface<ctkCmdLineModuleResult> : public QFutureInterfaceBase
  22. {
  23. public:
  24. QFutureInterface(State initialState = NoState)
  25. : QFutureInterfaceBase(initialState),
  26. CanCancel(false), CanPause(false)
  27. { }
  28. QFutureInterface(const QFutureInterface &other)
  29. : QFutureInterfaceBase(other),
  30. CanCancel(other.CanCancel), CanPause(other.CanPause)
  31. { }
  32. ~QFutureInterface()
  33. {
  34. if (referenceCountIsOne())
  35. resultStore().clear();
  36. }
  37. static QFutureInterface canceledResult()
  38. { return QFutureInterface(State(Started | Finished | Canceled)); }
  39. QFutureInterface& operator=(const QFutureInterface& other)
  40. {
  41. if (referenceCountIsOne())
  42. resultStore().clear();
  43. QFutureInterfaceBase::operator=(other);
  44. CanCancel = other.CanCancel;
  45. CanPause = other.CanPause;
  46. return *this;
  47. }
  48. inline ctkCmdLineModuleFuture future(); // implemented in ctkCmdLineModuleFuture.h
  49. inline bool canCancel() const { return CanCancel; }
  50. inline void setCanCancel(bool canCancel) { CanCancel = canCancel; }
  51. inline bool canPause() const { return CanPause; }
  52. inline void setCanPause(bool canPause) { CanPause = canPause; }
  53. inline void reportResult(const ctkCmdLineModuleResult *result, int index = -1);
  54. inline void reportResult(const ctkCmdLineModuleResult &result, int index = -1);
  55. inline void reportResults(const QVector<ctkCmdLineModuleResult> &results, int beginIndex = -1, int count = -1);
  56. inline void reportFinished(const ctkCmdLineModuleResult *result = 0);
  57. inline const ctkCmdLineModuleResult &resultReference(int index) const;
  58. inline const ctkCmdLineModuleResult *resultPointer(int index) const;
  59. inline QList<ctkCmdLineModuleResult> results();
  60. private:
  61. QtConcurrent::ResultStore<ctkCmdLineModuleResult> &resultStore()
  62. { return static_cast<QtConcurrent::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
  63. const QtConcurrent::ResultStore<ctkCmdLineModuleResult> &resultStore() const
  64. { return static_cast<const QtConcurrent::ResultStore<ctkCmdLineModuleResult> &>(resultStoreBase()); }
  65. bool CanCancel;
  66. bool CanPause;
  67. };
  68. inline void QFutureInterface<ctkCmdLineModuleResult>::reportResult(const ctkCmdLineModuleResult *result, int index)
  69. {
  70. QMutexLocker locker(mutex());
  71. if (this->queryState(Canceled) || this->queryState(Finished)) {
  72. return;
  73. }
  74. QtConcurrent::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
  75. if (store.filterMode()) {
  76. const int resultCountBefore = store.count();
  77. store.addResult(index, result);
  78. this->reportResultsReady(resultCountBefore, resultCountBefore + store.count());
  79. } else {
  80. const int insertIndex = store.addResult(index, result);
  81. this->reportResultsReady(insertIndex, insertIndex + 1);
  82. }
  83. }
  84. inline void QFutureInterface<ctkCmdLineModuleResult>::reportResult(const ctkCmdLineModuleResult &result, int index)
  85. {
  86. reportResult(&result, index);
  87. }
  88. inline void QFutureInterface<ctkCmdLineModuleResult>::reportResults(const QVector<ctkCmdLineModuleResult> &_results, int beginIndex, int count)
  89. {
  90. QMutexLocker locker(mutex());
  91. if (this->queryState(Canceled) || this->queryState(Finished)) {
  92. return;
  93. }
  94. QtConcurrent::ResultStore<ctkCmdLineModuleResult> &store = resultStore();
  95. if (store.filterMode()) {
  96. const int resultCountBefore = store.count();
  97. store.addResults(beginIndex, &_results, count);
  98. this->reportResultsReady(resultCountBefore, store.count());
  99. } else {
  100. const int insertIndex = store.addResults(beginIndex, &_results, count);
  101. this->reportResultsReady(insertIndex, insertIndex + _results.count());
  102. }
  103. }
  104. inline void QFutureInterface<ctkCmdLineModuleResult>::reportFinished(const ctkCmdLineModuleResult *result)
  105. {
  106. if (result)
  107. reportResult(result);
  108. QFutureInterfaceBase::reportFinished();
  109. }
  110. inline const ctkCmdLineModuleResult &QFutureInterface<ctkCmdLineModuleResult>::resultReference(int index) const
  111. {
  112. QMutexLocker lock(mutex());
  113. return resultStore().resultAt(index).value();
  114. }
  115. inline const ctkCmdLineModuleResult *QFutureInterface<ctkCmdLineModuleResult>::resultPointer(int index) const
  116. {
  117. QMutexLocker lock(mutex());
  118. return resultStore().resultAt(index).pointer();
  119. }
  120. inline QList<ctkCmdLineModuleResult> QFutureInterface<ctkCmdLineModuleResult>::results()
  121. {
  122. if (this->isCanceled()) {
  123. exceptionStore().throwPossibleException();
  124. return QList<ctkCmdLineModuleResult>();
  125. }
  126. QFutureInterfaceBase::waitForResult(-1);
  127. QList<ctkCmdLineModuleResult> res;
  128. QMutexLocker lock(mutex());
  129. QtConcurrent::ResultIterator<ctkCmdLineModuleResult> it = resultStore().begin();
  130. while (it != resultStore().end()) {
  131. res.append(it.value());
  132. ++it;
  133. }
  134. return res;
  135. }
  136. typedef QFutureInterface<ctkCmdLineModuleResult> ctkCmdLineModuleFutureInterface;
  137. #endif // CTKCMDLINEMODULEFUTUREINTERFACE_H