ctkServiceRegistration.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "ctkServiceRegistration.h"
  16. #include "ctkServiceRegistrationPrivate.h"
  17. #include "ctkPluginFrameworkContext_p.h"
  18. #include "ctkPluginPrivate_p.h"
  19. #include "ctkPluginConstants.h"
  20. #include "ctkServices_p.h"
  21. #include "ctkServiceFactory.h"
  22. #include "ctkServiceSlotEntry_p.h"
  23. #include <QMutex>
  24. #include <stdexcept>
  25. ctkServiceRegistration::ctkServiceRegistration()
  26. : d_ptr(0)
  27. {
  28. }
  29. ctkServiceRegistration::ctkServiceRegistration(const ctkServiceRegistration& reg)
  30. : d_ptr(reg.d_ptr)
  31. {
  32. d_func()->ref.ref();
  33. }
  34. ctkServiceRegistration::ctkServiceRegistration(ctkPluginPrivate* plugin, QObject* service,
  35. const ServiceProperties& props)
  36. : d_ptr(new ctkServiceRegistrationPrivate(this, plugin, service, props))
  37. {
  38. }
  39. ctkServiceRegistration::operator bool() const
  40. {
  41. return d_func();
  42. }
  43. ctkServiceRegistration::~ctkServiceRegistration()
  44. {
  45. if (d_func() && !d_func()->ref.deref())
  46. delete d_ptr;
  47. }
  48. ctkServiceReference ctkServiceRegistration::getReference() const
  49. {
  50. Q_D(const ctkServiceRegistration);
  51. if (!d) throw std::logic_error("ctkServiceRegistration object invalid");
  52. if (!d->available) throw std::logic_error("Service is unregistered");
  53. return d->reference;
  54. }
  55. void ctkServiceRegistration::setProperties(const ServiceProperties& props)
  56. {
  57. Q_D(ctkServiceRegistration);
  58. if (!d) throw std::logic_error("ctkServiceRegistration object invalid");
  59. QMutexLocker lock(&d->eventLock);
  60. QSet<ctkServiceSlotEntry> before;
  61. // TBD, optimize the locking of services
  62. {
  63. QMutexLocker lock2(&d->plugin->fwCtx->globalFwLock);
  64. QMutexLocker lock3(&d->propsLock);
  65. if (d->available)
  66. {
  67. // NYI! Optimize the MODIFIED_ENDMATCH code
  68. int old_rank = d->properties.value(ctkPluginConstants::SERVICE_RANKING).toInt();
  69. before = d->plugin->fwCtx->listeners.getMatchingServiceSlots(d->reference);
  70. QStringList classes = d->properties.value(ctkPluginConstants::OBJECTCLASS).toStringList();
  71. qlonglong sid = d->properties.value(ctkPluginConstants::SERVICE_ID).toLongLong();
  72. d->properties = ctkServices::createServiceProperties(props, classes, sid);
  73. int new_rank = d->properties.value(ctkPluginConstants::SERVICE_RANKING).toInt();
  74. if (old_rank != new_rank)
  75. {
  76. d->plugin->fwCtx->services->updateServiceRegistrationOrder(*this, classes);
  77. }
  78. }
  79. else
  80. {
  81. throw std::logic_error("Service is unregistered");
  82. }
  83. }
  84. d->plugin->fwCtx->listeners.serviceChanged(
  85. d->plugin->fwCtx->listeners.getMatchingServiceSlots(d->reference),
  86. ctkServiceEvent(ctkServiceEvent::MODIFIED, d->reference), before);
  87. d->plugin->fwCtx->listeners.serviceChanged(
  88. before,
  89. ctkServiceEvent(ctkServiceEvent::MODIFIED_ENDMATCH, d->reference));
  90. }
  91. void ctkServiceRegistration::unregister()
  92. {
  93. Q_D(ctkServiceRegistration);
  94. if (!d) throw std::logic_error("ctkServiceRegistration object invalid");
  95. if (d->unregistering) return; // Silently ignore redundant unregistration.
  96. {
  97. QMutexLocker lock(&d->eventLock);
  98. if (d->unregistering) return;
  99. d->unregistering = true;
  100. if (d->available)
  101. {
  102. if (d->plugin)
  103. {
  104. d->plugin->fwCtx->services->removeServiceRegistration(*this);
  105. }
  106. }
  107. else
  108. {
  109. throw std::logic_error("Service is unregistered");
  110. }
  111. }
  112. if (d->plugin)
  113. {
  114. d->plugin->fwCtx->listeners.serviceChanged(
  115. d->plugin->fwCtx->listeners.getMatchingServiceSlots(d->reference),
  116. ctkServiceEvent(ctkServiceEvent::UNREGISTERING, d->reference));
  117. }
  118. {
  119. QMutexLocker lock(&d->eventLock);
  120. {
  121. QMutexLocker lock2(&d->propsLock);
  122. d->available = false;
  123. if (d->plugin)
  124. {
  125. for (QHashIterator<QSharedPointer<ctkPlugin>, QObject*> i(d->serviceInstances); i.hasNext();)
  126. {
  127. QObject* obj = i.next().value();
  128. try
  129. {
  130. // NYI, don't call inside lock
  131. qobject_cast<ctkServiceFactory*>(d->service)->ungetService(i.key(),
  132. *this,
  133. obj);
  134. }
  135. catch (const std::exception& ue)
  136. {
  137. ctkPluginFrameworkEvent pfwEvent(ctkPluginFrameworkEvent::ERROR, d->plugin->q_func().data(), ue);
  138. d->plugin->fwCtx->listeners
  139. .emitFrameworkEvent(pfwEvent);
  140. }
  141. }
  142. }
  143. d->plugin = 0;
  144. d->dependents.clear();
  145. d->service = 0;
  146. d->serviceInstances.clear();;
  147. d->unregistering = false;
  148. }
  149. }
  150. }
  151. bool ctkServiceRegistration::operator<(const ctkServiceRegistration& o) const
  152. {
  153. Q_D(const ctkServiceRegistration);
  154. if (!d) return true;
  155. return d->reference <(o.d_func()->reference);
  156. }
  157. bool ctkServiceRegistration::operator==(const ctkServiceRegistration& registration) const
  158. {
  159. Q_D(const ctkServiceRegistration);
  160. return d == registration.d_func();
  161. }
  162. ctkServiceRegistration& ctkServiceRegistration::operator=(const ctkServiceRegistration& registration)
  163. {
  164. ctkServiceRegistrationPrivate* curr_d = d_func();
  165. d_ptr = registration.d_ptr;
  166. d_ptr->ref.ref();
  167. if (curr_d && !curr_d->ref.deref())
  168. delete curr_d;
  169. return *this;
  170. }
  171. uint qHash(const ctkServiceRegistration& serviceReg)
  172. {
  173. return qHash(serviceReg.d_func());
  174. }