ctkServiceReference.h 7.9 KB

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