/*============================================================================= 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 "ctkPluginAbstractTracked_p.h" #include //---------------------------------------------------------------------------- template const bool ctkPluginAbstractTracked::DEBUG_FLAG = false; //---------------------------------------------------------------------------- template ctkPluginAbstractTracked::ctkPluginAbstractTracked() { closed = false; } //---------------------------------------------------------------------------- template ctkPluginAbstractTracked::~ctkPluginAbstractTracked() { } //---------------------------------------------------------------------------- template bool ctkPluginAbstractTracked::wait(unsigned long timeout) { return waitCond.wait(this, timeout); } //---------------------------------------------------------------------------- template void ctkPluginAbstractTracked::wakeAll() { waitCond.wakeAll(); } //---------------------------------------------------------------------------- template void ctkPluginAbstractTracked::setInitial(const QList& list) { foreach (S item, list) { if (DEBUG_FLAG) { qDebug() << "ctkPluginAbstractTracked::setInitial:" << item; } initial.push_back(item); } } //---------------------------------------------------------------------------- template void ctkPluginAbstractTracked::trackInitial() { while (true) { S item(0); { QMutexLocker lock(this); if (closed || (initial.size() == 0)) { /* * if there are no more initial items */ return; /* we are done */ } /* * move the first item from the initial list to the adding list * within this synchronized block. */ item = initial.takeFirst(); if (tracked.value(item)) { /* if we are already tracking this item */ if (DEBUG_FLAG) { qDebug() << "ctkPluginAbstractTracked::trackInitial[already tracked]: " << item; } continue; /* skip this item */ } if (adding.contains(item)) { /* * if this item is already in the process of being added. */ if (DEBUG_FLAG) { qDebug() << "ctkPluginAbstractTracked::trackInitial[already adding]: " << item; } continue; /* skip this item */ } adding.push_back(item); } if (DEBUG_FLAG) { qDebug() << "ctkPluginAbstractTracked::trackInitial: " << item; } trackAdding(item, R()); /* * Begin tracking it. We call trackAdding * since we have already put the item in the * adding list. */ } } //---------------------------------------------------------------------------- template void ctkPluginAbstractTracked::close() { closed = true; } //---------------------------------------------------------------------------- template void ctkPluginAbstractTracked::track(S item, R related) { T object(0); { QMutexLocker lock(this); if (closed) { return; } object = tracked.value(item); if (!object) { /* we are not tracking the item */ if (adding.contains(item)) { /* if this item is already in the process of being added. */ if (DEBUG_FLAG) { qDebug() << "ctkPluginAbstractTracked::track[already adding]: " << item; } return; } adding.push_back(item); /* mark this item is being added */ } else { /* we are currently tracking this item */ if (DEBUG_FLAG) { qDebug() << "ctkPluginAbstractTracked::track[modified]: " << item; } modified(); /* increment modification count */ } } if (!object) { /* we are not tracking the item */ trackAdding(item, related); } else { /* Call customizer outside of synchronized region */ customizerModified(item, related, object); /* * If the customizer throws an unchecked exception, it is safe to * let it propagate */ } } //---------------------------------------------------------------------------- template void ctkPluginAbstractTracked::untrack(S item, R related) { T object(0); { QMutexLocker lock(this); if (initial.removeOne(item)) { /* if this item is already in the list * of initial references to process */ if (DEBUG_FLAG) { qDebug() << "ctkPluginAbstractTracked::untrack[removed from initial]: " << item; } return; /* we have removed it from the list and it will not be * processed */ } if (adding.removeOne(item)) { /* if the item is in the process of * being added */ if (DEBUG_FLAG) { qDebug() << "ctkPluginAbstractTracked::untrack[being added]: " << item; } return; /* * in case the item is untracked while in the process of * adding */ } object = tracked.take(item); /* * must remove from tracker before * calling customizer callback */ if (!object) { /* are we actually tracking the item */ return; } modified(); /* increment modification count */ } if (DEBUG_FLAG) { qDebug() << "ctkPluginAbstractTracked::untrack[removed]: " << item; } /* Call customizer outside of synchronized region */ customizerRemoved(item, related, object); /* * If the customizer throws an unchecked exception, it is safe to let it * propagate */ } //---------------------------------------------------------------------------- template int ctkPluginAbstractTracked::size() const { return tracked.size(); } //---------------------------------------------------------------------------- template bool ctkPluginAbstractTracked::isEmpty() const { return tracked.isEmpty(); } //---------------------------------------------------------------------------- template T ctkPluginAbstractTracked::getCustomizedObject(S item) const { return tracked.value(item); } //---------------------------------------------------------------------------- template QList ctkPluginAbstractTracked::getTracked() const { return tracked.keys(); } //---------------------------------------------------------------------------- template void ctkPluginAbstractTracked::modified() { trackingCount.ref(); } //---------------------------------------------------------------------------- template int ctkPluginAbstractTracked::getTrackingCount() const { #if QT_VERSION < QT_VERSION_CHECK(5,0,0) return trackingCount; #else return trackingCount.load(); #endif } //---------------------------------------------------------------------------- template QMap ctkPluginAbstractTracked::copyEntries(QMap& map) const { typename QHash::ConstIterator end = tracked.end(); for (typename QHash::ConstIterator it = tracked.begin(); it != end; ++it) { map.insert(it.key(), it.value()); } return map; } //---------------------------------------------------------------------------- template bool ctkPluginAbstractTracked::customizerAddingFinal(S item, const T& custom) { QMutexLocker lock(this); if (adding.removeOne(item) && !closed) { /* * if the item was not untracked during the customizer * callback */ if (custom) { tracked.insert(item, custom); modified(); /* increment modification count */ waitCond.wakeAll(); /* notify any waiters */ } return false; } else { return true; } } //---------------------------------------------------------------------------- template void ctkPluginAbstractTracked::trackAdding(S item, R related) { if (DEBUG_FLAG) { qDebug() << "ctkPluginAbstractTracked::trackAdding:" << item; } T object(0); bool becameUntracked = false; /* Call customizer outside of synchronized region */ try { object = customizerAdding(item, related); becameUntracked = this->customizerAddingFinal(item, object); } catch (...) { /* * If the customizer throws an exception, it will * propagate after the cleanup code. */ this->customizerAddingFinal(item, object); throw; } /* * The item became untracked during the customizer callback. */ if (becameUntracked && object) { if (DEBUG_FLAG) { qDebug() << "ctkPluginAbstractTracked::trackAdding[removed]: " << item; } /* Call customizer outside of synchronized region */ customizerRemoved(item, related, object); /* * If the customizer throws an unchecked exception, it is safe to * let it propagate */ } }