ctkServiceSlotEntry.cpp 4.0 KB

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