ctkEventBusImpl.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. Q_UNUSED(publisher)
  23. Q_UNUSED(signal)
  24. }
  25. void ctkEventBusImpl::subscribeSlot(const QObject* subscriber, const char* member, const Properties& properties)
  26. {
  27. // TODO check for duplicates
  28. ctkEventHandlerWrapper* wrapper = new ctkEventHandlerWrapper(subscriber, member, properties);
  29. if (wrapper->init())
  30. {
  31. bucket(wrapper);
  32. }
  33. }
  34. void ctkEventBusImpl::dispatchEvent(const ctkEvent& event, bool isAsync)
  35. {
  36. Q_UNUSED(isAsync)
  37. QString topic = event.topic();
  38. QSet<ctkEventHandlerWrapper*> eventHandlers = this->handlers(topic);
  39. if (eventHandlers.empty()) return;
  40. QSetIterator<ctkEventHandlerWrapper*> iter(eventHandlers);
  41. while (iter.hasNext())
  42. {
  43. iter.next()->handleEvent(event);
  44. }
  45. }
  46. void ctkEventBusImpl::bucket(ctkEventHandlerWrapper* wrapper)
  47. {
  48. // TODO bucket logic
  49. globalWildcard.push_back(wrapper);
  50. }
  51. QSet<ctkEventHandlerWrapper*> ctkEventBusImpl::handlers(const QString& topic)
  52. {
  53. Q_UNUSED(topic)
  54. // TODO
  55. return globalWildcard.toSet();
  56. }