ctkServiceSlotEntry.cpp 4.2 KB

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