ctkEvent.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "ctkEvent.h"
  16. #include "ctkEventConstants.h"
  17. #include <stdexcept>
  18. class ctkEventData : public QSharedData
  19. {
  20. public:
  21. ctkEventData(const QString& topic, const ctkDictionary& properties)
  22. : topic(topic), properties(properties)
  23. {
  24. validateTopicName(topic);
  25. this->properties.insert(ctkEventConstants::EVENT_TOPIC, topic);
  26. }
  27. static void validateTopicName(const QString& topic)
  28. {
  29. if (topic.isEmpty())
  30. {
  31. throw std::invalid_argument("empty topic");
  32. }
  33. // Can't start or end with a '/' but anywhere else is okay
  34. // Can't have "//" as that implies empty token
  35. if (topic.startsWith("/") || topic.endsWith("/") ||
  36. topic.contains("//"))
  37. {
  38. throw std::invalid_argument(QString("invalid topic: %1").arg(topic).toStdString());
  39. }
  40. QString::const_iterator topicEnd = topic.end();
  41. QChar A('A'), Z('Z'), a('a'), z('z'), zero('0'), nine('9');
  42. QChar dash('-'), slash('/'), underscore('_');
  43. for (QString::const_iterator i = topic.begin(); i < topicEnd; ++i)
  44. {
  45. QChar c(*i);
  46. if ((A <= c) && (c <= Z)) continue;
  47. if ((a <= c) && (c <= z)) continue;
  48. if ((zero <= c) && (c <= nine)) continue;
  49. if ((c == underscore) || (c == dash) || (c == slash)) continue;
  50. throw std::invalid_argument(QString("invalid topic: %1").arg(topic).toStdString());
  51. }
  52. }
  53. const QString topic;
  54. ctkDictionary properties;
  55. };
  56. //----------------------------------------------------------------------------
  57. ctkEvent::ctkEvent()
  58. : d(0)
  59. {
  60. }
  61. //----------------------------------------------------------------------------
  62. ctkEvent::ctkEvent(const QString& topic, const ctkDictionary& properties)
  63. : d(new ctkEventData(topic, properties))
  64. {
  65. }
  66. //----------------------------------------------------------------------------
  67. // This is fast thanks to implicit sharing
  68. ctkEvent::ctkEvent(const ctkEvent &event)
  69. : d(event.d)
  70. {
  71. }
  72. //----------------------------------------------------------------------------
  73. ctkEvent::~ctkEvent()
  74. {
  75. }
  76. //----------------------------------------------------------------------------
  77. bool ctkEvent::isNull() const
  78. {
  79. return !d;
  80. }
  81. //----------------------------------------------------------------------------
  82. ctkEvent& ctkEvent::operator=(const ctkEvent& other)
  83. {
  84. d = other.d;
  85. return *this;
  86. }
  87. //----------------------------------------------------------------------------
  88. bool ctkEvent::operator==(const ctkEvent& other) const
  89. {
  90. if (d == other.d)
  91. return true;
  92. if (d->topic == other.d->topic &&
  93. d->properties == other.d->properties)
  94. return true;
  95. return false;
  96. }
  97. //----------------------------------------------------------------------------
  98. QVariant ctkEvent::getProperty(const QString& name) const
  99. {
  100. return d->properties[name];
  101. }
  102. //----------------------------------------------------------------------------
  103. bool ctkEvent::containsProperty(const QString& name) const
  104. {
  105. if (ctkEventConstants::EVENT_TOPIC == name)
  106. {
  107. return true;
  108. }
  109. return d->properties.contains(name);
  110. }
  111. //----------------------------------------------------------------------------
  112. QStringList ctkEvent::getPropertyNames() const
  113. {
  114. QStringList result;
  115. foreach (ctkCaseInsensitiveString key, d->properties.keys())
  116. {
  117. result << key;
  118. }
  119. return result;
  120. }
  121. //----------------------------------------------------------------------------
  122. const QString& ctkEvent::getTopic() const
  123. {
  124. return d->topic;
  125. }
  126. //----------------------------------------------------------------------------
  127. bool ctkEvent::matches(const ctkLDAPSearchFilter& filter) const
  128. {
  129. return filter.matchCase(d->properties);
  130. }