Files
XCEngine/docs/api/core/event/event.md
ssdfasd de4086dbfe docs: Fix core module documentation discrepancies
Fixed the following issues in XCEngine Core module documentation:
- Added 'using namespace XCEngine::Core;' to all code examples that use
  Core types (Event, FileWriter, etc.) without full namespace qualification
- Added missing '#include <XCEngine/Containers/String.h>' to FileWriter
  examples that use Containers::String
- Added '#include <string>' to Flush.md example using std::to_string

Affected files:
- core/core.md: Added using directive and Containers include
- event/*.md: Added using namespace to all 8 event doc files
- filewriter/*.md: Added using namespace and proper includes to all 6 files
2026-03-19 01:04:30 +08:00

2.2 KiB

Event

命名空间: XCEngine::Core

类型: class (template)

描述: 事件系统模板类,提供类型安全的多订阅者事件/委托系统。

概述

Event<Args...> 是一个类型安全的事件发布-订阅系统。它支持多个回调订阅、退订和线程安全的调用。非常适合用于游戏引擎中的事件驱动通信。

模板参数

参数 描述
Args... 事件回调的参数类型

类型别名

别名 类型 描述
Callback std::function<void(Args...)> 回调函数类型
Listener std::pair<uint64_t, Callback> 监听器条目
Iterator std::vector<Listener>::iterator 迭代器类型

公共方法

订阅/退订

方法 描述
uint64_t Subscribe(Callback callback) 订阅事件,返回订阅 ID
void Unsubscribe(uint64_t id) 退订事件(延迟生效)
void ProcessUnsubscribes() 处理积压的退订请求
void Clear() 清空所有订阅

调用

方法 描述
void Invoke(Args... args) 调用所有订阅的回调

迭代

方法 描述
Iterator begin() 获取开始迭代器
Iterator end() 获取结束迭代器

方法列表

方法 描述
Subscribe 订阅事件回调
Unsubscribe 退订事件
ProcessUnsubscribes 处理积压的退订请求
Invoke 调用所有订阅的回调
Clear 清空所有订阅
begin 获取开始迭代器
end 获取结束迭代器

使用示例

#include <XCEngine/Core/Event.h>

using namespace XCEngine::Core;

// 定义事件(无参数)
Event<> frameStartEvent;

// 定义事件(带参数)
Event<int, float> damageEvent;

// 订阅
uint64_t id = damageEvent.Subscribe([](int damage, float time) {
    printf("Damage: %d at time %f\n", damage, time);
});

// 触发事件
damageEvent.Invoke(100, 3.14f);

// 退订
damageEvent.Unsubscribe(id);

// 清空
frameStartEvent.Clear();

相关文档