ctkEventBusImpl.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "ctkEventBusImpl_p.h"
  2. #include <QSetIterator>
  3. #include "ctkEventHandlerWrapper_p.h"
  4. ctkEventBusImpl* ctkEventBusImpl::instance()
  5. {
  6. static ctkEventBusImpl inst;
  7. return &inst;
  8. }
  9. ctkEventBusImpl::ctkEventBusImpl()
  10. {
  11. }
  12. void ctkEventBusImpl::postEvent(const ctkEvent& event)
  13. {
  14. dispatchEvent(event, true);
  15. }
  16. void ctkEventBusImpl::sendEvent(const ctkEvent& event)
  17. {
  18. dispatchEvent(event, false);
  19. }
  20. void ctkEventBusImpl::publishSignal(const QObject* publisher, const char* signal)
  21. {
  22. }
  23. void ctkEventBusImpl::subscribeSlot(const QObject* subscriber, const char* member, const Properties& properties)
  24. {
  25. // TODO check for duplicates
  26. ctkEventHandlerWrapper* wrapper = new ctkEventHandlerWrapper(subscriber, member, properties);
  27. if (wrapper->init())
  28. {
  29. bucket(wrapper);
  30. }
  31. }
  32. void ctkEventBusImpl::dispatchEvent(const ctkEvent& event, bool isAsync)
  33. {
  34. QString topic = event.topic();
  35. QSet<ctkEventHandlerWrapper*> eventHandlers = this->handlers(topic);
  36. if (eventHandlers.empty()) return;
  37. QSetIterator<ctkEventHandlerWrapper*> iter(eventHandlers);
  38. while (iter.hasNext())
  39. {
  40. iter.next()->handleEvent(event);
  41. }
  42. }
  43. void ctkEventBusImpl::bucket(ctkEventHandlerWrapper* wrapper)
  44. {
  45. // TODO bucket logic
  46. globalWildcard.push_back(wrapper);
  47. }
  48. QSet<ctkEventHandlerWrapper*> ctkEventBusImpl::handlers(const QString& topic)
  49. {
  50. // TODO
  51. return globalWildcard.toSet();
  52. }