123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- /*=============================================================================
- Library: CTK
- Copyright (c) German Cancer Research Center,
- Division of Medical and Biological Informatics
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =============================================================================*/
- #include "ctkCmdLineModuleFutureInterface.h"
- #include "ctkCmdLineModuleFutureInterface_p.h"
- const int ctkCmdLineModuleFutureCallOutEvent::TypeId = QEvent::registerEventType();
- //----------------------------------------------------------------------------
- // ctkCmdLineModuleFutureInterfacePrivate
- //----------------------------------------------------------------------------
- ctkCmdLineModuleFutureInterfacePrivate::ctkCmdLineModuleFutureInterfacePrivate(ctkCmdLineModuleFutureInterface* q)
- : RefCount(1)
- , CanCancel(false)
- , CanPause(false)
- , q(q)
- {
- }
- //----------------------------------------------------------------------------
- void ctkCmdLineModuleFutureInterfacePrivate::sendCallOut(const ctkCmdLineModuleFutureCallOutEvent &callOutEvent)
- {
- if (OutputConnections.isEmpty())
- {
- return;
- }
- for (int i = 0; i < OutputConnections.count(); ++i)
- {
- OutputConnections.at(i)->postCmdLineModuleCallOutEvent(callOutEvent);
- }
- }
- //----------------------------------------------------------------------------
- void ctkCmdLineModuleFutureInterfacePrivate::connectOutputInterface(ctkCmdLineModuleFutureCallOutInterface *iface)
- {
- QMutexLocker locker(&Mutex);
- if (q->isStarted())
- {
- if (!this->OutputData.isEmpty())
- {
- iface->postCmdLineModuleCallOutEvent(ctkCmdLineModuleFutureCallOutEvent(ctkCmdLineModuleFutureCallOutEvent::OutputReady));
- }
- if (!this->ErrorData.isEmpty())
- {
- iface->postCmdLineModuleCallOutEvent(ctkCmdLineModuleFutureCallOutEvent(ctkCmdLineModuleFutureCallOutEvent::ErrorReady));
- }
- }
- OutputConnections.append(iface);
- }
- //----------------------------------------------------------------------------
- void ctkCmdLineModuleFutureInterfacePrivate::disconnectOutputInterface(ctkCmdLineModuleFutureCallOutInterface *iface)
- {
- QMutexLocker lock(&Mutex);
- const int index = OutputConnections.indexOf(iface);
- if (index == -1)
- return;
- OutputConnections.removeAt(index);
- iface->cmdLineModuleCallOutInterfaceDisconnected();
- }
- //----------------------------------------------------------------------------
- // QFutureInterface<ctkCmdLineModuleResult>
- //----------------------------------------------------------------------------
- QFutureInterface<ctkCmdLineModuleResult>::QFutureInterface(State initialState)
- : QFutureInterfaceBase(initialState)
- , d(new ctkCmdLineModuleFutureInterfacePrivate(this))
- {
- }
- //----------------------------------------------------------------------------
- QFutureInterface<ctkCmdLineModuleResult>::QFutureInterface(const QFutureInterface& other)
- : QFutureInterfaceBase(other)
- , d(other.d)
- {
- d->RefCount.ref();
- }
- //----------------------------------------------------------------------------
- QFutureInterface<ctkCmdLineModuleResult>::~QFutureInterface()
- {
- if (referenceCountIsOne())
- resultStore().clear();
- if (!d->RefCount.deref())
- {
- delete d;
- }
- }
- //----------------------------------------------------------------------------
- QFutureInterface<ctkCmdLineModuleResult> QFutureInterface<ctkCmdLineModuleResult>::canceledResult()
- {
- return QFutureInterface(State(Started | Finished | Canceled));
- }
- //----------------------------------------------------------------------------
- QFutureInterface<ctkCmdLineModuleResult>&
- QFutureInterface<ctkCmdLineModuleResult>::operator=(const QFutureInterface& other)
- {
- if (referenceCountIsOne())
- resultStore().clear();
- QFutureInterfaceBase::operator=(other);
- other.d->RefCount.ref();
- if (!d->RefCount.deref())
- delete d;
- d = other.d;
- // update the q pointer in the private implementation
- d->q = this;
- return *this;
- }
- //----------------------------------------------------------------------------
- bool QFutureInterface<ctkCmdLineModuleResult>::canCancel() const
- {
- return d->CanCancel;
- }
- //----------------------------------------------------------------------------
- void QFutureInterface<ctkCmdLineModuleResult>::setCanCancel(bool canCancel)
- {
- d->CanCancel = canCancel;
- }
- //----------------------------------------------------------------------------
- bool QFutureInterface<ctkCmdLineModuleResult>::canPause() const
- {
- return d->CanPause;
- }
- //----------------------------------------------------------------------------
- void QFutureInterface<ctkCmdLineModuleResult>::setCanPause(bool canPause)
- {
- d->CanPause = canPause;
- }
- //----------------------------------------------------------------------------
- void QFutureInterface<ctkCmdLineModuleResult>::reportOutputData(const QByteArray& outputData)
- {
- QMutexLocker l(&d->Mutex);
- if (isCanceled() || isFinished()) return;
- d->OutputData.append(outputData);
- d->sendCallOut(ctkCmdLineModuleFutureCallOutEvent(ctkCmdLineModuleFutureCallOutEvent::OutputReady));
- }
- //----------------------------------------------------------------------------
- void QFutureInterface<ctkCmdLineModuleResult>::reportErrorData(const QByteArray& errorData)
- {
- QMutexLocker l(&d->Mutex);
- if (isCanceled() || isFinished()) return;
- d->ErrorData.append(errorData);
- d->sendCallOut(ctkCmdLineModuleFutureCallOutEvent(ctkCmdLineModuleFutureCallOutEvent::ErrorReady));
- }
- //----------------------------------------------------------------------------
- QByteArray QFutureInterface<ctkCmdLineModuleResult>::outputData(int position, int size) const
- {
- QMutexLocker l(&d->Mutex);
- if (size < 0) size = d->OutputData.size();
- if (size > d->OutputData.size() - position) size = d->OutputData.size() - position;
- return QByteArray(d->OutputData.data() + position, size);
- }
- //----------------------------------------------------------------------------
- QByteArray QFutureInterface<ctkCmdLineModuleResult>::errorData(int position, int size) const
- {
- QMutexLocker l(&d->Mutex);
- if (size < 0) size = d->ErrorData.size();
- if (size > d->ErrorData.size() - position) size = d->ErrorData.size() - position;
- return QByteArray(d->ErrorData.data() + position, size);
- }
|