ctkServiceReference.cpp 3.2 KB

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