forked from benlau/quickios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqisystemdispatcher.cpp
45 lines (36 loc) · 1.14 KB
/
qisystemdispatcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <QCoreApplication>
#include <QPointer>
#include <QtCore>
#include "qisystemdispatcher.h"
typedef bool (*handler)(QVariantMap& data);
static QMap<QString,handler> handlers;
static QPointer<QISystemDispatcher> m_instance;
QISystemDispatcher *QISystemDispatcher::instance()
{
if (!m_instance) {
QCoreApplication* app = QCoreApplication::instance();
m_instance = new QISystemDispatcher(app);
}
return m_instance;
}
QISystemDispatcher::QISystemDispatcher(QObject *parent) : QObject(parent) {
}
bool QISystemDispatcher::dispatch(QString type , QVariantMap message) {
QMetaObject::invokeMethod(this,"dispatched",Qt::QueuedConnection,
Q_ARG(QString , type),
Q_ARG(QVariantMap,message));
bool res = false;
if (handlers.contains(type)) {
res = handlers[type](message);
}
return res;
}
bool QISystemDispatcher::addListener(QString name, bool (*func)(QVariantMap&))
{
if (handlers.contains(name)) {
qWarning() << QString("%s is already registered").arg(name);
return false;
}
handlers[name] = func;
return true;
}