Update 更新文档
This commit is contained in:
81
TradingCore/utilities/EventBus.h
Normal file
81
TradingCore/utilities/EventBus.h
Normal file
@@ -0,0 +1,81 @@
|
||||
#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; // 0: auto, 1: queued, 2: blocking
|
||||
};
|
||||
|
||||
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 // QEVENTBUS_H
|
||||
Reference in New Issue
Block a user