ctkPluginAbstractTracked_p.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. * Return the customized object for the specified item
  97. *
  98. * @param item The item to lookup in the map
  99. * @return The customized object for the specified item.
  100. *
  101. * @GuardedBy this
  102. */
  103. T getCustomizedObject(S item) const;
  104. /**
  105. * Return the list of tracked items.
  106. *
  107. * @return The tracked items.
  108. * @GuardedBy this
  109. */
  110. QList<S> getTracked() const;
  111. /**
  112. * Increment the modification count. If this method is overridden, the
  113. * overriding method MUST call this method to increment the tracking count.
  114. *
  115. * @GuardedBy this
  116. */
  117. virtual void modified();
  118. /**
  119. * Returns the tracking count for this <code>ServiceTracker</code> object.
  120. *
  121. * The tracking count is initialized to 0 when this object is opened. Every
  122. * time an item is added, modified or removed from this object the tracking
  123. * count is incremented.
  124. *
  125. * @GuardedBy this
  126. * @return The tracking count for this object.
  127. */
  128. int getTrackingCount() const;
  129. /**
  130. * Call the specific customizer adding method. This method must not be
  131. * called while synchronized on this object.
  132. *
  133. * @param item S to be tracked.
  134. * @param related Action related object.
  135. * @return Customized object for the tracked item or <code>null</code> if
  136. * the item is not to be tracked.
  137. */
  138. virtual T customizerAdding(S item, const R& related) = 0;
  139. /**
  140. * Call the specific customizer modified method. This method must not be
  141. * called while synchronized on this object.
  142. *
  143. * @param item Tracked item.
  144. * @param related Action related object.
  145. * @param object Customized object for the tracked item.
  146. */
  147. virtual void customizerModified(S item, const R& related,
  148. T object) = 0;
  149. /**
  150. * Call the specific customizer removed method. This method must not be
  151. * called while synchronized on this object.
  152. *
  153. * @param item Tracked item.
  154. * @param related Action related object.
  155. * @param object Customized object for the tracked item.
  156. */
  157. virtual void customizerRemoved(S item, const R& related,
  158. T object) = 0;
  159. /**
  160. * List of items in the process of being added. This is used to deal with
  161. * nesting of events. Since events may be synchronously delivered, events
  162. * can be nested. For example, when processing the adding of a service and
  163. * the customizer causes the service to be unregistered, notification to the
  164. * nested call to untrack that the service was unregistered can be made to
  165. * the track method.
  166. *
  167. * Since the QList implementation is not synchronized, all access to
  168. * this list must be protected by the same synchronized object for
  169. * thread-safety.
  170. *
  171. * @GuardedBy this
  172. */
  173. QList<S> adding;
  174. /**
  175. * true if the tracked object is closed.
  176. *
  177. * This field is volatile because it is set by one thread and read by
  178. * another.
  179. */
  180. volatile bool closed;
  181. /**
  182. * Initial list of items for the tracker. This is used to correctly process
  183. * the initial items which could be modified before they are tracked. This
  184. * is necessary since the initial set of tracked items are not "announced"
  185. * by events and therefore the event which makes the item untracked could be
  186. * delivered before we track the item.
  187. *
  188. * An item must not be in both the initial and adding lists at the same
  189. * time. An item must be moved from the initial list to the adding list
  190. * "atomically" before we begin tracking it.
  191. *
  192. * Since the LinkedList implementation is not synchronized, all access to
  193. * this list must be protected by the same synchronized object for
  194. * thread-safety.
  195. *
  196. * @GuardedBy this
  197. */
  198. QLinkedList<S> initial;
  199. /**
  200. * Common logic to add an item to the tracker used by track and
  201. * trackInitial. The specified item must have been placed in the adding list
  202. * before calling this method.
  203. *
  204. * @param item S to be tracked.
  205. * @param related Action related object.
  206. */
  207. void trackAdding(S item, R related);
  208. private:
  209. QWaitCondition waitCond;
  210. /**
  211. * Map of tracked items to customized objects.
  212. *
  213. * @GuardedBy this
  214. */
  215. QHash<S, T> tracked;
  216. /**
  217. * Modification count. This field is initialized to zero and incremented by
  218. * modified.
  219. *
  220. * @GuardedBy this
  221. */
  222. QAtomicInt trackingCount;
  223. bool customizerAddingFinal(S item, const T& custom);
  224. };
  225. #include "ctkPluginAbstractTracked.tpp"
  226. #endif // CTKPLUGINABSTRACTTRACKED_P_H