2026-03-18 17:49:22 +08:00
|
|
|
# Event
|
|
|
|
|
|
|
|
|
|
**命名空间**: `XCEngine::Core`
|
|
|
|
|
|
|
|
|
|
**类型**: `class` (template)
|
|
|
|
|
|
2026-03-20 02:35:07 +08:00
|
|
|
**头文件**: `XCEngine/Core/Event.h`
|
|
|
|
|
|
2026-03-18 17:49:22 +08:00
|
|
|
**描述**: 事件系统模板类,提供类型安全的多订阅者事件/委托系统。
|
|
|
|
|
|
|
|
|
|
## 概述
|
|
|
|
|
|
|
|
|
|
`Event<Args...>` 是一个类型安全的事件发布-订阅系统。它支持多个回调订阅、退订和线程安全的调用。非常适合用于游戏引擎中的事件驱动通信。
|
|
|
|
|
|
|
|
|
|
## 模板参数
|
|
|
|
|
|
|
|
|
|
| 参数 | 描述 |
|
|
|
|
|
|------|------|
|
|
|
|
|
| `Args...` | 事件回调的参数类型 |
|
|
|
|
|
|
|
|
|
|
## 类型别名
|
|
|
|
|
|
|
|
|
|
| 别名 | 类型 | 描述 |
|
|
|
|
|
|------|------|------|
|
|
|
|
|
| `Callback` | `std::function<void(Args...)>` | 回调函数类型 |
|
|
|
|
|
| `Listener` | `std::pair<uint64_t, Callback>` | 监听器条目 |
|
|
|
|
|
| `Iterator` | `std::vector<Listener>::iterator` | 迭代器类型 |
|
|
|
|
|
|
|
|
|
|
## 公共方法
|
|
|
|
|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
| 方法 | 描述 |
|
|
|
|
|
|------|------|
|
2026-03-19 02:01:18 +08:00
|
|
|
| [`Subscribe`](Subscribe.md) | 订阅事件回调,返回订阅 ID |
|
|
|
|
|
| [`Unsubscribe`](Unsubscribe.md) | 退订事件(延迟生效) |
|
|
|
|
|
| [`ProcessUnsubscribes`](ProcessUnsubscribes.md) | 处理积压的退订请求 |
|
|
|
|
|
| [`Clear`](Clear.md) | 清空所有订阅 |
|
|
|
|
|
| [`Invoke`](Invoke.md) | 调用所有订阅的回调 |
|
|
|
|
|
| [`begin`](begin.md) | 获取开始迭代器 |
|
|
|
|
|
| [`end`](end.md) | 获取结束迭代器 |
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
|
2026-03-18 17:49:22 +08:00
|
|
|
## 使用示例
|
|
|
|
|
|
|
|
|
|
```cpp
|
2026-03-19 01:04:30 +08:00
|
|
|
#include <XCEngine/Core/Event.h>
|
|
|
|
|
|
|
|
|
|
using namespace XCEngine::Core;
|
|
|
|
|
|
2026-03-18 17:49:22 +08:00
|
|
|
// 定义事件(无参数)
|
|
|
|
|
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();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 相关文档
|
|
|
|
|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
- [Core 模块总览](../core.md) - 返回模块总览
|