ctkPluginAbstractTracked_p.h 8.2 KB

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