当前位置: 首页 > news >正文

UE5.7.4源代码分析——字符类型

GenericPlatform.h

完整路径:UE_5.7\Engine\Source\Runtime\Core\Public\GenericPlatform\GenericPlatform.h

template<typename T32BITS, typename T64BITS, int PointerSize> struct SelectIntPointerType { // nothing here are is it an error if the partial specializations fail }; template<typename T32BITS, typename T64BITS> struct SelectIntPointerType<T32BITS, T64BITS, 8> { // Select the 64 bit type. typedef T64BITS TIntPointer; }; template<typename T32BITS, typename T64BITS> struct SelectIntPointerType<T32BITS, T64BITS, 4> { // Select the 32 bit type. typedef T32BITS TIntPointer; };

当PointerSize既不是4也不是8时,会导致编译错误

当PointerSize==8时,选择 64 位类型指针

当PointerSize==4时,选择 32 位类型指针

该模板常用于跨平台编程,根据目标平台的指针大小选择合适的整数类型

struct FGenericPlatformTypes { //~ Unsigned base types // 8-bit unsigned integer typedef unsigned char uint8; // 16-bit unsigned integer typedef unsigned short int uint16; // 32-bit unsigned integer typedef unsigned int uint32; // 64-bit unsigned integer typedef unsigned long long uint64; //~ Signed base types. // 8-bit signed integer typedef signed char int8; // 16-bit signed integer typedef signed short int int16; // 32-bit signed integer typedef signed int int32; // 64-bit signed integer typedef signed long long int64; //~ Character types // An ANSI character. 8-bit fixed-width representation of 7-bit characters. typedef char ANSICHAR; // A wide character. In-memory only. ?-bit fixed-width representation of the platform's natural wide character set. Could be different sizes on different platforms. typedef wchar_t WIDECHAR; // An 8-bit character type. In-memory only. 8-bit representation. using UTF8CHAR = char8_t; // A 16-bit character type. In-memory only. 16-bit representation. Should really be char16_t but making this the generic option is easier for compilers which don't fully support C++11 yet (i.e. MSVC). typedef uint16 CHAR16; // A 32-bit character type. In-memory only. 32-bit representation. Should really be char32_t but making this the generic option is easier for compilers which don't fully support C++11 yet (i.e. MSVC). [[deprecated("CHAR32 has been deprecated - please use UTF32CHAR instead.")]] // not using UE_DEPRECATED(5.6, "...") for dependency reasons typedef uint32 CHAR32; // A 32-bit character type. In-memory only. 32-bit representation. using UTF32CHAR = char32_t; // A switchable character. In-memory only. Either ANSICHAR or WIDECHAR, depending on a licensee's requirements. typedef WIDECHAR TCHAR; // Unsigned int. The same size as a pointer. typedef SelectIntPointerType<uint32, uint64, sizeof(void*)>::TIntPointer UPTRINT; // Signed int. The same size as a pointer. typedef SelectIntPointerType<int32, int64, sizeof(void*)>::TIntPointer PTRINT; // Unsigned int. The same size as a pointer. typedef UPTRINT SIZE_T; // Signed int. The same size as a pointer. typedef PTRINT SSIZE_T; typedef int32 TYPE_OF_NULL; typedef decltype(nullptr) TYPE_OF_NULLPTR; };
using UTF8CHAR = char8_t;

Char8_t是 C++20 引入的新字符类型,用于表示 UTF-8 编码的字符单元(8 位)

示例:

UTF8CHAR c = u8'A'; // 使用 u8 前缀的字符字面量 const UTF8CHAR* str = u8"虚幻"; // UTF-8 字符串
// A 32-bit character type. In-memory only. 32-bit representation. Should really be char32_t but making this the generic option is easier for compilers which don't fully support C++11 yet (i.e. MSVC). [[deprecated("CHAR32 has been deprecated - please use UTF32CHAR instead.")]] // not using UE_DEPRECATED(5.6, "...") for dependency reasons typedef uint32 CHAR32; // A 32-bit character type. In-memory only. 32-bit representation. using UTF32CHAR = char32_t;

CHAR32已弃用,并建议使用UTF32CHARchar32_t替代

示例:

UTF32CHAR ch1 = U'A'; // 基本拉丁字母 UTF32CHAR ch2 = U'好'; // 中文字符 UTF32CHAR ch3 = U'\U0001F600'; // 表情符号(U+1F600,需要双引号转义)
// Unsigned int. The same size as a pointer. typedef SelectIntPointerType<uint32, uint64, sizeof(void*)>::TIntPointer UPTRINT; // Signed int. The same size as a pointer. typedef SelectIntPointerType<int32, int64, sizeof(void*)>::TIntPointer PTRINT; // Unsigned int. The same size as a pointer. typedef UPTRINT SIZE_T; // Signed int. The same size as a pointer. typedef PTRINT SSIZE_T; typedef int32 TYPE_OF_NULL; typedef decltype(nullptr) TYPE_OF_NULLPTR;

利用模板SelectIntPointerType,根据sizeof(void*)的值自动选择

若指针大小为 4 字节,则UPTRINTuint32, PTRINTint32

若指针大小为 8 字节,则UPTRINTuint64, PTRINTint64

SIZE_T定义为UPTRINTSSIZE_T:定义为PTRINT,大小与指针相同

保证了无论平台是 32 位还是 64 位,SIZE_TSSIZE_T都能容纳任何内存地址或大小,避免溢出

WindowsPlatform.h

完整路径:UE_5.7\Engine\Source\Runtime\Core\Public\Windows\WindowsPlatform.h

/** * Windows specific types **/ struct FWindowsPlatformTypes : public FGenericPlatformTypes { #ifdef _WIN64 typedef unsigned __int64 SIZE_T; typedef __int64 SSIZE_T; #else typedef unsigned long SIZE_T; typedef long SSIZE_T; #endif #if USE_UTF8_TCHARS typedef UTF8CHAR TCHAR; #endif }; typedef FWindowsPlatformTypes FPlatformTypes;

FWindowsPlatformTypes是一个结构体,继承自FGenericPlatformTypes

64 位 Windows,指针大小为 8 字节

unsigned __int64:无符号 64 位整数,对应SIZE_T

__int64:有符号 64 位整数,对应SSIZE_T

32 位 Windows,指针大小为 4 字节
unsigned long:无符号 32 位整数,对应SIZE_T

long:有符号 32 位整数,对应SSIZE_T

USE_UTF8_TCHARS是一个用户定义的宏,如果该宏被定义,则将TCHAR定义为UTF8CHAR

Platform.h

完整路径:UE_5.7\Engine\Source\Runtime\Core\Public\HAL\Platform.h

//~ Unsigned base types. /// An 8-bit unsigned integer. typedef FPlatformTypes::uint8 uint8; /// A 16-bit unsigned integer. typedef FPlatformTypes::uint16 uint16; /// A 32-bit unsigned integer. typedef FPlatformTypes::uint32 uint32; /// A 64-bit unsigned integer. typedef FPlatformTypes::uint64 uint64; //~ Signed base types. /// An 8-bit signed integer. typedef FPlatformTypes::int8 int8; /// A 16-bit signed integer. typedef FPlatformTypes::int16 int16; /// A 32-bit signed integer. typedef FPlatformTypes::int32 int32; /// A 64-bit signed integer. typedef FPlatformTypes::int64 int64; //~ Character types. /// An ANSI character. Normally a signed type. typedef FPlatformTypes::ANSICHAR ANSICHAR; /// A wide character. Normally a signed type. typedef FPlatformTypes::WIDECHAR WIDECHAR; /// Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requirements of the licensee. typedef FPlatformTypes::TCHAR TCHAR; /// An 8-bit character containing a UTF8 (Unicode, 8-bit, variable-width) code unit. typedef FPlatformTypes::UTF8CHAR UTF8CHAR; /// A 16-bit character containing a UCS2 (Unicode, 16-bit, fixed-width) code unit, used for compatibility with 'Windows TCHAR' across multiple platforms. typedef FPlatformTypes::CHAR16 UCS2CHAR; /// A 16-bit character containing a UTF16 (Unicode, 16-bit, variable-width) code unit. typedef FPlatformTypes::CHAR16 UTF16CHAR; /// A 32-bit character containing a UTF32 (Unicode, 32-bit, fixed-width) code unit. typedef FPlatformTypes::UTF32CHAR UTF32CHAR; /// An unsigned integer the same size as a pointer typedef FPlatformTypes::UPTRINT UPTRINT; /// A signed integer the same size as a pointer typedef FPlatformTypes::PTRINT PTRINT; /// An unsigned integer the same size as a pointer, the same as UPTRINT typedef FPlatformTypes::SIZE_T SIZE_T; /// An integer the same size as a pointer, the same as PTRINT typedef FPlatformTypes::SSIZE_T SSIZE_T; /// The type of the NULL constant. typedef FPlatformTypes::TYPE_OF_NULL TYPE_OF_NULL; /// The type of the C++ nullptr keyword. typedef FPlatformTypes::TYPE_OF_NULLPTR TYPE_OF_NULLPTR;

这段代码将平台类型转换为全局类型,实现了全平台的字符类型转换

http://www.jsqmd.com/news/477221/

相关文章:

  • Python 基础知识
  • 基于深度学习车牌识别方案
  • PyAPS下载Climate Data Store数据
  • VM虚拟机使用的镜像文件下载
  • Visual Studio - 自动变量和局部变量十六/十进制查看
  • 数字图像加密关键技术的研究与实现(Python)
  • Python3 ---关于numpy的方法总结笔记。
  • “前端已死”的声音逐渐兴起。前端已死?尊嘟假嘟?
  • 大学生HTML期末大作业——HTML+CSS+JavaScript小说网站(起点)
  • Marqo:一站式向量搜索引擎,助力您的AI应用
  • 【C++BFS算法】752 打开转盘锁
  • QLoRA中的对抗性生成:提升模型对恶意输入的抵抗力
  • C++11——声明
  • 写字基本功 - 阿拉伯数字
  • 随笔:家庭组网优化[光猫与路由连接,增加室内WiFi信号覆盖]
  • 大数据-246 离线数仓 - 电商分析 Hive 拉链表实战:初始化、每日增量更新、回滚脚本与错误排查
  • 3.7-STL(七)(map篇)
  • Qcom平台通过Hexagon IDE 测试程序性能指导
  • 如何快速实现prettier-vscode多语言界面配置:终极国际化指南
  • 2026年PPR堵头优质源头厂家推荐,哪家性价比高 - 工业设备
  • 2026年泸县黄金回收机构排名,黄金回收免费上门正规商家全解析 - 工业品牌热点
  • Linux 环境变量详解
  • 如何为AppManager贡献代码:完整的Android应用管理项目开发者指南
  • Ant Design Blazor 快速创建项目
  • Mysql 中数据主键类型不一样导致数据插入速度快慢问题
  • 5个必学的AST Explorer使用技巧:快速掌握代码分析神器
  • 如何从源码构建Sigil:跨平台EPUB编辑器的完整指南
  • 【01最短路 BFS】1368. 使网格图至少有一条有效路径的最小代价
  • RLHF在多模态领域的应用:MM-RLHF框架与视觉语言模型对齐技术
  • Taming Transformers完整贡献指南:10个技巧助你成为AI图像合成专家