2026-03-18 17:49:22 +08:00
|
|
|
|
# SmartPtr
|
|
|
|
|
|
|
|
|
|
|
|
**命名空间**: `XCEngine::Core`
|
|
|
|
|
|
|
|
|
|
|
|
**类型**: `header-only`
|
|
|
|
|
|
|
2026-03-20 02:35:07 +08:00
|
|
|
|
**头文件**: `XCEngine/Core/SmartPtr.h`
|
|
|
|
|
|
|
2026-03-18 17:49:22 +08:00
|
|
|
|
**描述**: 智能指针类型别名和工厂函数,提供 `std::shared_ptr` 和 `std::unique_ptr` 的简化接口。
|
|
|
|
|
|
|
|
|
|
|
|
## 概述
|
|
|
|
|
|
|
|
|
|
|
|
`Core::SmartPtr` 提供了 `std::shared_ptr` 和 `std::unique_ptr` 的类型别名和工厂函数,使智能指针的使用更加简洁。
|
|
|
|
|
|
|
|
|
|
|
|
## 类型别名
|
|
|
|
|
|
|
|
|
|
|
|
| 别名 | 底层类型 | 描述 |
|
|
|
|
|
|
|------|----------|------|
|
|
|
|
|
|
| `Core::Ref<T>` | `std::shared_ptr<T>` | 共享引用智能指针 |
|
|
|
|
|
|
| `Core::UniqueRef<T>` | `std::unique_ptr<T>` | 独占所有权的智能指针 |
|
|
|
|
|
|
|
|
|
|
|
|
## 工厂函数
|
|
|
|
|
|
|
|
|
|
|
|
| 函数 | 返回值 | 描述 |
|
|
|
|
|
|
|------|--------|------|
|
|
|
|
|
|
| `Core::MakeRef<T>(Args&&... args)` | `Ref<T>` | 创建共享指针 |
|
|
|
|
|
|
| `Core::MakeUnique<T>(Args&&... args)` | `UniqueRef<T>` | 创建独占指针 |
|
|
|
|
|
|
|
2026-03-19 02:01:18 +08:00
|
|
|
|
## 公共方法
|
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
|
|
|
|
| [`Ref`](Ref.md) | `std::shared_ptr<T>` 类型别名 |
|
|
|
|
|
|
| [`UniqueRef`](UniqueRef.md) | `std::unique_ptr<T>` 类型别名 |
|
|
|
|
|
|
| [`MakeRef`](MakeRef.md) | 创建共享指针的工厂函数 |
|
|
|
|
|
|
| [`MakeUnique`](MakeUnique.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
|
|
|
|
|
|
#include <XCEngine/Core/Core.h>
|
|
|
|
|
|
|
|
|
|
|
|
// 使用 Ref(共享指针)
|
|
|
|
|
|
Core::Ref<MyClass> ref = Core::MakeRef<MyClass>(arg1, arg2);
|
|
|
|
|
|
|
|
|
|
|
|
// 使用 UniqueRef(独占指针)
|
|
|
|
|
|
Core::UniqueRef<MyClass> unique = Core::MakeUnique<MyClass>();
|
|
|
|
|
|
|
|
|
|
|
|
// 传递所有权
|
|
|
|
|
|
Core::Ref<MyClass> ref2 = ref; // 引用计数 +1
|
|
|
|
|
|
|
|
|
|
|
|
// 检查有效性
|
|
|
|
|
|
if (ref) {
|
|
|
|
|
|
ref->DoSomething();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 自定义删除器
|
|
|
|
|
|
Core::UniqueRef<FILE, decltype(&fclose)>
|
|
|
|
|
|
file(fopen("test.txt", "r"), &fclose);
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 相关文档
|
|
|
|
|
|
|
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) - 返回模块总览
|
|
|
|
|
|
- [RefCounted](../refcounted/refcounted.md) - 引用计数基类
|
2026-03-19 00:47:57 +08:00
|
|
|
|
- [Types](../types/types.md) - 类型别名
|