ctkCmdLineModuleFutureInterface.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #elif QT_VERSION < QT_VERSION_CHECK(5,9,0)
  95. if (!derefT())
  96. resultStore().clear();
  97. #else
  98. if (!derefT())
  99. resultStore().clear<ctkCmdLineModuleResult>();
  100. #endif
  101. if (!d->RefCount.deref())
  102. {
  103. delete d;
  104. }
  105. }
  106. //----------------------------------------------------------------------------
  107. QFutureInterface<ctkCmdLineModuleResult> QFutureInterface<ctkCmdLineModuleResult>::canceledResult()
  108. {
  109. return QFutureInterface(State(Started | Finished | Canceled));
  110. }
  111. //----------------------------------------------------------------------------
  112. QFutureInterface<ctkCmdLineModuleResult>&
  113. QFutureInterface<ctkCmdLineModuleResult>::operator=(const QFutureInterface& other)
  114. {
  115. #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
  116. if (referenceCountIsOne())
  117. #else
  118. other.refT();
  119. if (!derefT())
  120. #endif
  121. #if QT_VERSION < QT_VERSION_CHECK(5,9,0)
  122. resultStore().clear();
  123. #else
  124. resultStore().clear<ctkCmdLineModuleResult>();
  125. #endif
  126. QFutureInterfaceBase::operator=(other);
  127. other.d->RefCount.ref();
  128. if (!d->RefCount.deref())
  129. delete d;
  130. d = other.d;
  131. // update the q pointer in the private implementation
  132. d->q = this;
  133. return *this;
  134. }
  135. //----------------------------------------------------------------------------
  136. bool QFutureInterface<ctkCmdLineModuleResult>::canCancel() const
  137. {
  138. return d->CanCancel;
  139. }
  140. //----------------------------------------------------------------------------
  141. void QFutureInterface<ctkCmdLineModuleResult>::setCanCancel(bool canCancel)
  142. {
  143. d->CanCancel = canCancel;
  144. }
  145. //----------------------------------------------------------------------------
  146. bool QFutureInterface<ctkCmdLineModuleResult>::canPause() const
  147. {
  148. return d->CanPause;
  149. }
  150. //----------------------------------------------------------------------------
  151. void QFutureInterface<ctkCmdLineModuleResult>::setCanPause(bool canPause)
  152. {
  153. d->CanPause = canPause;
  154. }
  155. //----------------------------------------------------------------------------
  156. void QFutureInterface<ctkCmdLineModuleResult>::reportOutputData(const QByteArray& outputData)
  157. {
  158. QMutexLocker l(&d->Mutex);
  159. if (isCanceled() || isFinished()) return;
  160. d->OutputData.append(outputData);
  161. d->sendCallOut(ctkCmdLineModuleFutureCallOutEvent(ctkCmdLineModuleFutureCallOutEvent::OutputReady));
  162. }
  163. //----------------------------------------------------------------------------
  164. void QFutureInterface<ctkCmdLineModuleResult>::reportErrorData(const QByteArray& errorData)
  165. {
  166. QMutexLocker l(&d->Mutex);
  167. if (isCanceled() || isFinished()) return;
  168. d->ErrorData.append(errorData);
  169. d->sendCallOut(ctkCmdLineModuleFutureCallOutEvent(ctkCmdLineModuleFutureCallOutEvent::ErrorReady));
  170. }
  171. //----------------------------------------------------------------------------
  172. QByteArray QFutureInterface<ctkCmdLineModuleResult>::outputData(int position, int size) const
  173. {
  174. QMutexLocker l(&d->Mutex);
  175. if (size < 0) size = d->OutputData.size();
  176. if (size > d->OutputData.size() - position) size = d->OutputData.size() - position;
  177. return QByteArray(d->OutputData.data() + position, size);
  178. }
  179. //----------------------------------------------------------------------------
  180. QByteArray QFutureInterface<ctkCmdLineModuleResult>::errorData(int position, int size) const
  181. {
  182. QMutexLocker l(&d->Mutex);
  183. if (size < 0) size = d->ErrorData.size();
  184. if (size > d->ErrorData.size() - position) size = d->ErrorData.size() - position;
  185. return QByteArray(d->ErrorData.data() + position, size);
  186. }