ctkPluginAbstractTracked_p.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #ifndef CTKPLUGINABSTRACTTRACKED_P_H
  16. #define CTKPLUGINABSTRACTTRACKED_P_H
  17. #include <QHash>
  18. #include <QMutex>
  19. #include <QWaitCondition>
  20. #include <QLinkedList>
  21. #include <QVariant>
  22. /**
  23. * Abstract class to track items. If a Tracker is reused (closed then reopened),
  24. * then a new ctkPluginAbstractTracked object is used. This class acts as a map of tracked
  25. * item -> customized object. Subclasses of this class will act as the listener
  26. * object for the tracker. This class is used to synchronize access to the
  27. * tracked items. This is not a public class. It is only for use by the
  28. * implementation of the Tracker class.
  29. *
  30. * @tparam S The tracked item. It is the key.
  31. * @tparam T The value mapped to the tracked item.
  32. * @tparam R The reason the tracked item is being tracked or untracked.
  33. * @ThreadSafe
  34. */
  35. template<class S, class T, class R>
  36. class ctkPluginAbstractTracked : public QMutex
  37. {
  38. public:
  39. /* set this to true to compile in debug messages */
  40. static const bool DEBUG; // = false;
  41. /**
  42. * ctkPluginAbstractTracked constructor.
  43. */
  44. ctkPluginAbstractTracked();
  45. virtual ~ctkPluginAbstractTracked();
  46. bool wait(unsigned long timeout);
  47. void wakeAll();
  48. /**
  49. * Set initial list of items into tracker before events begin to be
  50. * received.
  51. *
  52. * This method must be called from Tracker's open method while synchronized
  53. * on this object in the same synchronized block as the add listener call.
  54. *
  55. * @param list The initial list of items to be tracked. <code>null</code>
  56. * entries in the list are ignored.
  57. * @GuardedBy this
  58. */
  59. void setInitial(const QList<S>& list);
  60. /**
  61. * Track the initial list of items. This is called after events can begin to
  62. * be received.
  63. *
  64. * This method must be called from Tracker's open method while not
  65. * synchronized on this object after the add listener call.
  66. *
  67. */
  68. void trackInitial();
  69. /**
  70. * Called by the owning Tracker object when it is closed.
  71. */
  72. void close();
  73. /**
  74. * Begin to track an item.
  75. *
  76. * @param item S to be tracked.
  77. * @param related Action related object.
  78. */
  79. void track(S item, R related);
  80. /**
  81. * Discontinue tracking the item.
  82. *
  83. * @param item S to be untracked.
  84. * @param related Action related object.
  85. */
  86. void untrack(S item, R related);
  87. /**
  88. * Returns the number of tracked items.
  89. *
  90. * @return The number of tracked items.
  91. *
  92. * @GuardedBy this
  93. */
  94. int size() const;
  95. /**
  96. * Returns if the tracker is empty.
  97. *
  98. * @return Whether the tracker is empty.
  99. *
  100. * @GuardedBy this
  101. */
  102. bool isEmpty() const;
  103. /**
  104. * Return the customized object for the specified item
  105. *
  106. * @param item The item to lookup in the map
  107. * @return The customized object for the specified item.
  108. *
  109. * @GuardedBy this
  110. */
  111. T getCustomizedObject(S item) const;
  112. /**
  113. * Return the list of tracked items.
  114. *
  115. * @return The tracked items.
  116. * @GuardedBy this
  117. */
  118. QList<S> getTracked() const;
  119. /**
  120. * Increment the modification count. If this method is overridden, the
  121. * overriding method MUST call this method to increment the tracking count.
  122. *
  123. * @GuardedBy this
  124. */
  125. virtual void modified();
  126. /**
  127. * Returns the tracking count for this <code>ServiceTracker</code> object.
  128. *
  129. * The tracking count is initialized to 0 when this object is opened. Every
  130. * time an item is added, modified or removed from this object the tracking
  131. * count is incremented.
  132. *
  133. * @GuardedBy this
  134. * @return The tracking count for this object.
  135. */
  136. int getTrackingCount() const;
  137. /**
  138. * Copy the tracked items and associated values into the specified map.
  139. *
  140. * @param map The map into which to copy the tracked items and associated
  141. * values. This map must not be a user provided map so that user code
  142. * is not executed while synchronized on this.
  143. * @return The specified map.
  144. * @GuardedBy this
  145. */
  146. QMap<S,T> copyEntries(QMap<S,T>& map) const;
  147. /**
  148. * Call the specific customizer adding method. This method must not be
  149. * called while synchronized on this object.
  150. *
  151. * @param item S to be tracked.
  152. * @param related Action related object.
  153. * @return Customized object for the tracked item or <code>null</code> if
  154. * the item is not to be tracked.
  155. */
  156. virtual T customizerAdding(S item, const R& related) = 0;
  157. /**
  158. * Call the specific customizer modified method. This method must not be
  159. * called while synchronized on this object.
  160. *
  161. * @param item Tracked item.
  162. * @param related Action related object.
  163. * @param object Customized object for the tracked item.
  164. */
  165. virtual void customizerModified(S item, const R& related,
  166. T object) = 0;
  167. /**
  168. * Call the specific customizer removed method. This method must not be
  169. * called while synchronized on this object.
  170. *
  171. * @param item Tracked item.
  172. * @param related Action related object.
  173. * @param object Customized object for the tracked item.
  174. */
  175. virtual void customizerRemoved(S item, const R& related,
  176. T object) = 0;
  177. /**
  178. * List of items in the process of being added. This is used to deal with
  179. * nesting of events. Since events may be synchronously delivered, events
  180. * can be nested. For example, when processing the adding of a service and
  181. * the customizer causes the service to be unregistered, notification to the
  182. * nested call to untrack that the service was unregistered can be made to
  183. * the track method.
  184. *
  185. * Since the QList implementation is not synchronized, all access to
  186. * this list must be protected by the same synchronized object for
  187. * thread-safety.
  188. *
  189. * @GuardedBy this
  190. */
  191. QList<S> adding;
  192. /**
  193. * true if the tracked object is closed.
  194. *
  195. * This field is volatile because it is set by one thread and read by
  196. * another.
  197. */
  198. volatile bool closed;
  199. /**
  200. * Initial list of items for the tracker. This is used to correctly process
  201. * the initial items which could be modified before they are tracked. This
  202. * is necessary since the initial set of tracked items are not "announced"
  203. * by events and therefore the event which makes the item untracked could be
  204. * delivered before we track the item.
  205. *
  206. * An item must not be in both the initial and adding lists at the same
  207. * time. An item must be moved from the initial list to the adding list
  208. * "atomically" before we begin tracking it.
  209. *
  210. * Since the LinkedList implementation is not synchronized, all access to
  211. * this list must be protected by the same synchronized object for
  212. * thread-safety.
  213. *
  214. * @GuardedBy this
  215. */
  216. QLinkedList<S> initial;
  217. /**
  218. * Common logic to add an item to the tracker used by track and
  219. * trackInitial. The specified item must have been placed in the adding list
  220. * before calling this method.
  221. *
  222. * @param item S to be tracked.
  223. * @param related Action related object.
  224. */
  225. void trackAdding(S item, R related);
  226. private:
  227. QWaitCondition waitCond;
  228. /**
  229. * Map of tracked items to customized objects.
  230. *
  231. * @GuardedBy this
  232. */
  233. QHash<S, T> tracked;
  234. /**
  235. * Modification count. This field is initialized to zero and incremented by
  236. * modified.
  237. *
  238. * @GuardedBy this
  239. */
  240. QAtomicInt trackingCount;
  241. bool customizerAddingFinal(S item, const T& custom);
  242. };
  243. #include "ctkPluginAbstractTracked.tpp"
  244. #endif // CTKPLUGINABSTRACTTRACKED_P_H