ctkTopicRegistry.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * ctkTopicRegistry.cpp
  3. * ctkEventBus
  4. *
  5. * Created by Roberto Mucci on 26/01/11.
  6. * Copyright 2011 B3C. All rights reserved.
  7. *
  8. * See Licence at: http://tiny.cc/QXJ4D
  9. *
  10. */
  11. #include "ctkTopicRegistry.h"
  12. using namespace ctkEventBus;
  13. ctkTopicRegistry* ctkTopicRegistry::instance() {
  14. static ctkTopicRegistry instanceTopicRegistry;
  15. return &instanceTopicRegistry;
  16. }
  17. ctkTopicRegistry::ctkTopicRegistry() {
  18. }
  19. void ctkTopicRegistry::shutdown() {
  20. m_TopicHash.clear();
  21. }
  22. bool ctkTopicRegistry::registerTopic(const QString topic, const QObject *owner) {
  23. if(topic.isEmpty() || owner == NULL ){
  24. return false;
  25. }
  26. if(m_TopicHash.contains(topic)){
  27. //topic already registered
  28. const QObject *obj = m_TopicHash.value(topic,NULL);
  29. QString className(obj->metaObject()->className());
  30. qWarning() << QObject::tr("Topic %1 already owned by %2").arg(topic, className);
  31. return false;
  32. }
  33. m_TopicHash.insert(topic,owner);
  34. return true;
  35. }
  36. bool ctkTopicRegistry::unregisterTopic(const QString topic) {
  37. bool result = false;
  38. if(m_TopicHash.contains(topic)){
  39. if (m_TopicHash.remove(topic) > 0) {
  40. result = true;
  41. }
  42. }
  43. return result;
  44. }
  45. const QObject *ctkTopicRegistry::owner(const QString topic) const {
  46. return m_TopicHash.value(topic,NULL);
  47. }
  48. bool ctkTopicRegistry::isTopicRegistered(const QString topic) const {
  49. return m_TopicHash.contains(topic);
  50. }
  51. void ctkTopicRegistry::dump() {
  52. QHash<QString, const QObject*>::const_iterator i = m_TopicHash.constBegin();
  53. while (i != m_TopicHash.constEnd()) {
  54. const QObject *obj = i.value();
  55. QString key = i.key();
  56. QString name = obj->metaObject()->className();
  57. qDebug() << "Topic: " << key << " Owner: " << name;
  58. ++i;
  59. }
  60. }