ctkServiceReference.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 CTKSERVICEREFERENCE_H
  16. #define CTKSERVICEREFERENCE_H
  17. #include <QVariant>
  18. #include <QMetaType>
  19. #include "ctkPlugin.h"
  20. #include "ctkPluginFrameworkExport.h"
  21. class ctkServiceRegistrationPrivate;
  22. class ctkServiceReferencePrivate;
  23. class ctkServiceEvent;
  24. /**
  25. * \ingroup PluginFramework
  26. *
  27. * A reference to a service.
  28. *
  29. * <p>
  30. * The Framework returns <code>ctkServiceReference</code> objects from the
  31. * <code>ctkPluginContext::getServiceReference</code> and
  32. * <code>ctkPluginContext::getServiceReferences</code> methods.
  33. * <p>
  34. * A <code>ctkServiceReference</code> object may be shared between plugins and
  35. * can be used to examine the properties of the service and to get the service
  36. * object.
  37. * <p>
  38. * Every service registered in the Framework has a unique
  39. * <code>ctkServiceRegistration</code> object and may have multiple, distinct
  40. * <code>ctkServiceReference</code> objects referring to it.
  41. * <code>ctkServiceReference</code> objects associated with a
  42. * <code>ctkServiceRegistration</code> are considered equal
  43. * (more specifically, their <code>operator==()</code>
  44. * method will return <code>true</code> when compared).
  45. * <p>
  46. * If the same service object is registered multiple times,
  47. * <code>ctkServiceReference</code> objects associated with different
  48. * <code>ctkServiceRegistration</code> objects are not equal.
  49. *
  50. * @see ctkPluginContext::getServiceReference
  51. * @see ctkPluginContext::getServiceReferences
  52. * @see ctkPluginContext::getService
  53. * @remarks This class is thread safe.
  54. */
  55. class CTK_PLUGINFW_EXPORT ctkServiceReference {
  56. public:
  57. /**
  58. * Creates an invalid ctkServiceReference object. You can use
  59. * this object in boolean expressions and it will evaluate to
  60. * <code>false</code>.
  61. */
  62. ctkServiceReference();
  63. ctkServiceReference(const ctkServiceReference& ref);
  64. /**
  65. * Converts this ctkServiceReference instance into a boolean
  66. * expression. If this instance was default constructed or
  67. * the service it references has been unregistered, the conversion
  68. * returns <code>false</code>, otherwise it returns <code>true</code>.
  69. */
  70. operator bool() const;
  71. /**
  72. * Releases any resources held or locked by this
  73. * <code>ctkServiceReference</code> and renders it invalid.
  74. */
  75. ctkServiceReference& operator=(int null);
  76. ~ctkServiceReference();
  77. /**
  78. * Returns the property value to which the specified property key is mapped
  79. * in the properties <code>ctkDictionary</code> object of the service
  80. * referenced by this <code>ctkServiceReference</code> object.
  81. *
  82. * <p>
  83. * Property keys are case-insensitive.
  84. *
  85. * <p>
  86. * This method must continue to return property values after the service has
  87. * been unregistered. This is so references to unregistered services can
  88. * still be interrogated.
  89. *
  90. * @param key The property key.
  91. * @return The property value to which the key is mapped; an invalid QVariant
  92. * if there is no property named after the key.
  93. */
  94. QVariant getProperty(const QString& key) const;
  95. /**
  96. * Returns a list of the keys in the <code>ctkDictionary</code>
  97. * object of the service referenced by this <code>ctkServiceReference</code>
  98. * object.
  99. *
  100. * <p>
  101. * This method will continue to return the keys after the service has been
  102. * unregistered. This is so references to unregistered services can
  103. * still be interrogated.
  104. *
  105. * <p>
  106. * This method is not <i>case-preserving</i>; this means that every key in the
  107. * returned array is in lower case, which may not be the case for the corresponding key in the
  108. * properties <code>ctkDictionary</code> that was passed to the
  109. * {@link ctkPluginContext::registerService(const QStringList&, QObject*, const ctkDictionary&)} or
  110. * {@link ctkServiceRegistration::setProperties} methods.
  111. *
  112. * @return A list of property keys.
  113. */
  114. QStringList getPropertyKeys() const;
  115. /**
  116. * Returns the plugin that registered the service referenced by this
  117. * <code>ctkServiceReference</code> object.
  118. *
  119. * <p>
  120. * This method must return <code>0</code> when the service has been
  121. * unregistered. This can be used to determine if the service has been
  122. * unregistered.
  123. *
  124. * @return The plugin that registered the service referenced by this
  125. * <code>ctkServiceReference</code> object; <code>0</code> if that
  126. * service has already been unregistered.
  127. * @see ctkPluginContext::registerService(const QStringList&, QObject* , const ctkDictionary&)
  128. */
  129. QSharedPointer<ctkPlugin> getPlugin() const;
  130. /**
  131. * Returns the plugins that are using the service referenced by this
  132. * <code>ctkServiceReference</code> object. Specifically, this method returns
  133. * the plugins whose usage count for that service is greater than zero.
  134. *
  135. * @return A list of plugins whose usage count for the service referenced
  136. * by this <code>ctkServiceReference</code> object is greater than
  137. * zero.
  138. */
  139. QList<QSharedPointer<ctkPlugin> > getUsingPlugins() const;
  140. /**
  141. * Compares this <code>ctkServiceReference</code> with the specified
  142. * <code>ctkServiceReference</code> for order.
  143. *
  144. * <p>
  145. * If this <code>ctkServiceReference</code> and the specified
  146. * <code>ctkServiceReference</code> have the same {@link ctkPluginConstants::SERVICE_ID
  147. * service id} they are equal. This <code>ctkServiceReference</code> is less
  148. * than the specified <code>ctkServiceReference</code> if it has a lower
  149. * {@link ctkPluginConstants::SERVICE_RANKING service ranking} and greater if it has a
  150. * higher service ranking. Otherwise, if this <code>ctkServiceReference</code>
  151. * and the specified <code>ctkServiceReference</code> have the same
  152. * {@link ctkPluginConstants::SERVICE_RANKING service ranking}, this
  153. * <code>ctkServiceReference</code> is less than the specified
  154. * <code>ctkServiceReference</code> if it has a higher
  155. * {@link ctkPluginConstants::SERVICE_ID service id} and greater if it has a lower
  156. * service id.
  157. *
  158. * @param reference The <code>ctkServiceReference</code> to be compared.
  159. * @return Returns a false or true if this
  160. * <code>ctkServiceReference</code> is less than or greater
  161. * than the specified <code>ctkServiceReference</code>.
  162. * @throws ctkInvalidArgumentException If the specified
  163. * <code>ctkServiceReference</code> was not created by the same
  164. * framework instance as this <code>ctkServiceReference</code>.
  165. */
  166. bool operator<(const ctkServiceReference& reference) const;
  167. bool operator==(const ctkServiceReference& reference) const;
  168. ctkServiceReference& operator=(const ctkServiceReference& reference);
  169. protected:
  170. friend class ctkLDAPSearchFilter;
  171. friend class ctkServiceRegistrationPrivate;
  172. friend class ctkPluginContext;
  173. friend class ctkPluginPrivate;
  174. friend class ctkPluginFrameworkListeners;
  175. template<class S, class T> friend class ctkServiceTracker;
  176. template<class S, class T> friend class ctkServiceTrackerPrivate;
  177. template<class S, class R, class T> friend class ctkPluginAbstractTracked;
  178. friend uint CTK_PLUGINFW_EXPORT qHash(const ctkServiceReference&);
  179. ctkServiceReference(ctkServiceRegistrationPrivate* reg);
  180. ctkServiceReferencePrivate * d_ptr;
  181. private:
  182. Q_DECLARE_PRIVATE(ctkServiceReference)
  183. };
  184. /**
  185. * \ingroup PluginFramework
  186. * @{
  187. */
  188. uint CTK_PLUGINFW_EXPORT qHash(const ctkServiceReference& serviceRef);
  189. QDebug CTK_PLUGINFW_EXPORT operator<<(QDebug dbg, const ctkServiceReference& serviceRef);
  190. /** @}*/
  191. Q_DECLARE_METATYPE(ctkServiceReference)
  192. #endif // CTKSERVICEREFERENCE_H