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 个断裂引用
This commit is contained in:
37
docs/api/core/event/Clear.md
Normal file
37
docs/api/core/event/Clear.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Event::Clear
|
||||
|
||||
```cpp
|
||||
void Clear();
|
||||
```
|
||||
|
||||
清空所有订阅。
|
||||
|
||||
**描述**
|
||||
|
||||
移除所有已订阅的回调,清空事件。线程安全。
|
||||
|
||||
**复杂度:** O(n) 其中 n 为订阅数量
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
#include <XCEngine/Core/Event.h>
|
||||
|
||||
Event<int> event;
|
||||
|
||||
// 订阅多个回调
|
||||
event.Subscribe([](int v) { });
|
||||
event.Subscribe([](int v) { });
|
||||
event.Subscribe([](int v) { });
|
||||
|
||||
// 清空所有订阅
|
||||
event.Clear();
|
||||
|
||||
// 触发(无回调被执行)
|
||||
event.Invoke(42);
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [Event 总览](event.md) - 返回类总览
|
||||
- [Unsubscribe](Unsubscribe.md) - 退订单个事件
|
||||
43
docs/api/core/event/Invoke.md
Normal file
43
docs/api/core/event/Invoke.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# Event::Invoke
|
||||
|
||||
```cpp
|
||||
void Invoke(Args... args);
|
||||
```
|
||||
|
||||
调用所有订阅的回调。
|
||||
|
||||
**描述**
|
||||
|
||||
依次调用所有已订阅的回调函数。在调用前会自动处理待退订的回调。回调是在锁外执行的,因此可以在回调中安全地进行订阅/退订操作。线程安全。
|
||||
|
||||
**参数:**
|
||||
- `args` - 传递给回调的参数
|
||||
|
||||
**复杂度:** O(n) 其中 n 为订阅数量
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
#include <XCEngine/Core/Event.h>
|
||||
|
||||
// 无参数事件
|
||||
Event<> frameEndEvent;
|
||||
frameEndEvent.Subscribe([]() {
|
||||
printf("Frame ended\n");
|
||||
});
|
||||
|
||||
// 带参数事件
|
||||
Event<int, const char*> playerDiedEvent;
|
||||
playerDiedEvent.Subscribe([](int playerId, const char* reason) {
|
||||
printf("Player %d died: %s\n", playerId, reason);
|
||||
});
|
||||
|
||||
// 触发事件
|
||||
frameEndEvent.Invoke();
|
||||
playerDiedEvent.Invoke(1, "fell off a cliff");
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [Event 总览](event.md) - 返回类总览
|
||||
- [Subscribe](Subscribe.md) - 订阅事件
|
||||
37
docs/api/core/event/ProcessUnsubscribes.md
Normal file
37
docs/api/core/event/ProcessUnsubscribes.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Event::ProcessUnsubscribes
|
||||
|
||||
```cpp
|
||||
void ProcessUnsubscribes();
|
||||
```
|
||||
|
||||
处理积压的退订请求。
|
||||
|
||||
**描述**
|
||||
|
||||
手动处理待退订的回调,将其从订阅列表中移除。通常不需要手动调用,`Invoke` 会自动处理退订。但在某些场景下可能需要主动调用。
|
||||
|
||||
**复杂度:** O(n) 其中 n 为待退订数量
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
#include <XCEngine/Core/Event.h>
|
||||
|
||||
Event<int> event;
|
||||
|
||||
// 订阅
|
||||
uint64_t id = event.Subscribe([](int value) { });
|
||||
|
||||
// 退订请求入队
|
||||
event.Unsubscribe(id);
|
||||
|
||||
// 主动处理退订
|
||||
event.ProcessUnsubscribes();
|
||||
|
||||
// 此时事件列表中已无该回调
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [Event 总览](event.md) - 返回类总览
|
||||
- [Unsubscribe](Unsubscribe.md) - 退订事件
|
||||
48
docs/api/core/event/Subscribe.md
Normal file
48
docs/api/core/event/Subscribe.md
Normal file
@@ -0,0 +1,48 @@
|
||||
# Event::Subscribe
|
||||
|
||||
```cpp
|
||||
uint64_t Subscribe(Callback callback);
|
||||
```
|
||||
|
||||
订阅事件回调。
|
||||
|
||||
**描述**
|
||||
|
||||
将回调函数添加到事件订阅列表中,并返回一个唯一的订阅 ID。该 ID 可用于后续退订。线程安全,可在任意线程调用。
|
||||
|
||||
**参数:**
|
||||
- `callback` - 要订阅的回调函数,类型为 `std::function<void(Args...)>`
|
||||
|
||||
**返回:** `uint64_t` - 订阅 ID,用于退订
|
||||
|
||||
**复杂度:** O(1) amortized
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
#include <XCEngine/Core/Event.h>
|
||||
|
||||
// 定义事件
|
||||
Event<int, float> damageEvent;
|
||||
|
||||
// 订阅多个回调
|
||||
uint64_t id1 = damageEvent.Subscribe([](int damage, float time) {
|
||||
printf("Damage taken: %d at time %f\n", damage, time);
|
||||
});
|
||||
|
||||
uint64_t id2 = damageEvent.Subscribe([](int damage, float time) {
|
||||
// 记录伤害日志
|
||||
});
|
||||
|
||||
// 使用 lambda 表达式
|
||||
auto callback = [](int damage, float time) {
|
||||
// 处理伤害
|
||||
};
|
||||
uint64_t id3 = damageEvent.Subscribe(callback);
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [Event 总览](event.md) - 返回类总览
|
||||
- [Unsubscribe](Unsubscribe.md) - 退订事件
|
||||
- [Invoke](Invoke.md) - 触发事件
|
||||
41
docs/api/core/event/Unsubscribe.md
Normal file
41
docs/api/core/event/Unsubscribe.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# Event::Unsubscribe
|
||||
|
||||
```cpp
|
||||
void Unsubscribe(uint64_t id);
|
||||
```
|
||||
|
||||
退订事件。
|
||||
|
||||
**描述**
|
||||
|
||||
将指定 ID 的回调从订阅列表中移除。退订是延迟生效的,在调用 `Invoke` 时会一并处理待退订的回调。线程安全,可在任意线程调用。
|
||||
|
||||
**参数:**
|
||||
- `id` - 订阅时返回的 ID
|
||||
|
||||
**复杂度:** O(n) 在 Invoke 时处理
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
#include <XCEngine/Core/Event.h>
|
||||
|
||||
Event<int> someEvent;
|
||||
|
||||
// 订阅
|
||||
uint64_t id = someEvent.Subscribe([](int value) {
|
||||
printf("Value: %d\n", value);
|
||||
});
|
||||
|
||||
// 退订
|
||||
someEvent.Unsubscribe(id);
|
||||
|
||||
// 触发(已退订的回调不会被调用)
|
||||
someEvent.Invoke(42);
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [Event 总览](event.md) - 返回类总览
|
||||
- [Subscribe](Subscribe.md) - 订阅事件
|
||||
- [ProcessUnsubscribes](ProcessUnsubscribes.md) - 手动处理退订
|
||||
35
docs/api/core/event/begin.md
Normal file
35
docs/api/core/event/begin.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# Event::begin
|
||||
|
||||
```cpp
|
||||
Iterator begin();
|
||||
```
|
||||
|
||||
获取开始迭代器。
|
||||
|
||||
**描述**
|
||||
|
||||
返回订阅列表的开始迭代器,用于范围for循环遍历所有订阅的回调。
|
||||
|
||||
**返回:** `Iterator` - 指向第一个监听器的迭代器
|
||||
|
||||
**复杂度:** O(1)
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
#include <XCEngine/Core/Event.h>
|
||||
|
||||
Event<int> event;
|
||||
event.Subscribe([](int v) { printf("Callback 1: %d\n", v); });
|
||||
event.Subscribe([](int v) { printf("Callback 2: %d\n", v); });
|
||||
|
||||
// 遍历所有订阅
|
||||
for (auto& [id, callback] : event) {
|
||||
callback(100);
|
||||
}
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [Event 总览](event.md) - 返回类总览
|
||||
- [end](end.md) - 获取结束迭代器
|
||||
34
docs/api/core/event/end.md
Normal file
34
docs/api/core/event/end.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# Event::end
|
||||
|
||||
```cpp
|
||||
Iterator end();
|
||||
```
|
||||
|
||||
获取结束迭代器。
|
||||
|
||||
**描述**
|
||||
|
||||
返回订阅列表的结束迭代器,用于范围for循环遍历所有订阅的回调。
|
||||
|
||||
**返回:** `Iterator` - 指向末尾的迭代器
|
||||
|
||||
**复杂度:** O(1)
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
#include <XCEngine/Core/Event.h>
|
||||
|
||||
Event<int> event;
|
||||
event.Subscribe([](int v) { printf("Callback: %d\n", v); });
|
||||
|
||||
// 遍历所有订阅
|
||||
for (auto& [id, callback] : event) {
|
||||
callback(42);
|
||||
}
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [Event 总览](event.md) - 返回类总览
|
||||
- [begin](begin.md) - 获取开始迭代器
|
||||
89
docs/api/core/event/event.md
Normal file
89
docs/api/core/event/event.md
Normal file
@@ -0,0 +1,89 @@
|
||||
# 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](Subscribe.md) | 订阅事件回调 |
|
||||
| [Unsubscribe](Unsubscribe.md) | 退订事件 |
|
||||
| [ProcessUnsubscribes](ProcessUnsubscribes.md) | 处理积压的退订请求 |
|
||||
| [Invoke](Invoke.md) | 调用所有订阅的回调 |
|
||||
| [Clear](Clear.md) | 清空所有订阅 |
|
||||
| [begin](begin.md) | 获取开始迭代器 |
|
||||
| [end](end.md) | 获取结束迭代器 |
|
||||
|
||||
## 使用示例
|
||||
|
||||
```cpp
|
||||
// 定义事件(无参数)
|
||||
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();
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [Core 模块总览](../core.md) - 返回模块总览
|
||||
Reference in New Issue
Block a user