ctkServiceSlotEntry.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "ctkServiceSlotEntry_p.h"
  16. #include "ctkLDAPExpr_p.h"
  17. #include "ctkPlugin.h"
  18. #include "ctkException.h"
  19. #include <QSharedData>
  20. #include <cstring>
  21. class ctkServiceSlotEntryData : public QSharedData
  22. {
  23. public:
  24. ctkServiceSlotEntryData(QSharedPointer<ctkPlugin> p, QObject* receiver,
  25. const char* slot)
  26. : plugin(p), receiver(receiver),
  27. slot(slot), removed(false),
  28. hashValue(0)
  29. {
  30. }
  31. /**
  32. * The elements of "simple" filters are cached, for easy lookup.
  33. *
  34. * The grammar for simple filters is as follows:
  35. *
  36. * <pre>
  37. * Simple = '(' attr '=' value ')'
  38. * | '(' '|' Simple+ ')'
  39. * </pre>
  40. * where <code>attr</code> is one of {@link Constants#OBJECTCLASS},
  41. * {@link Constants#SERVICE_ID} or {@link Constants#SERVICE_PID}, and
  42. * <code>value</code> must not contain a wildcard character.
  43. * <p>
  44. * The index of the vector determines which key the cache is for
  45. * (see {@link ServiceListenerState#hashedKeys}). For each key, there is
  46. * a vector pointing out the values which are accepted by this
  47. * ServiceListenerEntry's filter. This cache is maintained to make
  48. * it easy to remove this service listener.
  49. */
  50. ctkLDAPExpr::LocalCache local_cache;
  51. ctkLDAPExpr ldap;
  52. QSharedPointer<ctkPlugin> plugin;
  53. QObject* receiver;
  54. const char* slot;
  55. bool removed;
  56. uint hashValue;
  57. };
  58. //----------------------------------------------------------------------------
  59. ctkServiceSlotEntry::ctkServiceSlotEntry(
  60. QSharedPointer<ctkPlugin> p, QObject* receiver, const char* slot, const QString& filter)
  61. : d(new ctkServiceSlotEntryData(p, receiver, slot))
  62. {
  63. if (!filter.isNull())
  64. {
  65. d->ldap = ctkLDAPExpr(filter);
  66. }
  67. }
  68. //----------------------------------------------------------------------------
  69. ctkServiceSlotEntry::ctkServiceSlotEntry(const ctkServiceSlotEntry& other)
  70. : d(other.d)
  71. {
  72. }
  73. //----------------------------------------------------------------------------
  74. ctkServiceSlotEntry::ctkServiceSlotEntry()
  75. : d(new ctkServiceSlotEntryData(QSharedPointer<ctkPlugin>(0), 0, 0))
  76. {
  77. }
  78. //----------------------------------------------------------------------------
  79. ctkServiceSlotEntry& ctkServiceSlotEntry::operator=(const ctkServiceSlotEntry& other)
  80. {
  81. d = other.d;
  82. return *this;
  83. }
  84. //----------------------------------------------------------------------------
  85. ctkServiceSlotEntry::~ctkServiceSlotEntry()
  86. {
  87. }
  88. //----------------------------------------------------------------------------
  89. bool ctkServiceSlotEntry::operator==(const ctkServiceSlotEntry& other) const
  90. {
  91. return ((d->plugin == 0 || other.d->plugin == 0) || d->plugin == other.d->plugin) &&
  92. d->receiver == other.d->receiver &&
  93. ((d->slot == 0 || other.d->slot == 0) || std::strcmp(d->slot, other.d->slot) == 0);
  94. }
  95. //----------------------------------------------------------------------------
  96. void ctkServiceSlotEntry::invokeSlot(const ctkServiceEvent &event)
  97. {
  98. if (!QMetaObject::invokeMethod(d->receiver, d->slot,
  99. Qt::DirectConnection,
  100. Q_ARG(ctkServiceEvent, event)))
  101. {
  102. throw ctkRuntimeException(
  103. QString("Slot %1 of %2 could not be invoked. A call to "
  104. "ctkPluginContext::connectServiceListener() must only contain "
  105. "the slot name, not the whole signature.").
  106. arg(d->slot).arg(d->receiver->metaObject()->className()));
  107. }
  108. }
  109. //----------------------------------------------------------------------------
  110. void ctkServiceSlotEntry::setRemoved(bool removed)
  111. {
  112. d->removed = removed;
  113. }
  114. //----------------------------------------------------------------------------
  115. bool ctkServiceSlotEntry::isRemoved() const
  116. {
  117. return d->removed;
  118. }
  119. //----------------------------------------------------------------------------
  120. QSharedPointer<ctkPlugin> ctkServiceSlotEntry::getPlugin() const
  121. {
  122. return d->plugin;
  123. }
  124. //----------------------------------------------------------------------------
  125. ctkLDAPExpr ctkServiceSlotEntry::getLDAPExpr() const
  126. {
  127. return d->ldap;
  128. }
  129. //----------------------------------------------------------------------------
  130. QString ctkServiceSlotEntry::getFilter() const
  131. {
  132. return d->ldap.isNull() ? QString() : d->ldap.toString();
  133. }
  134. //----------------------------------------------------------------------------
  135. ctkLDAPExpr::LocalCache& ctkServiceSlotEntry::getLocalCache() const
  136. {
  137. return d->local_cache;
  138. }
  139. //----------------------------------------------------------------------------
  140. uint qHash(const ctkServiceSlotEntry& serviceSlot)
  141. {
  142. if (serviceSlot.d->hashValue == 0)
  143. {
  144. serviceSlot.d->hashValue = qHash(serviceSlot.d->plugin) * 4 +
  145. qHash(serviceSlot.d->receiver) * 2 + qHash(serviceSlot.d->slot);
  146. }
  147. return serviceSlot.d->hashValue;
  148. }