ctkCmdLineModuleFutureInterface.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include "ctkCmdLineModuleFutureInterface.h"
  16. #include "ctkCmdLineModuleFutureInterface_p.h"
  17. const int ctkCmdLineModuleFutureCallOutEvent::TypeId = QEvent::registerEventType();
  18. //----------------------------------------------------------------------------
  19. // ctkCmdLineModuleFutureInterfacePrivate
  20. //----------------------------------------------------------------------------
  21. ctkCmdLineModuleFutureInterfacePrivate::ctkCmdLineModuleFutureInterfacePrivate(ctkCmdLineModuleFutureInterface* q)
  22. : RefCount(1)
  23. , CanCancel(false)
  24. , CanPause(false)
  25. , q(q)
  26. {
  27. }
  28. //----------------------------------------------------------------------------
  29. void ctkCmdLineModuleFutureInterfacePrivate::sendCallOut(const ctkCmdLineModuleFutureCallOutEvent &callOutEvent)
  30. {
  31. if (OutputConnections.isEmpty())
  32. {
  33. return;
  34. }
  35. for (int i = 0; i < OutputConnections.count(); ++i)
  36. {
  37. OutputConnections.at(i)->postCmdLineModuleCallOutEvent(callOutEvent);
  38. }
  39. }
  40. //----------------------------------------------------------------------------
  41. void ctkCmdLineModuleFutureInterfacePrivate::connectOutputInterface(ctkCmdLineModuleFutureCallOutInterface *iface)
  42. {
  43. QMutexLocker locker(&Mutex);
  44. if (q->isStarted())
  45. {
  46. if (!this->OutputData.isEmpty())
  47. {
  48. iface->postCmdLineModuleCallOutEvent(ctkCmdLineModuleFutureCallOutEvent(ctkCmdLineModuleFutureCallOutEvent::OutputReady));
  49. }
  50. if (!this->ErrorData.isEmpty())
  51. {
  52. iface->postCmdLineModuleCallOutEvent(ctkCmdLineModuleFutureCallOutEvent(ctkCmdLineModuleFutureCallOutEvent::ErrorReady));
  53. }
  54. }
  55. OutputConnections.append(iface);
  56. }
  57. //----------------------------------------------------------------------------
  58. void ctkCmdLineModuleFutureInterfacePrivate::disconnectOutputInterface(ctkCmdLineModuleFutureCallOutInterface *iface)
  59. {
  60. QMutexLocker lock(&Mutex);
  61. const int index = OutputConnections.indexOf(iface);
  62. if (index == -1)
  63. return;
  64. OutputConnections.removeAt(index);
  65. iface->cmdLineModuleCallOutInterfaceDisconnected();
  66. }
  67. //----------------------------------------------------------------------------
  68. // QFutureInterface<ctkCmdLineModuleResult>
  69. //----------------------------------------------------------------------------
  70. QFutureInterface<ctkCmdLineModuleResult>::QFutureInterface(State initialState)
  71. : QFutureInterfaceBase(initialState)
  72. , d(new ctkCmdLineModuleFutureInterfacePrivate(this))
  73. {
  74. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  75. refT();
  76. #endif
  77. }
  78. //----------------------------------------------------------------------------
  79. QFutureInterface<ctkCmdLineModuleResult>::QFutureInterface(const QFutureInterface& other)
  80. : QFutureInterfaceBase(other)
  81. , d(other.d)
  82. {
  83. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  84. refT();
  85. #endif
  86. d->RefCount.ref();
  87. }
  88. //----------------------------------------------------------------------------
  89. QFutureInterface<ctkCmdLineModuleResult>::~QFutureInterface()
  90. {
  91. #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
  92. if (referenceCountIsOne())
  93. resultStore().clear();
  94. #else
  95. if (!derefT())
  96. resultStore().clear();
  97. #endif
  98. if (!d->RefCount.deref())
  99. {
  100. delete d;
  101. }
  102. }
  103. //----------------------------------------------------------------------------
  104. QFutureInterface<ctkCmdLineModuleResult> QFutureInterface<ctkCmdLineModuleResult>::canceledResult()
  105. {
  106. return QFutureInterface(State(Started | Finished | Canceled));
  107. }
  108. //----------------------------------------------------------------------------
  109. QFutureInterface<ctkCmdLineModuleResult>&
  110. QFutureInterface<ctkCmdLineModuleResult>::operator=(const QFutureInterface& other)
  111. {
  112. #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
  113. if (referenceCountIsOne())
  114. #else
  115. other.refT();
  116. if (!derefT())
  117. #endif
  118. resultStore().clear();
  119. QFutureInterfaceBase::operator=(other);
  120. other.d->RefCount.ref();
  121. if (!d->RefCount.deref())
  122. delete d;
  123. d = other.d;
  124. // update the q pointer in the private implementation
  125. d->q = this;
  126. return *this;
  127. }
  128. //----------------------------------------------------------------------------
  129. bool QFutureInterface<ctkCmdLineModuleResult>::canCancel() const
  130. {
  131. return d->CanCancel;
  132. }
  133. //----------------------------------------------------------------------------
  134. void QFutureInterface<ctkCmdLineModuleResult>::setCanCancel(bool canCancel)
  135. {
  136. d->CanCancel = canCancel;
  137. }
  138. //----------------------------------------------------------------------------
  139. bool QFutureInterface<ctkCmdLineModuleResult>::canPause() const
  140. {
  141. return d->CanPause;
  142. }
  143. //----------------------------------------------------------------------------
  144. void QFutureInterface<ctkCmdLineModuleResult>::setCanPause(bool canPause)
  145. {
  146. d->CanPause = canPause;
  147. }
  148. //----------------------------------------------------------------------------
  149. void QFutureInterface<ctkCmdLineModuleResult>::reportOutputData(const QByteArray& outputData)
  150. {
  151. QMutexLocker l(&d->Mutex);
  152. if (isCanceled() || isFinished()) return;
  153. d->OutputData.append(outputData);
  154. d->sendCallOut(ctkCmdLineModuleFutureCallOutEvent(ctkCmdLineModuleFutureCallOutEvent::OutputReady));
  155. }
  156. //----------------------------------------------------------------------------
  157. void QFutureInterface<ctkCmdLineModuleResult>::reportErrorData(const QByteArray& errorData)
  158. {
  159. QMutexLocker l(&d->Mutex);
  160. if (isCanceled() || isFinished()) return;
  161. d->ErrorData.append(errorData);
  162. d->sendCallOut(ctkCmdLineModuleFutureCallOutEvent(ctkCmdLineModuleFutureCallOutEvent::ErrorReady));
  163. }
  164. //----------------------------------------------------------------------------
  165. QByteArray QFutureInterface<ctkCmdLineModuleResult>::outputData(int position, int size) const
  166. {
  167. QMutexLocker l(&d->Mutex);
  168. if (size < 0) size = d->OutputData.size();
  169. if (size > d->OutputData.size() - position) size = d->OutputData.size() - position;
  170. return QByteArray(d->OutputData.data() + position, size);
  171. }
  172. //----------------------------------------------------------------------------
  173. QByteArray QFutureInterface<ctkCmdLineModuleResult>::errorData(int position, int size) const
  174. {
  175. QMutexLocker l(&d->Mutex);
  176. if (size < 0) size = d->ErrorData.size();
  177. if (size > d->ErrorData.size() - position) size = d->ErrorData.size() - position;
  178. return QByteArray(d->ErrorData.data() + position, size);
  179. }