ctkEvent.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. ctkEvent::ctkEvent()
  57. : d(0)
  58. {
  59. }
  60. ctkEvent::ctkEvent(const QString& topic, const ctkDictionary& properties)
  61. : d(new ctkEventData(topic, properties))
  62. {
  63. }
  64. /*
  65. * This is fast thanks to implicit sharing
  66. */
  67. ctkEvent::ctkEvent(const ctkEvent &event)
  68. : d(event.d)
  69. {
  70. d->ref.ref();
  71. }
  72. ctkEvent::~ctkEvent()
  73. {
  74. }
  75. bool ctkEvent::isNull() const
  76. {
  77. return !d;
  78. }
  79. ctkEvent& ctkEvent::operator=(const ctkEvent& other)
  80. {
  81. d = other.d;
  82. return *this;
  83. }
  84. bool ctkEvent::operator==(const ctkEvent& other) const
  85. {
  86. if (d == other.d)
  87. return true;
  88. if (d->topic == other.d->topic &&
  89. d->properties == other.d->properties)
  90. return true;
  91. return false;
  92. }
  93. QVariant ctkEvent::getProperty(const QString& name) const
  94. {
  95. return d->properties[name];
  96. }
  97. bool ctkEvent::containsProperty(const QString& name) const
  98. {
  99. if (ctkEventConstants::EVENT_TOPIC == name)
  100. {
  101. return true;
  102. }
  103. return d->properties.contains(name);
  104. }
  105. QStringList ctkEvent::getPropertyNames() const
  106. {
  107. QStringList result;
  108. foreach (ctkCaseInsensitiveString key, d->properties.keys())
  109. {
  110. result << key;
  111. }
  112. return result;
  113. }
  114. const QString& ctkEvent::getTopic() const
  115. {
  116. return d->topic;
  117. }
  118. bool ctkEvent::matches(const ctkLDAPSearchFilter& filter) const
  119. {
  120. return filter.matchCase(d->properties);
  121. }