ctkEvent.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 ctkEventPrivate {
  19. public:
  20. ctkEventPrivate(const QString& topic, const ctkDictionary& properties)
  21. : ref(1), topic(topic), properties(properties)
  22. {
  23. validateTopicName(topic);
  24. this->properties.insert(EventConstants::EVENT_TOPIC, topic);
  25. }
  26. static void validateTopicName(const QString& topic)
  27. {
  28. if (topic.isEmpty())
  29. {
  30. throw std::invalid_argument("empty topic");
  31. }
  32. // Can't start or end with a '/' but anywhere else is okay
  33. // Can't have "//" as that implies empty token
  34. if (topic.startsWith("/") || topic.endsWith("/") ||
  35. topic.contains("//"))
  36. {
  37. throw std::invalid_argument(QString("invalid topic: %1").arg(topic).toStdString());
  38. }
  39. QString::const_iterator topicEnd = topic.end();
  40. QChar A('A'), Z('Z'), a('a'), z('z'), zero('0'), nine('9');
  41. QChar dash('-'), slash('/'), underscore('_');
  42. for (QString::const_iterator i = topic.begin(); i < topicEnd; ++i)
  43. {
  44. QChar c(*i);
  45. if ((A <= c) && (c <= Z)) continue;
  46. if ((a <= c) && (c <= z)) continue;
  47. if ((zero <= c) && (c <= nine)) continue;
  48. if ((c == underscore) || (c == dash) || (c == slash)) continue;
  49. throw std::invalid_argument(QString("invalid topic: %1").arg(topic).toStdString());
  50. }
  51. }
  52. QAtomicInt ref;
  53. const QString topic;
  54. ctkDictionary properties;
  55. };
  56. ctkEvent::ctkEvent(const QString& topic, const ctkDictionary& properties)
  57. : d(new ctkEventPrivate(topic, properties))
  58. {
  59. }
  60. /*
  61. * This is fast thanks to implicit sharing
  62. */
  63. ctkEvent::ctkEvent(const ctkEvent &event)
  64. : d(event.d)
  65. {
  66. d->ref.ref();
  67. }
  68. ctkEvent::~ctkEvent()
  69. {
  70. if (!d->ref.deref())
  71. delete d;
  72. }
  73. bool ctkEvent::operator==(const ctkEvent& other) const
  74. {
  75. if (d == other.d)
  76. return true;
  77. if (d->topic == other.d->topic &&
  78. d->properties == other.d->properties)
  79. return true;
  80. return false;
  81. }
  82. QVariant ctkEvent::property(const QString& name) const
  83. {
  84. return d->properties[name];
  85. }
  86. QStringList ctkEvent::propertyNames() const
  87. {
  88. return d->properties.keys();
  89. }
  90. const QString& ctkEvent::topic() const
  91. {
  92. return d->topic;
  93. }
  94. bool ctkEvent::matches(const ctkLDAPSearchFilter& filter) const
  95. {
  96. return filter.matchCase(d->properties);
  97. }