ctkCmdLineModuleFutureInterface.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. }
  75. //----------------------------------------------------------------------------
  76. QFutureInterface<ctkCmdLineModuleResult>::QFutureInterface(const QFutureInterface& other)
  77. : QFutureInterfaceBase(other)
  78. , d(other.d)
  79. {
  80. d->RefCount.ref();
  81. }
  82. //----------------------------------------------------------------------------
  83. QFutureInterface<ctkCmdLineModuleResult>::~QFutureInterface()
  84. {
  85. if (referenceCountIsOne())
  86. resultStore().clear();
  87. if (!d->RefCount.deref())
  88. {
  89. delete d;
  90. }
  91. }
  92. //----------------------------------------------------------------------------
  93. QFutureInterface<ctkCmdLineModuleResult> QFutureInterface<ctkCmdLineModuleResult>::canceledResult()
  94. {
  95. return QFutureInterface(State(Started | Finished | Canceled));
  96. }
  97. //----------------------------------------------------------------------------
  98. QFutureInterface<ctkCmdLineModuleResult>&
  99. QFutureInterface<ctkCmdLineModuleResult>::operator=(const QFutureInterface& other)
  100. {
  101. if (referenceCountIsOne())
  102. resultStore().clear();
  103. QFutureInterfaceBase::operator=(other);
  104. other.d->RefCount.ref();
  105. if (!d->RefCount.deref())
  106. delete d;
  107. d = other.d;
  108. // update the q pointer in the private implementation
  109. d->q = this;
  110. return *this;
  111. }
  112. //----------------------------------------------------------------------------
  113. bool QFutureInterface<ctkCmdLineModuleResult>::canCancel() const
  114. {
  115. return d->CanCancel;
  116. }
  117. //----------------------------------------------------------------------------
  118. void QFutureInterface<ctkCmdLineModuleResult>::setCanCancel(bool canCancel)
  119. {
  120. d->CanCancel = canCancel;
  121. }
  122. //----------------------------------------------------------------------------
  123. bool QFutureInterface<ctkCmdLineModuleResult>::canPause() const
  124. {
  125. return d->CanPause;
  126. }
  127. //----------------------------------------------------------------------------
  128. void QFutureInterface<ctkCmdLineModuleResult>::setCanPause(bool canPause)
  129. {
  130. d->CanPause = canPause;
  131. }
  132. //----------------------------------------------------------------------------
  133. void QFutureInterface<ctkCmdLineModuleResult>::reportOutputData(const QByteArray& outputData)
  134. {
  135. QMutexLocker l(&d->Mutex);
  136. if (isCanceled() || isFinished()) return;
  137. d->OutputData.append(outputData);
  138. d->sendCallOut(ctkCmdLineModuleFutureCallOutEvent(ctkCmdLineModuleFutureCallOutEvent::OutputReady));
  139. }
  140. //----------------------------------------------------------------------------
  141. void QFutureInterface<ctkCmdLineModuleResult>::reportErrorData(const QByteArray& errorData)
  142. {
  143. QMutexLocker l(&d->Mutex);
  144. if (isCanceled() || isFinished()) return;
  145. d->ErrorData.append(errorData);
  146. d->sendCallOut(ctkCmdLineModuleFutureCallOutEvent(ctkCmdLineModuleFutureCallOutEvent::ErrorReady));
  147. }
  148. //----------------------------------------------------------------------------
  149. QByteArray QFutureInterface<ctkCmdLineModuleResult>::outputData(int position, int size) const
  150. {
  151. QMutexLocker l(&d->Mutex);
  152. if (size < 0) size = d->OutputData.size();
  153. if (size > d->OutputData.size() - position) size = d->OutputData.size() - position;
  154. return QByteArray(d->OutputData.data() + position, size);
  155. }
  156. //----------------------------------------------------------------------------
  157. QByteArray QFutureInterface<ctkCmdLineModuleResult>::errorData(int position, int size) const
  158. {
  159. QMutexLocker l(&d->Mutex);
  160. if (size < 0) size = d->ErrorData.size();
  161. if (size > d->ErrorData.size() - position) size = d->ErrorData.size() - position;
  162. return QByteArray(d->ErrorData.data() + position, size);
  163. }