ctkBusEvent.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "ctkBusEvent.h"
  16. #include "ctkEventDefinitions.h"
  17. #include <stdexcept>
  18. class ctkBusEventData : public QSharedData
  19. {
  20. public:
  21. ctkBusEventData(const QString& topic, const ctkDictionary& properties)
  22. : topic(topic), properties(properties)
  23. {
  24. this->properties.insert(TOPIC, topic);
  25. }
  26. ctkBusEventData(QString topic, int event_type, int signature_type, QObject *objectPointer, QString signature)
  27. : topic(topic)
  28. {
  29. properties.insert(TOPIC, topic);
  30. properties.insert(TYPE, static_cast<int>(event_type));
  31. properties.insert(SIGTYPE, static_cast<int>(signature_type));
  32. QVariant var;
  33. var.setValue(objectPointer);
  34. properties.insert(OBJECT, var);
  35. properties.insert(SIGNATURE, signature);
  36. }
  37. static void validateTopicName(const QString& topic)
  38. {
  39. if (topic.isEmpty())
  40. {
  41. throw std::invalid_argument("empty topic");
  42. }
  43. // Can't start or end with a '/' but anywhere else is okay
  44. // Can't have "//" as that implies empty token
  45. if (topic.startsWith("/") || topic.endsWith("/") ||
  46. topic.contains("//"))
  47. {
  48. throw std::invalid_argument(QString("invalid topic: %1").arg(topic).toStdString());
  49. }
  50. QString::const_iterator topicEnd = topic.end();
  51. QChar A('A'), Z('Z'), a('a'), z('z'), zero('0'), nine('9');
  52. QChar dash('-'), slash('/'), underscore('_');
  53. for (QString::const_iterator i = topic.begin(); i < topicEnd; ++i)
  54. {
  55. QChar c(*i);
  56. if ((A <= c) && (c <= Z)) continue;
  57. if ((a <= c) && (c <= z)) continue;
  58. if ((zero <= c) && (c <= nine)) continue;
  59. if ((c == underscore) || (c == dash) || (c == slash)) continue;
  60. throw std::invalid_argument(QString("invalid topic: %1").arg(topic).toStdString());
  61. }
  62. }
  63. const QString topic;
  64. ctkDictionary properties;
  65. };
  66. ctkBusEvent::ctkBusEvent()
  67. : d(0)
  68. {
  69. }
  70. ctkBusEvent::ctkBusEvent(const QString& topic, const ctkDictionary& properties)
  71. : d(new ctkBusEventData(topic, properties))
  72. {
  73. }
  74. ctkBusEvent::ctkBusEvent(QString topic, int event_type, int signature_type, QObject *objectPointer, QString signature)
  75. : d(new ctkBusEventData(topic, event_type, signature_type, objectPointer, signature)) {
  76. }
  77. /*
  78. * This is fast thanks to implicit sharing
  79. */
  80. ctkBusEvent::ctkBusEvent(const ctkBusEvent &event)
  81. : ctkEvent(event), d(event.d)
  82. {
  83. }
  84. ctkBusEvent::~ctkBusEvent()
  85. {
  86. }
  87. ctkBusEvent& ctkBusEvent::operator=(const ctkBusEvent& event)
  88. {
  89. d = event.d;
  90. return *this;
  91. }
  92. QVariant &ctkBusEvent::operator[](QString key) {
  93. return (d->properties)[key];
  94. }
  95. int ctkBusEvent::eventType() const {
  96. return static_cast<int>((d->properties).value("EventType").toInt());
  97. }
  98. QString ctkBusEvent::eventTopic() const {
  99. return (d->properties).value(TOPIC).toString();
  100. }
  101. bool ctkBusEvent::isEventLocal() const {
  102. int et = (d->properties).value(TYPE).toInt();
  103. return et == 0; //is local
  104. }
  105. void ctkBusEvent::setEventType(int et) {
  106. (d->properties).insert(TYPE, static_cast<int>(et));
  107. }
  108. void ctkBusEvent::setEventTopic(QString topic) {
  109. (d->properties).insert(TOPIC, topic);
  110. }