2025-04-28 12:25:20 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
#include "XNBaseFrameObject.h"
|
2025-05-20 15:39:40 +08:00
|
|
|
|
|
2025-04-28 12:25:20 +08:00
|
|
|
|
// 事件优先级定义
|
|
|
|
|
namespace XNEvent
|
|
|
|
|
{
|
|
|
|
|
enum class Priority {
|
|
|
|
|
RealTime = 0, // 实时优先级
|
|
|
|
|
High = 1, // 高优先级
|
|
|
|
|
Normal = 2, // 普通优先级
|
|
|
|
|
Low = 3 // 低优先级
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-05-20 15:39:40 +08:00
|
|
|
|
|
2025-04-28 12:25:20 +08:00
|
|
|
|
// 前向声明私有类
|
2025-05-20 15:39:40 +08:00
|
|
|
|
struct XNEventManagerPrivate;
|
2025-04-28 12:25:20 +08:00
|
|
|
|
|
|
|
|
|
// 事件管理器类,继承自XNBaseFrameObject
|
|
|
|
|
class XNEventManager : public XNBaseFrameObject
|
|
|
|
|
{
|
2025-05-20 15:39:40 +08:00
|
|
|
|
XN_METATYPE(XNEventManager, XNBaseFrameObject)
|
|
|
|
|
XN_DECLARE_PRIVATE(XNEventManager)
|
|
|
|
|
public:
|
|
|
|
|
// 构造函数,创建事件管理器实例
|
|
|
|
|
XNEventManager();
|
2025-04-28 12:25:20 +08:00
|
|
|
|
// 析构函数
|
|
|
|
|
~XNEventManager();
|
|
|
|
|
|
|
|
|
|
// 注册事件处理器
|
|
|
|
|
// @param eventName: 事件名称
|
|
|
|
|
// @param callback: 事件处理回调函数
|
|
|
|
|
// @param objectId: 对象ID
|
|
|
|
|
// @param async: 是否异步处理该事件
|
|
|
|
|
// @param priority: 事件优先级
|
|
|
|
|
// @return: 返回处理器ID,失败返回-1
|
2025-05-20 15:39:40 +08:00
|
|
|
|
int RegisterEventHandler(const std::string &eventName, XNEventCallback callback,
|
|
|
|
|
uint32_t objectId, bool async = false,
|
2025-04-28 12:25:20 +08:00
|
|
|
|
XNEvent::Priority priority = XNEvent::Priority::Normal);
|
|
|
|
|
// 移除事件处理器
|
|
|
|
|
// @param eventName: 事件名称
|
|
|
|
|
// @param handlerId: 处理器ID
|
|
|
|
|
// @return: 移除是否成功
|
2025-05-20 15:39:40 +08:00
|
|
|
|
bool RemoveEventHandler(const std::string &eventName, int handlerId);
|
2025-04-28 12:25:20 +08:00
|
|
|
|
// 触发指定事件
|
|
|
|
|
// @param eventName: 要触发的事件名称
|
|
|
|
|
// @param eventData: 事件携带的数据
|
|
|
|
|
// @param forceAsync: 强制异步处理
|
|
|
|
|
// @param priority: 事件优先级
|
2025-05-20 15:39:40 +08:00
|
|
|
|
void TriggerEvent(const std::string &eventName, const std::any &eventData = std::any(),
|
2025-04-28 12:25:20 +08:00
|
|
|
|
bool forceAsync = false,
|
|
|
|
|
XNEvent::Priority priority = XNEvent::Priority::Normal);
|
|
|
|
|
|
|
|
|
|
// 设置线程池最大线程数
|
|
|
|
|
void SetMaxThreadCount(int count);
|
|
|
|
|
|
|
|
|
|
// 获取线程池最大线程数
|
|
|
|
|
int GetMaxThreadCount() const;
|
|
|
|
|
|
|
|
|
|
// 等待所有异步事件处理完成
|
|
|
|
|
void WaitForAsyncEvents();
|
|
|
|
|
|
|
|
|
|
// 设置实时线程池参数
|
|
|
|
|
void SetRTThreadPoolConfig(int maxThreads, int minPriority, int maxPriority);
|
|
|
|
|
|
2025-05-20 15:39:40 +08:00
|
|
|
|
// 事件处理完成回调
|
|
|
|
|
void EventProcessed(const std::string &eventName, bool success);
|
|
|
|
|
|
2025-04-28 12:25:20 +08:00
|
|
|
|
protected:
|
|
|
|
|
// 保护构造函数,用于继承实现
|
2025-05-20 15:39:40 +08:00
|
|
|
|
XNEventManager(PrivateType *p);
|
2025-04-28 12:25:20 +08:00
|
|
|
|
|
2025-05-20 15:39:40 +08:00
|
|
|
|
public:
|
2025-04-28 12:25:20 +08:00
|
|
|
|
// 初始化事件管理器
|
2025-05-20 15:39:40 +08:00
|
|
|
|
virtual bool Initialize() override;
|
2025-04-28 12:25:20 +08:00
|
|
|
|
// 准备执行
|
2025-05-20 15:39:40 +08:00
|
|
|
|
virtual bool PrepareForExecute() override;
|
2025-04-28 12:25:20 +08:00
|
|
|
|
};
|