ctkServiceRegistration.cpp 6.2 KB

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