ctkCmdLineModuleFuture.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 CTKCMDLINEMODULEFUTURE_H
  16. #define CTKCMDLINEMODULEFUTURE_H
  17. #include "ctkCmdLineModuleFutureInterface.h"
  18. #include <QString>
  19. #include <QVariant>
  20. #include <QFuture>
  21. template <typename ctkCmdLineModuleResult>
  22. class QFutureWatcher;
  23. template <>
  24. class QFutureWatcher<void>;
  25. /**
  26. * QFuture specialization with two additional methods:
  27. *
  28. * - bool canCancel()
  29. * - bool canPause()
  30. */
  31. template<>
  32. class QFuture<ctkCmdLineModuleResult>
  33. {
  34. public:
  35. QFuture()
  36. : d(QFutureInterface<ctkCmdLineModuleResult>::canceledResult())
  37. { }
  38. explicit QFuture(QFutureInterface<ctkCmdLineModuleResult> *p) // internal
  39. : d(*p)
  40. { }
  41. QFuture(const QFuture &other)
  42. : d(other.d)
  43. { }
  44. ~QFuture()
  45. { }
  46. inline QFuture &operator=(const QFuture &other);
  47. bool operator==(const QFuture &other) const { return (d == other.d); }
  48. bool operator!=(const QFuture &other) const { return (d != other.d); }
  49. // additional methods
  50. bool canCancel() const { return d.canCancel(); }
  51. bool canPause() const { return d.canPause(); }
  52. void cancel() { d.cancel(); }
  53. bool isCanceled() const { return d.isCanceled(); }
  54. void setPaused(bool paused) { d.setPaused(paused); }
  55. bool isPaused() const { return d.isPaused(); }
  56. void pause() { setPaused(true); }
  57. void resume() { setPaused(false); }
  58. void togglePaused() { d.togglePaused(); }
  59. bool isStarted() const { return d.isStarted(); }
  60. bool isFinished() const { return d.isFinished(); }
  61. bool isRunning() const { return d.isRunning(); }
  62. int resultCount() const { return d.resultCount(); }
  63. int progressValue() const { return d.progressValue(); }
  64. int progressMinimum() const { return d.progressMinimum(); }
  65. int progressMaximum() const { return d.progressMaximum(); }
  66. QString progressText() const { return d.progressText(); }
  67. void waitForFinished() { d.waitForFinished(); }
  68. inline ctkCmdLineModuleResult result() const;
  69. inline ctkCmdLineModuleResult resultAt(int index) const;
  70. bool isResultReadyAt(int resultIndex) const { return d.isResultReadyAt(resultIndex); }
  71. operator ctkCmdLineModuleResult() const { return result(); }
  72. QList<ctkCmdLineModuleResult> results() const { return d.results(); }
  73. class const_iterator
  74. {
  75. public:
  76. typedef std::bidirectional_iterator_tag iterator_category;
  77. typedef qptrdiff difference_type;
  78. typedef ctkCmdLineModuleResult value_type;
  79. typedef const ctkCmdLineModuleResult *pointer;
  80. typedef const ctkCmdLineModuleResult &reference;
  81. inline const_iterator() {}
  82. inline const_iterator(QFuture const * const _future, int _index) : future(_future), index(_index) {}
  83. inline const_iterator(const const_iterator &o) : future(o.future), index(o.index) {}
  84. inline const_iterator &operator=(const const_iterator &o)
  85. { future = o.future; index = o.index; return *this; }
  86. inline const ctkCmdLineModuleResult &operator*() const { return future->d.resultReference(index); }
  87. inline const ctkCmdLineModuleResult *operator->() const { return future->d.resultPointer(index); }
  88. inline bool operator!=(const const_iterator &other) const
  89. {
  90. if (index == -1 && other.index == -1) // comparing end != end?
  91. return false;
  92. if (other.index == -1)
  93. return (future->isRunning() || (index < future->resultCount()));
  94. return (index != other.index);
  95. }
  96. inline bool operator==(const const_iterator &o) const { return !operator!=(o); }
  97. inline const_iterator &operator++() { ++index; return *this; }
  98. inline const_iterator operator++(int) { const_iterator r = *this; ++index; return r; }
  99. inline const_iterator &operator--() { --index; return *this; }
  100. inline const_iterator operator--(int) { const_iterator r = *this; --index; return r; }
  101. inline const_iterator operator+(int j) const { return const_iterator(future, index + j); }
  102. inline const_iterator operator-(int j) const { return const_iterator(future, index - j); }
  103. inline const_iterator &operator+=(int j) { index += j; return *this; }
  104. inline const_iterator &operator-=(int j) { index -= j; return *this; }
  105. private:
  106. QFuture const * future;
  107. int index;
  108. };
  109. friend class const_iterator;
  110. typedef const_iterator ConstIterator;
  111. const_iterator begin() const { return const_iterator(this, 0); }
  112. const_iterator constBegin() const { return const_iterator(this, 0); }
  113. const_iterator end() const { return const_iterator(this, -1); }
  114. const_iterator constEnd() const { return const_iterator(this, -1); }
  115. private:
  116. friend class QFutureWatcher<ctkCmdLineModuleResult>;
  117. public: // Warning: the d pointer is not documented and is considered private.
  118. mutable QFutureInterface<ctkCmdLineModuleResult> d;
  119. };
  120. typedef QFuture<ctkCmdLineModuleResult> ctkCmdLineModuleFuture;
  121. inline ctkCmdLineModuleFuture& ctkCmdLineModuleFuture::operator=(const ctkCmdLineModuleFuture& other)
  122. {
  123. d = other.d;
  124. return *this;
  125. }
  126. inline ctkCmdLineModuleResult ctkCmdLineModuleFuture::result() const
  127. {
  128. d.waitForResult(0);
  129. return d.resultReference(0);
  130. }
  131. inline ctkCmdLineModuleResult ctkCmdLineModuleFuture::resultAt(int index) const
  132. {
  133. d.waitForResult(index);
  134. return d.resultReference(index);
  135. }
  136. inline ctkCmdLineModuleFuture ctkCmdLineModuleFutureInterface::future()
  137. {
  138. return ctkCmdLineModuleFuture(this);
  139. }
  140. #endif // CTKCMDLINEMODULEFUTURE_H