ctkServiceReference.cpp 4.1 KB

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