ctkServiceReference.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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. namespace ctk {
  23. ServiceReference::ServiceReference(ServiceRegistrationPrivate* reg)
  24. : d_ptr(new ServiceReferencePrivate(reg))
  25. {
  26. }
  27. ServiceReference::~ServiceReference()
  28. {
  29. delete d_ptr;
  30. }
  31. QVariant ServiceReference::getProperty(const QString& key) const
  32. {
  33. Q_D(const ServiceReference);
  34. QMutexLocker lock(&d->registration->propsLock);
  35. return d->registration->properties.value(key);
  36. }
  37. QStringList ServiceReference::getPropertyKeys() const
  38. {
  39. Q_D(const ServiceReference);
  40. QMutexLocker lock(&d->registration->propsLock);
  41. return d->registration->properties.keys();
  42. }
  43. Plugin* ServiceReference::getPlugin() const
  44. {
  45. return d_func()->registration->plugin->q_func();
  46. }
  47. QList<Plugin*> ServiceReference::getUsingPlugins() const
  48. {
  49. Q_D(const ServiceReference);
  50. QMutexLocker lock(&d->registration->propsLock);
  51. if (d->registration->reference != 0)
  52. {
  53. return d->registration->dependents.keys();
  54. }
  55. else
  56. {
  57. return QList<Plugin*>();
  58. }
  59. }
  60. bool ServiceReference::operator<(const ServiceReference& reference) const
  61. {
  62. bool sameFw = d_func()->registration->plugin->fwCtx == reference.d_func()->registration->plugin->fwCtx;
  63. if (!sameFw)
  64. {
  65. throw std::invalid_argument("Can not compare service references "
  66. "belonging to different framework "
  67. "instances.");
  68. }
  69. int r1 = getProperty(PluginConstants::SERVICE_RANKING).toInt();
  70. int r2 = reference.getProperty(PluginConstants::SERVICE_RANKING).toInt();
  71. if (r1 != r2)
  72. {
  73. // use ranking if ranking differs
  74. return r1 < r2 ? false : true;
  75. }
  76. else
  77. {
  78. qlonglong id1 = getProperty(PluginConstants::SERVICE_ID).toLongLong();
  79. qlonglong id2 = reference.getProperty(PluginConstants::SERVICE_ID).toLongLong();
  80. // otherwise compare using IDs,
  81. // is less than if it has a higher ID.
  82. return id2< id1;
  83. }
  84. }
  85. bool ServiceReference::operator==(const ServiceReference& reference) const
  86. {
  87. return d_func()->registration == reference.d_func()->registration;
  88. }
  89. }