ctkPluginAbstractTracked_p.h 7.5 KB

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