ctkServiceReference.cpp 3.4 KB

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