Skip to content
Snippets Groups Projects
Commit 9d431358 authored by Riccardo Musso's avatar Riccardo Musso
Browse files

Replaced Topics with TopicFilters

parent e8264541
No related branches found
No related tags found
No related merge requests found
......@@ -13,13 +13,13 @@ ModuleMessagesBroker::~ModuleMessagesBroker() {
}
void ModuleMessagesBroker::subscribe(Topic topic, Module* observer, std::function<void(const ModuleMessage& msg)> callBack) {
void ModuleMessagesBroker::subscribe(TopicFilter topic, Module* observer, std::function<void(const ModuleMessage& msg)> callBack) {
CallbackCollection& collection = topic.isWildcard() ? wildcardObservers : exactObservers;
collection.insert(topic.toString(), qMakePair(observer, callBack));
connect(observer->getModuleEventsHandler(), &ModuleEventsHandler::beforeDelete, this, &ModuleMessagesBroker::onModuleDeleted);
}
void ModuleMessagesBroker::unsubscribe(Topic topic, Module* module) {
void ModuleMessagesBroker::unsubscribe(TopicFilter topic, Module* module) {
CallbackCollection& collection = topic.isWildcard() ? wildcardObservers : exactObservers;
QList<QPair<Module*, MessageCallback>> callbackList = collection.values(topic.toString());
for(int i = 0; i < callbackList.size(); ) {
......@@ -42,7 +42,7 @@ void ModuleMessagesBroker::publish(const ModuleMessage& msg) {
}
for(auto it = wildcardObservers.keyBegin(); it != wildcardObservers.keyEnd(); ++it) {
if(msg.getTopic().match(*it)) {
if(TopicFilter(*it).match(msg.getTopic())) {
auto list = wildcardObservers.values(*it);
for(auto& item : list) {
item.second(msg);
......@@ -51,18 +51,18 @@ void ModuleMessagesBroker::publish(const ModuleMessage& msg) {
}
}
QList<Topic> ModuleMessagesBroker::getSubscriptionsOf(Module* module) {
QList<Topic> topics;
QList<TopicFilter> ModuleMessagesBroker::getSubscriptionsOf(Module* module) {
QList<TopicFilter> topics;
for(auto it = wildcardObservers.keyValueBegin(); it != wildcardObservers.keyValueEnd(); ++it) {
if(it->second.first == module) {
topics.append(Topic(it->first));
topics.append(TopicFilter(it->first));
}
}
for(auto it = exactObservers.keyValueBegin(); it != wildcardObservers.keyValueEnd(); ++it) {
if(it->second.first == module) {
topics.append(Topic(it->first));
topics.append(TopicFilter(it->first));
}
}
......@@ -73,10 +73,10 @@ void ModuleMessagesBroker::onModuleDeleted(Module* module) {
// Deletion of modules happens so rarely that we can afford
// this "slow" way:
auto listOfTopics = getSubscriptionsOf(module);
for(Topic topic : listOfTopics) {
CallbackCollection& collection = topic.isWildcard() ? wildcardObservers : exactObservers;
auto subList = collection.values(topic.toString());
auto listOfFilters = getSubscriptionsOf(module);
for(TopicFilter filter : listOfFilters) {
CallbackCollection& collection = filter.isWildcard() ? wildcardObservers : exactObservers;
auto subList = collection.values(filter.toString());
for(int i = 0; i != subList.size(); ) {
if(subList[i].first == module) {
subList.removeAt(i);
......
......@@ -7,7 +7,7 @@
#include <QList>
#include "Message/modulemessage.h"
#include "Message/topic.h"
#include "Message/topicfilter.h"
class Module;
......@@ -21,12 +21,12 @@ class ModuleMessagesBroker : public QObject {
ModuleMessagesBroker();
~ModuleMessagesBroker();
void subscribe(Topic topic, Module* observer, MessageCallback callBack);
// unsubscribe remove ALL callbacks associated with (topic, module)
void unsubscribe(Topic topic, Module* module);
void subscribe(TopicFilter topic, Module* observer, MessageCallback callBack);
// unsubscribe remove ALL callbacks associated with (topicFilter, module)
void unsubscribe(TopicFilter topic, Module* module);
void publish(const ModuleMessage& msg);
QList<Topic> getSubscriptionsOf(Module* module);
QList<TopicFilter> getSubscriptionsOf(Module* module);
public slots:
void onModuleDeleted(Module* module);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment