ctkServiceRegistration.cpp 6.3 KB

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