ctkServiceReference.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #include <QStringList>
  16. #include <QMutexLocker>
  17. #include <QDebug>
  18. #include "ctkPlugin_p.h"
  19. #include "ctkPluginConstants.h"
  20. #include "ctkServiceReference.h"
  21. #include "ctkServiceReference_p.h"
  22. #include "ctkServiceRegistration_p.h"
  23. //----------------------------------------------------------------------------
  24. ctkServiceReference::ctkServiceReference()
  25. : d_ptr(new ctkServiceReferencePrivate(0))
  26. {
  27. }
  28. //----------------------------------------------------------------------------
  29. ctkServiceReference::ctkServiceReference(const ctkServiceReference& ref)
  30. : d_ptr(ref.d_ptr)
  31. {
  32. d_func()->ref.ref();
  33. }
  34. //----------------------------------------------------------------------------
  35. ctkServiceReference::ctkServiceReference(ctkServiceRegistrationPrivate* reg)
  36. : d_ptr(new ctkServiceReferencePrivate(reg))
  37. {
  38. }
  39. //----------------------------------------------------------------------------
  40. ctkServiceReference::operator bool() const
  41. {
  42. return getPlugin();
  43. }
  44. //----------------------------------------------------------------------------
  45. ctkServiceReference& ctkServiceReference::operator=(int null)
  46. {
  47. if (null == 0)
  48. {
  49. if (!d_func()->ref.deref())
  50. delete d_ptr;
  51. d_ptr = new ctkServiceReferencePrivate(0);
  52. }
  53. return *this;
  54. }
  55. //----------------------------------------------------------------------------
  56. ctkServiceReference::~ctkServiceReference()
  57. {
  58. if (!d_func()->ref.deref())
  59. delete d_ptr;
  60. }
  61. //----------------------------------------------------------------------------
  62. QVariant ctkServiceReference::getProperty(const QString& key) const
  63. {
  64. Q_D(const ctkServiceReference);
  65. QMutexLocker lock(&d->registration->propsLock);
  66. return d->registration->properties.value(key);
  67. }
  68. //----------------------------------------------------------------------------
  69. QStringList ctkServiceReference::getPropertyKeys() const
  70. {
  71. Q_D(const ctkServiceReference);
  72. QMutexLocker lock(&d->registration->propsLock);
  73. QStringList result;
  74. foreach (ctkCaseInsensitiveString key, d->registration->properties.keys())
  75. {
  76. result << key;
  77. }
  78. return result;
  79. }
  80. //----------------------------------------------------------------------------
  81. QSharedPointer<ctkPlugin> ctkServiceReference::getPlugin() const
  82. {
  83. if (d_func()->registration == 0 || d_func()->registration->plugin == 0)
  84. {
  85. return QSharedPointer<ctkPlugin>();
  86. }
  87. return d_func()->registration->plugin->q_func().toStrongRef();
  88. }
  89. //----------------------------------------------------------------------------
  90. QList<QSharedPointer<ctkPlugin> > ctkServiceReference::getUsingPlugins() const
  91. {
  92. Q_D(const ctkServiceReference);
  93. QMutexLocker lock(&d->registration->propsLock);
  94. return d->registration->dependents.keys();
  95. }
  96. //----------------------------------------------------------------------------
  97. bool ctkServiceReference::operator<(const ctkServiceReference& reference) const
  98. {
  99. bool sameFw = d_func()->registration->plugin->fwCtx == reference.d_func()->registration->plugin->fwCtx;
  100. if (!sameFw)
  101. {
  102. throw ctkInvalidArgumentException("Can not compare service references "
  103. "belonging to different framework "
  104. "instances.");
  105. }
  106. int r1 = getProperty(ctkPluginConstants::SERVICE_RANKING).toInt();
  107. int r2 = reference.getProperty(ctkPluginConstants::SERVICE_RANKING).toInt();
  108. if (r1 != r2)
  109. {
  110. // use ranking if ranking differs
  111. return r1 < r2;
  112. }
  113. else
  114. {
  115. qlonglong id1 = getProperty(ctkPluginConstants::SERVICE_ID).toLongLong();
  116. qlonglong id2 = reference.getProperty(ctkPluginConstants::SERVICE_ID).toLongLong();
  117. // otherwise compare using IDs,
  118. // is less than if it has a higher ID.
  119. return id2< id1;
  120. }
  121. }
  122. //----------------------------------------------------------------------------
  123. bool ctkServiceReference::operator==(const ctkServiceReference& reference) const
  124. {
  125. return d_func()->registration == reference.d_func()->registration;
  126. }
  127. //----------------------------------------------------------------------------
  128. ctkServiceReference& ctkServiceReference::operator=(const ctkServiceReference& reference)
  129. {
  130. ctkServiceReferencePrivate* curr_d = d_func();
  131. d_ptr = reference.d_ptr;
  132. d_ptr->ref.ref();
  133. if (!curr_d->ref.deref())
  134. delete curr_d;
  135. return *this;
  136. }
  137. //----------------------------------------------------------------------------
  138. uint qHash(const ctkServiceReference& serviceRef)
  139. {
  140. return qHash(serviceRef.d_func()->registration);
  141. }
  142. //----------------------------------------------------------------------------
  143. QDebug operator<<(QDebug dbg, const ctkServiceReference& serviceRef)
  144. {
  145. dbg.nospace() << "Reference for service object registered from "
  146. << serviceRef.getPlugin()->getSymbolicName() << " " << serviceRef.getPlugin()->getVersion()
  147. << " (";
  148. int i = serviceRef.getPropertyKeys().size();
  149. foreach(QString key, serviceRef.getPropertyKeys())
  150. {
  151. dbg.nospace() << key << "=" << serviceRef.getProperty(key).toString();
  152. if (--i > 0) dbg.nospace() << ",";
  153. }
  154. dbg.nospace() << ")";
  155. return dbg.maybeSpace();
  156. }