123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- /*=============================================================================
- 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 <QDebug>
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- const bool ctkPluginAbstractTracked<S,T,R>::DEBUG = false;
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- ctkPluginAbstractTracked<S,T,R>::ctkPluginAbstractTracked()
- {
- closed = false;
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- ctkPluginAbstractTracked<S,T,R>::~ctkPluginAbstractTracked()
- {
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- bool ctkPluginAbstractTracked<S,T,R>::wait(unsigned long timeout)
- {
- return waitCond.wait(this, timeout);
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- void ctkPluginAbstractTracked<S,T,R>::wakeAll()
- {
- waitCond.wakeAll();
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- void ctkPluginAbstractTracked<S,T,R>::setInitial(const QList<S>& list)
- {
- foreach (S item, list)
- {
- if (DEBUG)
- {
- qDebug() << "ctkPluginAbstractTracked::setInitial:" << item;
- }
- initial.push_back(item);
- }
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- void ctkPluginAbstractTracked<S,T,R>::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)
- {
- 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)
- {
- qDebug() << "ctkPluginAbstractTracked::trackInitial[already adding]: " << item;
- }
- continue; /* skip this item */
- }
- adding.push_back(item);
- }
- if (DEBUG)
- {
- 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<class S, class T, class R>
- void ctkPluginAbstractTracked<S,T,R>::close()
- {
- closed = true;
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- void ctkPluginAbstractTracked<S,T,R>::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)
- {
- 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)
- {
- 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<class S, class T, class R>
- void ctkPluginAbstractTracked<S,T,R>::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)
- {
- 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)
- {
- 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)
- {
- 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<class S, class T, class R>
- int ctkPluginAbstractTracked<S,T,R>::size() const
- {
- return tracked.size();
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- bool ctkPluginAbstractTracked<S,T,R>::isEmpty() const
- {
- return tracked.isEmpty();
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- T ctkPluginAbstractTracked<S,T,R>::getCustomizedObject(S item) const
- {
- return tracked.value(item);
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- QList<S> ctkPluginAbstractTracked<S,T,R>::getTracked() const
- {
- return tracked.keys();
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- void ctkPluginAbstractTracked<S,T,R>::modified()
- {
- trackingCount.ref();
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- int ctkPluginAbstractTracked<S,T,R>::getTrackingCount() const
- {
- #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
- return trackingCount;
- #else
- return trackingCount.load();
- #endif
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- QMap<S,T> ctkPluginAbstractTracked<S,T,R>::copyEntries(QMap<S,T>& map) const
- {
- typename QHash<S,T>::ConstIterator end = tracked.end();
- for (typename QHash<S,T>::ConstIterator it = tracked.begin();
- it != end; ++it)
- {
- map.insert(it.key(), it.value());
- }
- return map;
- }
- //----------------------------------------------------------------------------
- template<class S, class T, class R>
- bool ctkPluginAbstractTracked<S,T,R>::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<class S, class T, class R>
- void ctkPluginAbstractTracked<S,T,R>::trackAdding(S item, R related)
- {
- if (DEBUG)
- {
- 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)
- {
- 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
- */
- }
- }
|