ctkServiceReference.cpp 4.4 KB

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