62 lines
2.1 KiB
C++
62 lines
2.1 KiB
C++
#ifndef QEVENTBUS_H
|
|
#define QEVENTBUS_H
|
|
#include <QObject>
|
|
#include <QMap>
|
|
#include <QList>
|
|
#include <QMutex>
|
|
#include <QVariant>
|
|
#include <QDateTime>
|
|
#include <QMetaMethod>
|
|
namespace EventType {
|
|
const QString ORDER_PROCESSED = "order.processed";
|
|
const QString BIG_ORDER_DETECTED = "bigorder.detected";
|
|
const QString SUBSCRIPTION_CHANGED = "subscription.changed";
|
|
const QString CONNECTION_STATUS_CHANGED = "connection.status.changed";
|
|
const QString SYSTEM_ERROR = "system.error";
|
|
const QString PROCESSING_STARTED = "processing.started";
|
|
const QString PROCESSING_FINISHED = "processing.finished";
|
|
}
|
|
struct Event {
|
|
QString type;
|
|
QVariant data;
|
|
QDateTime timestamp;
|
|
QString source;
|
|
Event() : timestamp(QDateTime::currentDateTime()) {}
|
|
Event(const QString& type, const QVariant& data = QVariant(), const QString& source = "")
|
|
: type(type), data(data), timestamp(QDateTime::currentDateTime()), source(source) {}
|
|
};
|
|
class QEventBus : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
static QEventBus* instance();
|
|
void publish(const QString& eventType, const QVariant& data = QVariant(),
|
|
const QString& source = "");
|
|
void subscribe(const QString& eventType, QObject* receiver, const char* method);
|
|
void unsubscribe(const QString& eventType, QObject* receiver);
|
|
void unsubscribeAll(QObject* receiver);
|
|
QList<Event> getEventHistory(const QString& type = "", int limit = 100) const;
|
|
void clearEventHistory();
|
|
int getEventCount(const QString& type = "") const;
|
|
QMap<QString, int> getEventStatistics() const;
|
|
signals:
|
|
void eventPublished(const Event& event);
|
|
private:
|
|
explicit QEventBus(QObject *parent = nullptr);
|
|
~QEventBus();
|
|
struct Subscription {
|
|
QObject* receiver;
|
|
QByteArray method;
|
|
int connectionType;
|
|
};
|
|
QMap<QString, QList<Subscription>> m_subscriptions;
|
|
QList<Event> m_eventHistory;
|
|
mutable QMutex m_mutex;
|
|
int m_maxHistorySize = 1000;
|
|
enum ConnectionType {
|
|
AutoConnection = 0,
|
|
QueuedConnection = 1,
|
|
BlockingQueuedConnection = 2
|
|
};
|
|
};
|
|
#endif |